Conditions | 6 |
Paths | 18 |
Total Lines | 33 |
Code Lines | 20 |
Lines | 0 |
Ratio | 0 % |
Tests | 14 |
CRAP Score | 6 |
Changes | 0 |
1 | <?php |
||
24 | 3 | public function updateCollection( |
|
25 | Collection $collection, |
||
26 | array $input, |
||
27 | callable $extractComparator, |
||
28 | callable $createElement, |
||
29 | callable $removeElement = null |
||
30 | ): Collection { |
||
31 | 3 | $existingElements = []; |
|
32 | |||
33 | 3 | foreach ($collection as $element) { |
|
34 | 2 | $key = $extractComparator($element); |
|
35 | |||
36 | 2 | $existingElements[$key] = $element; |
|
37 | } |
||
38 | |||
39 | 3 | foreach ($input as $value) { |
|
40 | 2 | if (true !== isset($existingElements[$value])) { |
|
41 | 2 | $collection->add($createElement($value)); |
|
42 | } |
||
43 | |||
44 | 2 | unset($existingElements[$value]); |
|
45 | } |
||
46 | |||
47 | 3 | foreach ($existingElements as $key => $element) { |
|
48 | 2 | $collection->removeElement($element); |
|
49 | |||
50 | 2 | if (null !== $removeElement) { |
|
51 | 2 | $removeElement($element); |
|
52 | } |
||
53 | } |
||
54 | |||
55 | 3 | return $collection; |
|
56 | } |
||
57 | } |