SequenceIterator::next()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\Sequence\Iterators;
6
7
use Smoren\Sequence\Interfaces\SequenceInterface;
8
use Smoren\Sequence\Interfaces\SequenceIteratorInterface;
9
10
/**
11
 * Iterator implementation for sequences.
12
 *
13
 * @template T
14
 * @implements SequenceIteratorInterface<T>
15
 */
16
class SequenceIterator implements SequenceIteratorInterface
17
{
18
    /**
19
     * @var SequenceInterface<T> sequence for iterating
20
     */
21
    protected SequenceInterface $sequence;
22
    /**
23
     * @var int current iteration index
24
     */
25
    protected int $currentIndex;
26
    /**
27
     * @var T current iteration value
0 ignored issues
show
Bug introduced by
The type Smoren\Sequence\Iterators\T 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...
28
     */
29
    protected $currentValue;
30
31
    /**
32
     * SequenceIterator constructor.
33
     *
34
     * @param SequenceInterface<T> $sequence
35
     */
36 70
    public function __construct(SequenceInterface $sequence)
37
    {
38 70
        $this->sequence = $sequence;
39 70
        $this->currentIndex = 0;
40
    }
41
42
    /**
43
     * {@inheritDoc}
44
     */
45 59
    public function current()
46
    {
47 59
        return $this->currentValue;
48
    }
49
50
    /**
51
     * {@inheritDoc}
52
     */
53 59
    public function next(): void
54
    {
55 59
        $this->currentIndex++;
56 59
        $this->currentValue = $this->sequence->getNextValue($this->currentValue);
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62 41
    public function key(): int
63
    {
64 41
        return $this->currentIndex;
65
    }
66
67
    /**
68
     * {@inheritDoc}
69
     */
70 70
    public function valid(): bool
71
    {
72 70
        $count = $this->sequence->isInfinite() ? INF : $this->sequence->count();
73 70
        return $this->currentIndex >= 0 && $this->currentIndex < $count;
74
    }
75
76
    /**
77
     * {@inheritDoc}
78
     */
79 70
    public function rewind(): void
80
    {
81 70
        $this->currentIndex = 0;
82 70
        $this->currentValue = $this->sequence->getStartValue();
83
    }
84
}
85