ReductionIterableAggregate::getIterator()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 2
eloc 4
c 2
b 1
f 0
nc 2
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace loophp\iterators;
11
12
use Closure;
13
use Generator;
14
use IteratorAggregate;
15
16
/**
17
 * @template TKey
18
 * @template T
19
 * @template W
20
 *
21
 * @implements IteratorAggregate<TKey|int, W>
22
 */
23
final class ReductionIterableAggregate implements IteratorAggregate
24
{
25
    /**
26
     * @param iterable<TKey, T> $iterable
27
     * @param (Closure(W, T, TKey, iterable<TKey, T>): W) $closure
0 ignored issues
show
Documentation Bug introduced by
The doc comment (Closure(W, T, TKey, iterable<TKey, T>): W) at position 1 could not be parsed: Expected ')' at position 1, but found 'Closure'.
Loading history...
28
     * @param W $initial
29
     */
30 2
    public function __construct(private iterable $iterable, private Closure $closure, private mixed $initial)
31
    {
32 2
    }
33
34
    /**
35
     * @return Generator<TKey|int, W>
36
     */
37 2
    public function getIterator(): Generator
38
    {
39 2
        yield $initial = $this->initial;
40
41 2
        foreach ($this->iterable as $key => $value) {
42 1
            $initial = ($this->closure)($initial, $value, $key, $this->iterable);
43
44 1
            yield $key => $initial;
45
        }
46
    }
47
}
48