Conditions | 6 |
Paths | 7 |
Total Lines | 19 |
Code Lines | 11 |
Lines | 0 |
Ratio | 0 % |
Tests | 11 |
CRAP Score | 6 |
Changes | 0 |
1 | <?php |
||
14 | 9 | function iterable_unique(iterable $iterable, callable $serialize = null): \Generator |
|
15 | { |
||
16 | 8 | $fastMap = []; // entries as keys, entry must be a string |
|
17 | 8 | $slowMap = []; // non-string entries |
|
18 | |||
19 | 8 | foreach ($iterable as $key => $value) { |
|
20 | 7 | $entry = isset($serialize) ? call_user_func($serialize, $value, $key) : $value; |
|
21 | |||
22 | 7 | if (is_string($entry) ? isset($fastMap[$entry]) : in_array($entry, $slowMap, true)) { |
|
23 | 7 | continue; |
|
24 | } |
||
25 | |||
26 | 7 | if (is_string($entry)) { |
|
27 | 6 | $fastMap[$entry] = true; |
|
28 | } else { |
||
29 | 1 | $slowMap[] = $entry; |
|
30 | } |
||
31 | |||
32 | 7 | yield $key => $value; |
|
33 | } |
||
35 |