Skip to content

Commit 49498ce

Browse files
authored
IgnoreLexer: drop support for multiline identifiers
1 parent 39ce042 commit 49498ce

File tree

2 files changed

+62
-17
lines changed

2 files changed

+62
-17
lines changed

src/Parser/RichParser.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@
1414
use PHPStan\ShouldNotHappenException;
1515
use function array_filter;
1616
use function array_pop;
17-
use function array_values;
1817
use function count;
1918
use function implode;
20-
use function in_array;
2119
use function is_string;
2220
use function preg_match_all;
2321
use function sprintf;
@@ -278,8 +276,19 @@ private function getLinesToIgnoreForTokenByIgnoreComment(
278276
private function parseIdentifiers(string $text, int $ignorePos): array
279277
{
280278
$text = substr($text, $ignorePos + strlen('@phpstan-ignore'));
281-
$tokens = $this->ignoreLexer->tokenize($text);
282-
$tokens = array_values(array_filter($tokens, static fn (array $token) => !in_array($token[IgnoreLexer::TYPE_OFFSET], [IgnoreLexer::TOKEN_WHITESPACE, IgnoreLexer::TOKEN_EOL], true)));
279+
$originalTokens = $this->ignoreLexer->tokenize($text);
280+
$tokens = [];
281+
282+
foreach ($originalTokens as $originalToken) {
283+
if ($originalToken[IgnoreLexer::TYPE_OFFSET] === IgnoreLexer::TOKEN_WHITESPACE) {
284+
continue;
285+
}
286+
if ($originalToken[IgnoreLexer::TYPE_OFFSET] === IgnoreLexer::TOKEN_EOL) {
287+
break;
288+
}
289+
$tokens[] = $originalToken;
290+
}
291+
283292
$c = count($tokens);
284293

285294
$identifiers = [];
@@ -321,6 +330,10 @@ private function parseIdentifiers(string $text, int $ignorePos): array
321330
$parenthesisStack[] = $tokenLine;
322331
}
323332

333+
if (isset($tokens[$c - 1]) && $tokens[$c - 1][IgnoreLexer::TYPE_OFFSET] === IgnoreLexer::TOKEN_COMMA) {
334+
throw new IgnoreParseException('Unexpected trailing comma (,)', $tokens[$c - 1][IgnoreLexer::LINE_OFFSET]);
335+
}
336+
324337
if (count($parenthesisStack) > 0) {
325338
throw new IgnoreParseException('Unclosed opening parenthesis "(" without closing parenthesis ")"', $parenthesisStack[count($parenthesisStack) - 1]);
326339
}

tests/PHPStan/Parser/RichParserTest.php

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -221,18 +221,6 @@ public function dataLinesToIgnore(): iterable
221221
3 => ['test'],
222222
],
223223
];
224-
225-
yield [
226-
'<?php' . PHP_EOL .
227-
PHP_EOL .
228-
'/**' . PHP_EOL .
229-
' * @phpstan-ignore return.ref,' . PHP_EOL .
230-
' * return.non,' . PHP_EOL .
231-
' */',
232-
[
233-
6 => ['return.ref', 'return.non'],
234-
],
235-
];
236224
}
237225

238226
/**
@@ -245,8 +233,8 @@ public function testLinesToIgnore(string $code, array $expectedLines): void
245233
$parser = self::getContainer()->getService('currentPhpVersionRichParser');
246234
$ast = $parser->parseString($code);
247235
$lines = $ast[0]->getAttribute('linesToIgnore');
248-
$this->assertSame($expectedLines, $lines);
249236
$this->assertNull($ast[0]->getAttribute('linesToIgnoreParseErrors'));
237+
$this->assertSame($expectedLines, $lines);
250238
}
251239

252240
public function dataLinesToIgnoreParseErrors(): iterable
@@ -263,6 +251,50 @@ public function dataLinesToIgnoreParseErrors(): iterable
263251
],
264252
];
265253

254+
yield [
255+
'<?php' . PHP_EOL .
256+
'/**' . PHP_EOL .
257+
' * @phpstan-ignore return.ref,' . PHP_EOL .
258+
' */',
259+
[
260+
3 => ['Unexpected trailing comma (,)'],
261+
],
262+
];
263+
264+
yield [
265+
'<?php' . PHP_EOL .
266+
'/**' . PHP_EOL .
267+
' * @phpstan-ignore' . PHP_EOL .
268+
' */',
269+
[
270+
3 => ['Missing identifier'],
271+
],
272+
];
273+
274+
yield [
275+
'<?php' . PHP_EOL .
276+
'test(); // @phpstan-ignore return.ref,' . PHP_EOL,
277+
[
278+
2 => ['Unexpected trailing comma (,)'],
279+
],
280+
];
281+
282+
yield [
283+
'<?php' . PHP_EOL .
284+
'test(); // @phpstan-ignore test (comment),' . PHP_EOL,
285+
[
286+
2 => ['Unexpected trailing comma (,)'],
287+
],
288+
];
289+
290+
yield [
291+
'<?php' . PHP_EOL .
292+
'test(); // @phpstan-ignore ,' . PHP_EOL,
293+
[
294+
2 => ['First token is not an identifier'],
295+
],
296+
];
297+
266298
yield [
267299
'<?php' . PHP_EOL .
268300
'test(); // @phpstan-ignore return.ref, return.non )foo',

0 commit comments

Comments
 (0)