iterable_flatten()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 7
nc 4
nop 2
dl 0
loc 12
ccs 8
cts 8
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
 * Walk through all sub-iterables (array, Iterator or IteratorAggregate) and combine them.
9
 *
10
 * @param iterable $iterable
11
 * @param bool     $preserveKeys
12
 * @return \Generator
13
 */
14 12
function iterable_flatten(iterable $iterable, bool $preserveKeys = false): \Generator
0 ignored issues
show
introduced by
Function Improved\iterable_flatten() has parameter $iterable with no value type specified in iterable type iterable.
Loading history...
introduced by
Function Improved\iterable_flatten() return type has no value type specified in iterable type Generator.
Loading history...
15
{
16 11
    $counter = 0;
17
18 11
    foreach ($iterable as $topKey => $element) {
19 10
        if (!is_iterable($element)) {
20 10
            yield ($preserveKeys ? $topKey : $counter++) => $element;
21 10
            continue;
22
        }
23
24 10
        foreach ($element as $key => $item) {
25 10
            yield ($preserveKeys ? $key : $counter++) => $item;
26
        }
27
    }
28
}
29