iterable_sort_keys()   A
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 32
nop 2
dl 0
loc 23
ccs 15
cts 15
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Improved;
6
7
/**
8
 * Sort all elements of an iterator based on the key.
9
 *
10
 * @param iterable     $iterable
11
 * @param callable|int $compare   SORT_* flag or callback comparator function
12
 * @return \Generator
13
 */
14 9
function iterable_sort_keys(iterable $iterable, $compare): \Generator
0 ignored issues
show
introduced by
Function Improved\iterable_sort_keys() has parameter $iterable with no value type specified in iterable type iterable.
Loading history...
introduced by
Function Improved\iterable_sort_keys() return type has no value type specified in iterable type Generator.
Loading history...
15
{
16 8
    type_check($compare, ['callable', 'int'], new \TypeError("Expected compare to be callable or integer, %s given"));
17
18 8
    $comparator = is_int($compare) ? null : $compare;
19 8
    $flags = is_int($compare) ? $compare : 0;
20
21 8
    $keys = [];
22 8
    $values = [];
23
24 8
    foreach ($iterable as $key => $value) {
25 7
        $keys[] = $key;
26 7
        $values[] = $value;
27
    }
28
29 8
    unset($iterable);
30
31 8
    isset($comparator)
32 2
        ? uasort($keys, $comparator)
33 6
        : asort($keys, $flags);
34
35 8
    foreach ($keys as $index => $key) {
36 7
        yield $key => $values[$index];
37
    }
38
}
39