Conditions | 6 |
Paths | 10 |
Total Lines | 22 |
Code Lines | 13 |
Lines | 0 |
Ratio | 0 % |
Tests | 13 |
CRAP Score | 6 |
Changes | 0 |
1 | <?php |
||
38 | 15 | public function patch( array $base, Diff $diff ): array { |
|
39 | 15 | if ( $diff->looksAssociative() ) { |
|
40 | 1 | $this->handleError( 'ListPatcher can only patch using non-associative diffs' ); |
|
41 | } |
||
42 | |||
43 | 15 | foreach ( $diff as $diffOp ) { |
|
44 | 10 | if ( $diffOp instanceof DiffOpAdd ) { |
|
45 | 7 | $base[] = $diffOp->getNewValue(); |
|
46 | 6 | } elseif ( $diffOp instanceof DiffOpRemove ) { |
|
47 | 6 | $key = array_search( $diffOp->getOldValue(), $base, true ); |
|
48 | |||
49 | 6 | if ( $key === false ) { |
|
50 | 2 | $this->handleError( 'Cannot remove an element from a list if it is not present' ); |
|
51 | 2 | continue; |
|
52 | } |
||
53 | |||
54 | 9 | unset( $base[$key] ); |
|
55 | } |
||
56 | } |
||
57 | |||
58 | 15 | return $base; |
|
59 | } |
||
60 | |||
62 |