| Conditions | 8 |
| Paths | 56 |
| Total Lines | 30 |
| Code Lines | 20 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 19 |
| CRAP Score | 8 |
| Changes | 0 | ||
| 1 | <?php declare(strict_types=1); |
||
| 15 | function iterable_sort(iterable $iterable, $compare, bool $preserveKeys = false): \Generator |
||
| 16 | { |
||
| 17 | 11 | expect_type( |
|
| 18 | 11 | $compare, |
|
| 19 | 11 | ['callable', 'int'], |
|
| 20 | 11 | \TypeError::class, |
|
| 21 | 11 | "Expected compare to be a callable or integer, %s given" |
|
| 22 | ); |
||
| 23 | |||
| 24 | 11 | $comparator = is_int($compare) ? null : $compare; |
|
| 25 | 11 | $flags = is_int($compare) ? $compare : 0; |
|
| 26 | |||
| 27 | 11 | ['keys' => $keys, 'values' => $values] = $preserveKeys |
|
| 28 | 3 | ? iterable_separate($iterable) |
|
| 29 | 8 | : ['keys' => null, 'values' => iterable_to_array($iterable, false)]; |
|
| 30 | |||
| 31 | 11 | if (!is_array($values)) { |
|
| 32 | throw new \UnexpectedValueException("Values should be an array"); // @codeCoverageIgnore |
||
| 33 | } |
||
| 34 | |||
| 35 | 11 | isset($comparator) |
|
| 36 | 3 | ? uasort($values, $comparator) |
|
| 37 | 8 | : asort($values, $flags); |
|
| 38 | |||
| 39 | 11 | $counter = 0; |
|
| 40 | 11 | unset($iterable); |
|
| 41 | |||
| 42 | 11 | foreach ($values as $index => $value) { |
|
| 43 | 10 | $key = isset($keys) ? $keys[$index] : $counter++; |
|
| 44 | 10 | yield $key => $value; |
|
| 45 | } |
||
| 47 |