Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.00% |
24 / 30 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| AliasGroupListPatcher | |
80.00% |
24 / 30 |
|
75.00% |
3 / 4 |
19.31 | |
0.00% |
0 / 1 |
| patchAliasGroupList | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
| applyAliasGroupDiff | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
| containsOperationsOnOldValues | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getPatchedAliases | |
64.71% |
11 / 17 |
|
0.00% |
0 / 1 |
10.81 | |||
| 1 | <?php |
| 2 | |
| 3 | declare( strict_types = 1 ); |
| 4 | |
| 5 | namespace EntitySchema\Services\Diff; |
| 6 | |
| 7 | use Diff\DiffOp\Diff\Diff; |
| 8 | use Diff\DiffOp\DiffOpAdd; |
| 9 | use Diff\DiffOp\DiffOpChange; |
| 10 | use Diff\DiffOp\DiffOpRemove; |
| 11 | use Diff\Patcher\PatcherException; |
| 12 | |
| 13 | /** |
| 14 | * Copied and adjusted from wikibase/data-model; |
| 15 | * originally authored by Thiemo Kreuz and Jeroen De Dauw |
| 16 | * |
| 17 | * @license GPL-2.0-or-later |
| 18 | */ |
| 19 | class AliasGroupListPatcher { |
| 20 | |
| 21 | /** |
| 22 | * @throws PatcherException |
| 23 | */ |
| 24 | public function patchAliasGroupList( array $groups, ?Diff $patch = null ): array { |
| 25 | if ( $patch === null ) { |
| 26 | return $groups; |
| 27 | } |
| 28 | foreach ( $patch as $lang => $diffOp ) { |
| 29 | $groups = $this->applyAliasGroupDiff( $groups, $lang, $diffOp ); |
| 30 | } |
| 31 | |
| 32 | return $groups; |
| 33 | } |
| 34 | |
| 35 | private function applyAliasGroupDiff( array $groups, string $lang, Diff $patch ): array { |
| 36 | $hasLang = !empty( $groups[$lang] ); |
| 37 | |
| 38 | if ( $hasLang || !$this->containsOperationsOnOldValues( $patch ) ) { |
| 39 | $aliases = $hasLang ? $groups[$lang] : []; |
| 40 | $aliases = $this->getPatchedAliases( $aliases, $patch ); |
| 41 | $groups[$lang] = $aliases; |
| 42 | } |
| 43 | |
| 44 | return $groups; |
| 45 | } |
| 46 | |
| 47 | private function containsOperationsOnOldValues( Diff $diff ): bool { |
| 48 | return $diff->getChanges() !== [] |
| 49 | || $diff->getRemovals() !== []; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @see ListPatcher |
| 54 | * |
| 55 | * @param string[] $aliases |
| 56 | * @param Diff $patch |
| 57 | * |
| 58 | * @throws PatcherException |
| 59 | * @return string[] |
| 60 | */ |
| 61 | private function getPatchedAliases( array $aliases, Diff $patch ): array { |
| 62 | foreach ( $patch as $diffOp ) { |
| 63 | switch ( true ) { |
| 64 | case $diffOp instanceof DiffOpAdd: |
| 65 | $aliases[] = $diffOp->getNewValue(); |
| 66 | break; |
| 67 | |
| 68 | case $diffOp instanceof DiffOpChange: |
| 69 | $key = array_search( $diffOp->getOldValue(), $aliases, true ); |
| 70 | if ( $key !== false ) { |
| 71 | unset( $aliases[$key] ); |
| 72 | $aliases[] = $diffOp->getNewValue(); |
| 73 | } |
| 74 | break; |
| 75 | |
| 76 | case $diffOp instanceof DiffOpRemove: |
| 77 | $key = array_search( $diffOp->getOldValue(), $aliases, true ); |
| 78 | if ( $key !== false ) { |
| 79 | unset( $aliases[$key] ); |
| 80 | } |
| 81 | break; |
| 82 | |
| 83 | default: |
| 84 | throw new PatcherException( 'Invalid aliases diff' ); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | return array_values( $aliases ); |
| 89 | } |
| 90 | |
| 91 | } |