Unfold::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 23
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 7
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 23
ccs 8
cts 8
cp 1
crap 2
rs 10
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
final class Unfold extends AbstractOperation
14
{
15
    /**
16
     * @template TKey
17
     * @template T
18
     *
19
     * @return Closure(iterable<TKey, T>): Closure(callable(T...): iterable<array-key, T>): Closure(): Generator<int, iterable<array-key, T>>
20
     */
21 2
    public function __invoke(): Closure
22
    {
23 2
        return
24
            /**
25
             * @param iterable<array-key, T> $parameters
26
             *
27
             * @return Closure(callable(T...): iterable<array-key, T>): Closure(): Generator<int, iterable<array-key, T>>
28
             */
29 2
            static fn (iterable $parameters): Closure =>
30
                /**
31
                 * @param callable(T...): iterable<array-key, T> $callback
32
                 *
33
                 * @return Closure(): Generator<int, iterable<array-key, T>>
34
                 */
35 2
                static fn (callable $callback): Closure =>
36
                    /**
37
                     * @return Generator<int, iterable<array-key, T>>
38
                     */
39 2
                    static function () use ($parameters, $callback): Generator {
40 2
                        while (true) {
41 2
                            $parameters = $callback(...$parameters);
42
43 2
                            yield $parameters;
44
                        }
45 2
                    };
46
    }
47
}
48