Flatten   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
eloc 11
c 1
b 1
f 0
dl 0
loc 26
ccs 11
cts 11
cp 1
rs 10
wmc 4

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 21 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\collection\Operation;
6
7
use Closure;
8
use Generator;
9
10
/**
11
 * @immutable
12
 *
13
 * @template TKey
14
 * @template T
15
 */
16
final class Flatten extends AbstractOperation
17
{
18
    /**
19
     * @return Closure(int): Closure(iterable<TKey, T>): Generator<mixed, mixed>
20
     */
21 123
    public function __invoke(): Closure
22
    {
23 123
        return
24
            /**
25
             * @return Closure(iterable<TKey, T>): Generator<mixed, mixed>
26
             */
27 123
            static fn (int $depth): Closure =>
28
                /**
29
                 * @param iterable<TKey, T> $iterable
30
                 */
31 123
                static function (iterable $iterable) use ($depth): Generator {
32 121
                    foreach ($iterable as $key => $value) {
33 117
                        if (!is_iterable($value)) {
34 8
                            yield $key => $value;
35
36 8
                            continue;
37
                        }
38
39 117
                        yield from (1 !== $depth)
40 4
                            ? (new Flatten())()($depth - 1)($value)
41 117
                            : $value;
42
                    }
43 123
                };
44
    }
45
}
46