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; |
|
|
|
|
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(); |
|
|
|
|
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
|
|
|
|
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.