ScanLeft1::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 1

Size

Total Lines 32
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 12
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 32
ccs 14
cts 14
cp 1
crap 2
rs 9.8666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace loophp\collection\Operation;
6
7
use Closure;
8
use Generator;
9
use loophp\iterators\IterableIteratorAggregate;
10
11
/**
12
 * @immutable
13
 *
14
 * @template TKey
15
 * @template T
16
 */
17
final class ScanLeft1 extends AbstractOperation
18
{
19
    /**
20
     * @template V
21
     *
22
     * @return Closure(callable((T|V), T, TKey, iterable<TKey, T>): (T|V)): Closure(iterable<TKey, T>): Generator<TKey|int, T|V>
23
     */
24 48
    public function __invoke(): Closure
25
    {
26 48
        return
27
            /**
28
             * @param callable((T|V), T, TKey, iterable<TKey, T>): (T|V) $callback
29
             *
30
             * @return Closure(iterable<TKey, T>): Generator<TKey|int, T|V>
31
             */
32 48
            static fn (callable $callback): Closure =>
33
                /**
34
                 * @param iterable<TKey, T> $iterable
35
                 *
36
                 * @return Generator<TKey|int, T|V>
37
                 */
38 48
                static function (iterable $iterable) use ($callback): Generator {
39 48
                    $iteratorAggregate = new IterableIteratorAggregate($iterable);
40
41 48
                    $iteratorInitial = $iteratorAggregate->getIterator();
42
43 48
                    if (false === $iteratorInitial->valid()) {
44 10
                        return;
45
                    }
46
47 38
                    $initial = $iteratorInitial->current();
48
49
                    /** @var Closure(iterable<TKey, T>): Generator<TKey|int, T|V> $pipe */
50 38
                    $pipe = (new Pipe())()(
51 38
                        (new Tail())(),
52 38
                        (new Reduction())()($callback)($initial),
53 38
                    );
54
55 38
                    yield from $pipe($iteratorAggregate);
56 48
                };
57
    }
58
}
59