Passed
Push — main ( d9b765...47c28f )
by Pol
01:54
created

ReductionIterableAggregate::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 5
ccs 4
cts 4
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, W>
22
 */
23
final class ReductionIterableAggregate implements IteratorAggregate
24
{
25
    /**
26
     * @var Closure(W, T, TKey, iterable<TKey, T>): W
27
     */
28
    private Closure $closure;
29
30
    /**
31
     * @var W
0 ignored issues
show
Bug introduced by
The type loophp\iterators\W was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
     */
33
    private $initial;
34
35
    /**
36
     * @var iterable<TKey, T>
37
     */
38
    private iterable $iterable;
39
40
    /**
41
     * @param iterable<TKey, T> $iterable
42
     * @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...
43
     * @param mixed $initial
44
     */
45 1
    public function __construct(iterable $iterable, Closure $closure, $initial)
46
    {
47 1
        $this->iterable = $iterable;
48 1
        $this->closure = $closure;
49 1
        $this->initial = $initial;
50
    }
51
52
    /**
53
     * @return Generator<TKey, W>
54
     */
55 1
    public function getIterator(): Generator
56
    {
57 1
        $initial = $this->initial;
58
59 1
        foreach ($this->iterable as $key => $value) {
60 1
            $initial = ($this->closure)($initial, $value, $key, $this->iterable);
61
62 1
            yield $key => $initial;
63
        }
64
    }
65
}
66