Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
75.00% covered (warning)
75.00%
3 / 4
CRAP
76.67% covered (warning)
76.67%
23 / 30
EntitySchema\Services\Diff\AliasGroupListPatcher
0.00% covered (danger)
0.00%
0 / 1
75.00% covered (warning)
75.00%
3 / 4
19.25
76.67% covered (warning)
76.67%
23 / 30
 patchAliasGroupList
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
5 / 5
 applyAliasGroupDiff
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
6 / 6
 containsOperationsOnOldValues
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
2 / 2
 getPatchedAliases
0.00% covered (danger)
0.00%
0 / 1
10.42
58.82% covered (warning)
58.82%
10 / 17
<?php
namespace EntitySchema\Services\Diff;
use Diff\DiffOp\Diff\Diff;
use Diff\DiffOp\DiffOpAdd;
use Diff\DiffOp\DiffOpChange;
use Diff\DiffOp\DiffOpRemove;
use Diff\Patcher\PatcherException;
/**
 * Copied and adjusted from wikibase/data-model;
 * originally authored by Thiemo Kreuz and Jeroen De Dauw
 *
 * @license GPL-2.0-or-later
 */
class AliasGroupListPatcher {
    /**
     * @throws PatcherException
     */
    public function patchAliasGroupList( array $groups, Diff $patch = null ): array {
        if ( $patch === null ) {
            return $groups;
        }
        foreach ( $patch as $lang => $diffOp ) {
            $groups = $this->applyAliasGroupDiff( $groups, $lang, $diffOp );
        }
        return $groups;
    }
    /**
     * @param array $groups
     * @param string $lang
     * @param Diff $patch
     *
     * @return array
     */
    private function applyAliasGroupDiff( array $groups, $lang, Diff $patch ) {
        $hasLang = !empty( $groups[$lang] );
        if ( $hasLang || !$this->containsOperationsOnOldValues( $patch ) ) {
            $aliases = $hasLang ? $groups[$lang] : [];
            $aliases = $this->getPatchedAliases( $aliases, $patch );
            $groups[$lang] = $aliases;
        }
        return $groups;
    }
    /**
     * @param Diff $diff
     *
     * @return bool
     */
    private function containsOperationsOnOldValues( Diff $diff ) {
        return $diff->getChanges() !== []
            || $diff->getRemovals() !== [];
    }
    /**
     * @see ListPatcher
     *
     * @param string[] $aliases
     * @param Diff $patch
     *
     * @throws PatcherException
     * @return string[]
     */
    private function getPatchedAliases( array $aliases, Diff $patch ) {
        foreach ( $patch as $diffOp ) {
            switch ( true ) {
                case $diffOp instanceof DiffOpAdd:
                    $aliases[] = $diffOp->getNewValue();
                    break;
                case $diffOp instanceof DiffOpChange:
                    $key = array_search( $diffOp->getOldValue(), $aliases, true );
                    if ( $key !== false ) {
                        unset( $aliases[$key] );
                        $aliases[] = $diffOp->getNewValue();
                    }
                    break;
                case $diffOp instanceof DiffOpRemove:
                    $key = array_search( $diffOp->getOldValue(), $aliases, true );
                    if ( $key !== false ) {
                        unset( $aliases[$key] );
                    }
                    break;
                default:
                    throw new PatcherException( 'Invalid aliases diff' );
            }
        }
        return array_values( $aliases );
    }
}