Conditions | 4 |
Paths | 6 |
Total Lines | 21 |
Code Lines | 11 |
Lines | 0 |
Ratio | 0 % |
Tests | 12 |
CRAP Score | 4 |
Changes | 0 |
1 | <?php |
||
14 | 9 | function iterable_group(iterable $iterable, callable $grouping): \Generator |
|
15 | { |
||
16 | 8 | $groups = []; |
|
17 | 8 | $values = []; |
|
18 | |||
19 | 8 | foreach ($iterable as $key => $value) { |
|
20 | 7 | $group = call_user_func($grouping, $value, $key); |
|
21 | |||
22 | 7 | $index = array_search($group, $groups, true); |
|
23 | |||
24 | 7 | if ($index === false) { |
|
25 | 7 | $index = array_push($groups, $group) - 1; |
|
26 | } |
||
27 | |||
28 | 7 | $values[$index][] = $value; |
|
29 | } |
||
30 | |||
31 | 8 | unset($iterable); |
|
32 | |||
33 | 8 | foreach ($groups as $index => $group) { |
|
34 | 7 | yield $group => $values[$index]; |
|
35 | } |
||
37 |