Conditions | 7 |
Paths | 9 |
Total Lines | 26 |
Code Lines | 16 |
Lines | 0 |
Ratio | 0 % |
Tests | 16 |
CRAP Score | 7 |
Changes | 0 |
1 | <?php |
||
14 | 15 | function iterable_reverse(iterable $iterable, bool $preserveKeys = false): \Generator |
|
15 | { |
||
16 | 14 | if (is_array($iterable)) { |
|
17 | 2 | $values = array_reverse($iterable, $preserveKeys); |
|
18 | 12 | } elseif (!$preserveKeys) { |
|
19 | 6 | $values = []; |
|
20 | |||
21 | 6 | foreach ($iterable as $key => $value) { |
|
22 | 5 | array_unshift($values, $value); |
|
23 | } |
||
24 | } else { |
||
25 | /** @var array $keys */ |
||
26 | 6 | $keys = []; |
|
27 | 6 | $values = []; |
|
28 | |||
29 | 6 | foreach ($iterable as $key => $value) { |
|
30 | 5 | array_unshift($keys, $key); |
|
31 | 5 | array_unshift($values, $value); |
|
32 | } |
||
33 | } |
||
34 | |||
35 | 14 | unset($iterable); |
|
36 | |||
37 | 14 | foreach ($values as $index => $value) { |
|
38 | 12 | $key = isset($keys) ? $keys[$index] : $index; |
|
39 | 12 | yield $key => $value; |
|
40 | } |
||
42 |