ClosureIterator::next()   A
last analyzed

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 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
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 Generator;
13
use Iterator;
14
15
/**
16
 * @template TKey
17
 * @template T
18
 *
19
 * @implements Iterator<TKey, T>
20
 */
21
final class ClosureIterator implements Iterator
22
{
23
    /**
24
     * @var callable(mixed): iterable<TKey, T>
25
     */
26
    private $callable;
27
28
    /**
29
     * @var Generator<TKey, T, mixed, void>
30
     */
31
    private Generator $generator;
32
33
    /**
34
     * @param callable(mixed): iterable<TKey, T> $callable
35
     * @param iterable<int, mixed> $parameters
36
     */
37 9
    public function __construct(callable $callable, private iterable $parameters = [])
38
    {
39 9
        $this->callable = $callable;
40 9
        $this->generator = $this->getGenerator();
41
    }
42
43
    /**
44
     * @return T
45
     */
46 7
    public function current(): mixed
47
    {
48 7
        return $this->generator->current();
49
    }
50
51
    /**
52
     * @return TKey
0 ignored issues
show
Bug introduced by
The type loophp\iterators\TKey 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...
53
     */
54 3
    public function key(): mixed
55
    {
56 3
        return $this->generator->key();
57
    }
58
59 6
    public function next(): void
60
    {
61 6
        $this->generator->next();
62
    }
63
64 3
    public function rewind(): void
65
    {
66 3
        $this->generator = $this->getGenerator();
67
    }
68
69 5
    public function valid(): bool
70
    {
71 5
        return $this->generator->valid();
72
    }
73
74
    /**
75
     * @return Generator<TKey, T, mixed, void>
76
     */
77 9
    private function getGenerator(): Generator
78
    {
79 9
        yield from ($this->callable)(...$this->parameters);
80
    }
81
}
82