@@ -22,6 +22,9 @@ class TokenIterator
22
22
/** @var int[] */
23
23
private $ savePoints = [];
24
24
25
+ /** @var list<int> */
26
+ private $ skippedTokenTypes = [Lexer::TOKEN_HORIZONTAL_WS ];
27
+
25
28
/**
26
29
* @param list<array{string, int, int}> $tokens
27
30
*/
@@ -30,11 +33,7 @@ public function __construct(array $tokens, int $index = 0)
30
33
$ this ->tokens = $ tokens ;
31
34
$ this ->index = $ index ;
32
35
33
- if ($ this ->tokens [$ this ->index ][Lexer::TYPE_OFFSET ] !== Lexer::TOKEN_HORIZONTAL_WS ) {
34
- return ;
35
- }
36
-
37
- $ this ->index ++;
36
+ $ this ->skipIrrelevantTokens ();
38
37
}
39
38
40
39
@@ -131,12 +130,7 @@ public function consumeTokenType(int $tokenType): void
131
130
}
132
131
133
132
$ this ->index ++;
134
-
135
- if (($ this ->tokens [$ this ->index ][Lexer::TYPE_OFFSET ] ?? -1 ) !== Lexer::TOKEN_HORIZONTAL_WS ) {
136
- return ;
137
- }
138
-
139
- $ this ->index ++;
133
+ $ this ->skipIrrelevantTokens ();
140
134
}
141
135
142
136
@@ -150,12 +144,7 @@ public function consumeTokenValue(int $tokenType, string $tokenValue): void
150
144
}
151
145
152
146
$ this ->index ++;
153
-
154
- if (($ this ->tokens [$ this ->index ][Lexer::TYPE_OFFSET ] ?? -1 ) !== Lexer::TOKEN_HORIZONTAL_WS ) {
155
- return ;
156
- }
157
-
158
- $ this ->index ++;
147
+ $ this ->skipIrrelevantTokens ();
159
148
}
160
149
161
150
@@ -167,10 +156,7 @@ public function tryConsumeTokenValue(string $tokenValue): bool
167
156
}
168
157
169
158
$ this ->index ++;
170
-
171
- if ($ this ->tokens [$ this ->index ][Lexer::TYPE_OFFSET ] === Lexer::TOKEN_HORIZONTAL_WS ) {
172
- $ this ->index ++;
173
- }
159
+ $ this ->skipIrrelevantTokens ();
174
160
175
161
return true ;
176
162
}
@@ -184,10 +170,7 @@ public function tryConsumeTokenType(int $tokenType): bool
184
170
}
185
171
186
172
$ this ->index ++;
187
-
188
- if ($ this ->tokens [$ this ->index ][Lexer::TYPE_OFFSET ] === Lexer::TOKEN_HORIZONTAL_WS ) {
189
- $ this ->index ++;
190
- }
173
+ $ this ->skipIrrelevantTokens ();
191
174
192
175
return true ;
193
176
}
@@ -217,12 +200,34 @@ public function joinUntil(int ...$tokenType): string
217
200
public function next (): void
218
201
{
219
202
$ this ->index ++;
203
+ $ this ->skipIrrelevantTokens ();
204
+ }
220
205
221
- if ($ this ->tokens [$ this ->index ][Lexer::TYPE_OFFSET ] !== Lexer::TOKEN_HORIZONTAL_WS ) {
206
+
207
+ private function skipIrrelevantTokens (): void
208
+ {
209
+ if (!isset ($ this ->tokens [$ this ->index ])) {
222
210
return ;
223
211
}
224
212
225
- $ this ->index ++;
213
+ while (in_array ($ this ->tokens [$ this ->index ][Lexer::TYPE_OFFSET ], $ this ->skippedTokenTypes , true )) {
214
+ if (!isset ($ this ->tokens [$ this ->index + 1 ])) {
215
+ break ;
216
+ }
217
+ $ this ->index ++;
218
+ }
219
+ }
220
+
221
+
222
+ public function addEndOfLineToSkippedTokens (): void
223
+ {
224
+ $ this ->skippedTokenTypes = [Lexer::TOKEN_HORIZONTAL_WS , Lexer::TOKEN_PHPDOC_EOL ];
225
+ }
226
+
227
+
228
+ public function removeEndOfLineFromSkippedTokens (): void
229
+ {
230
+ $ this ->skippedTokenTypes = [Lexer::TOKEN_HORIZONTAL_WS ];
226
231
}
227
232
228
233
/** @phpstan-impure */
0 commit comments