@@ -22,6 +22,9 @@ class TokenIterator
2222 /** @var int[] */
2323 private $ savePoints = [];
2424
25+ /** @var list<int> */
26+ private $ skippedTokenTypes = [Lexer::TOKEN_HORIZONTAL_WS ];
27+
2528 /**
2629 * @param list<array{string, int, int}> $tokens
2730 */
@@ -30,11 +33,7 @@ public function __construct(array $tokens, int $index = 0)
3033 $ this ->tokens = $ tokens ;
3134 $ this ->index = $ index ;
3235
33- if ($ this ->tokens [$ this ->index ][Lexer::TYPE_OFFSET ] !== Lexer::TOKEN_HORIZONTAL_WS ) {
34- return ;
35- }
36-
37- $ this ->index ++;
36+ $ this ->skipIrrelevantTokens ();
3837 }
3938
4039
@@ -131,12 +130,7 @@ public function consumeTokenType(int $tokenType): void
131130 }
132131
133132 $ 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 ();
140134 }
141135
142136
@@ -150,12 +144,7 @@ public function consumeTokenValue(int $tokenType, string $tokenValue): void
150144 }
151145
152146 $ 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 ();
159148 }
160149
161150
@@ -167,10 +156,7 @@ public function tryConsumeTokenValue(string $tokenValue): bool
167156 }
168157
169158 $ this ->index ++;
170-
171- if ($ this ->tokens [$ this ->index ][Lexer::TYPE_OFFSET ] === Lexer::TOKEN_HORIZONTAL_WS ) {
172- $ this ->index ++;
173- }
159+ $ this ->skipIrrelevantTokens ();
174160
175161 return true ;
176162 }
@@ -184,10 +170,7 @@ public function tryConsumeTokenType(int $tokenType): bool
184170 }
185171
186172 $ this ->index ++;
187-
188- if ($ this ->tokens [$ this ->index ][Lexer::TYPE_OFFSET ] === Lexer::TOKEN_HORIZONTAL_WS ) {
189- $ this ->index ++;
190- }
173+ $ this ->skipIrrelevantTokens ();
191174
192175 return true ;
193176 }
@@ -217,12 +200,34 @@ public function joinUntil(int ...$tokenType): string
217200 public function next (): void
218201 {
219202 $ this ->index ++;
203+ $ this ->skipIrrelevantTokens ();
204+ }
220205
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 ])) {
222210 return ;
223211 }
224212
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 ];
226231 }
227232
228233 /** @phpstan-impure */
0 commit comments