Passed
Push — main ( 4f242a...c97b10 )
by Pol
01:49
created

ReduceIterableAggregate::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 3
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 ReduceIterableAggregate implements IteratorAggregate
24
{
25
    /**
26
     * @var Closure(W, T, TKey, iterable<TKey, T>): W
27
     */
28
    private Closure $closure;
29
30
    /**
31
     * @param iterable<TKey, T> $iterable
32
     * @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...
33
     * @param W $initial
34
     */
35 1
    public function __construct(private iterable $iterable, Closure $closure, private mixed $initial)
36
    {
37 1
        $this->closure = $closure;
38
    }
39
40
    /**
41
     * @return Generator<int, W>
42
     */
43 1
    public function getIterator(): Generator
44
    {
45 1
        $initial = $this->initial;
46
47 1
        foreach ($this->iterable as $key => $value) {
48 1
            $initial = ($this->closure)($initial, $value, $key, $this->iterable);
49
        }
50
51 1
        yield $initial;
52
    }
53
}
54