Passed
Push — master ( e44dc3...5dec7d )
by Kirill
07:30
created

LookaheadIterator::rewind()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
c 0
b 0
f 0
ccs 0
cts 6
cp 0
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * This file is part of Railt package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Railt\Compiler\Iterator;
11
12
/**
13
 * Class LookaheadIterator
14
 */
15
class LookaheadIterator extends \IteratorIterator
16
{
17
    /**
18
     * Current iterator.
19
     *
20
     * @var \Iterator
21
     */
22
    protected $_iterator;
23
24
    /**
25
     * Current key.
26
     *
27
     * @var mixed
28
     */
29
    protected $_key = 0;
30
31
    /**
32
     * Current value.
33
     *
34
     * @var mixed
35
     */
36
    protected $_current;
37
38
    /**
39
     * Whether the current element is valid or not.
40
     *
41
     * @var bool
42
     */
43
    protected $_valid = false;
44
45
    /**
46
     * LookaheadIterator constructor.
47
     * @param \Traversable $iterator
48
     */
49
    public function __construct(\Traversable $iterator)
50
    {
51
        $this->_iterator = $iterator;
0 ignored issues
show
Documentation Bug introduced by
$iterator is of type object<Traversable>, but the property $_iterator was declared to be of type object<Iterator>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
52
    }
53
54
    /**
55
     * Return the current element.
56
     *
57
     * @return mixed
58
     */
59
    public function current()
60
    {
61
        return $this->_current;
62
    }
63
64
    /**
65
     * Return the key of the current element.
66
     *
67
     * @return mixed
68
     */
69
    public function key()
70
    {
71
        return $this->_key;
72
    }
73
74
    /**
75
     * Rewind the iterator to the first element.
76
     *
77
     * @return void
78
     */
79
    public function rewind()
80
    {
81
        $out = $this->getInnerIterator()->rewind();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $out is correct as $this->getInnerIterator()->rewind() (which targets Iterator::rewind()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
82
        $this->next();
83
84
        return $out;
85
    }
86
87
    /**
88
     * Get inner iterator.
89
     *
90
     * @return \Iterator
91
     */
92
    public function getInnerIterator()
93
    {
94
        return $this->_iterator;
95
    }
96
97
    /**
98
     * Move forward to next element.
99
     *
100
     * @return void
101
     */
102
    public function next()
103
    {
104
        $innerIterator = $this->getInnerIterator();
105
        $this->_valid  = $innerIterator->valid();
106
107
        if (false === $this->_valid) {
108
            return;
109
        }
110
111
        $this->_key     = $innerIterator->key();
112
        $this->_current = $innerIterator->current();
113
114
        return $innerIterator->next();
115
    }
116
117
    /**
118
     * Check if current position is valid.
119
     *
120
     * @return bool
121
     */
122
    public function valid()
123
    {
124
        return $this->_valid;
125
    }
126
127
    /**
128
     * Check whether there is a next element.
129
     *
130
     * @return bool
131
     */
132
    public function hasNext()
133
    {
134
        return $this->getInnerIterator()->valid();
135
    }
136
137
    /**
138
     * Get next value.
139
     *
140
     * @return mixed
141
     */
142
    public function getNext()
143
    {
144
        return $this->getInnerIterator()->current();
145
    }
146
147
    /**
148
     * Get next key.
149
     *
150
     * @return mixed
151
     */
152
    public function getNextKey()
153
    {
154
        return $this->getInnerIterator()->key();
155
    }
156
}
157