Unzip::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 25
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 25
ccs 12
cts 12
cp 1
crap 2
rs 9.9666
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 Unzip extends AbstractOperation
17
{
18
    /**
19
     * @return Closure(iterable<TKey, list<T>>): Generator<int, list<T>>
20
     */
21 2
    public function __invoke(): Closure
22
    {
23 2
        $reduceCallback =
24
            /**
25
             * @param array<int, list<T>> $carry
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, list<T>> at position 4 could not be parsed: Expected '>' at position 4, but found 'list'.
Loading history...
26
             * @param iterable<TKey, T> $value
27
             *
28
             * @return array<int, list<T>>
0 ignored issues
show
Documentation Bug introduced by
The doc comment array<int, list<T>> at position 4 could not be parsed: Expected '>' at position 4, but found 'list'.
Loading history...
29
             */
30 2
            static function (array $carry, iterable $value): array {
31 2
                $index = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $index is dead and can be removed.
Loading history...
32
33 2
                foreach ($value as $carry[$index++][]);
34
35 2
                return $carry;
36 2
            };
37
38
        /** @var Closure(iterable<TKey, list<T>>): Generator<int, list<T>> $pipe */
39 2
        $pipe = (new Pipe())()(
40 2
            (new Reduce())()($reduceCallback)([]),
41 2
            (new Flatten())()(1)
42 2
        );
43
44
        // Point free style.
45 2
        return $pipe;
46
    }
47
}
48