Skip to content

Commit

Permalink
IgnoreLexer: drop support for multiline identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
janedbal committed May 10, 2024
1 parent 39ce042 commit 49498ce
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 17 deletions.
21 changes: 17 additions & 4 deletions src/Parser/RichParser.php
Expand Up @@ -14,10 +14,8 @@
use PHPStan\ShouldNotHappenException;
use function array_filter;
use function array_pop;
use function array_values;
use function count;
use function implode;
use function in_array;
use function is_string;
use function preg_match_all;
use function sprintf;
Expand Down Expand Up @@ -278,8 +276,19 @@ private function getLinesToIgnoreForTokenByIgnoreComment(
private function parseIdentifiers(string $text, int $ignorePos): array
{
$text = substr($text, $ignorePos + strlen('@phpstan-ignore'));
$tokens = $this->ignoreLexer->tokenize($text);
$tokens = array_values(array_filter($tokens, static fn (array $token) => !in_array($token[IgnoreLexer::TYPE_OFFSET], [IgnoreLexer::TOKEN_WHITESPACE, IgnoreLexer::TOKEN_EOL], true)));
$originalTokens = $this->ignoreLexer->tokenize($text);
$tokens = [];

foreach ($originalTokens as $originalToken) {
if ($originalToken[IgnoreLexer::TYPE_OFFSET] === IgnoreLexer::TOKEN_WHITESPACE) {
continue;
}
if ($originalToken[IgnoreLexer::TYPE_OFFSET] === IgnoreLexer::TOKEN_EOL) {
break;
}
$tokens[] = $originalToken;
}

$c = count($tokens);

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

if (isset($tokens[$c - 1]) && $tokens[$c - 1][IgnoreLexer::TYPE_OFFSET] === IgnoreLexer::TOKEN_COMMA) {
throw new IgnoreParseException('Unexpected trailing comma (,)', $tokens[$c - 1][IgnoreLexer::LINE_OFFSET]);
}

if (count($parenthesisStack) > 0) {
throw new IgnoreParseException('Unclosed opening parenthesis "(" without closing parenthesis ")"', $parenthesisStack[count($parenthesisStack) - 1]);
}
Expand Down
58 changes: 45 additions & 13 deletions tests/PHPStan/Parser/RichParserTest.php
Expand Up @@ -221,18 +221,6 @@ public function dataLinesToIgnore(): iterable
3 => ['test'],
],
];

yield [
'<?php' . PHP_EOL .
PHP_EOL .
'/**' . PHP_EOL .
' * @phpstan-ignore return.ref,' . PHP_EOL .
' * return.non,' . PHP_EOL .
' */',
[
6 => ['return.ref', 'return.non'],
],
];
}

/**
Expand All @@ -245,8 +233,8 @@ public function testLinesToIgnore(string $code, array $expectedLines): void
$parser = self::getContainer()->getService('currentPhpVersionRichParser');
$ast = $parser->parseString($code);
$lines = $ast[0]->getAttribute('linesToIgnore');
$this->assertSame($expectedLines, $lines);
$this->assertNull($ast[0]->getAttribute('linesToIgnoreParseErrors'));
$this->assertSame($expectedLines, $lines);
}

public function dataLinesToIgnoreParseErrors(): iterable
Expand All @@ -263,6 +251,50 @@ public function dataLinesToIgnoreParseErrors(): iterable
],
];

yield [
'<?php' . PHP_EOL .
'/**' . PHP_EOL .
' * @phpstan-ignore return.ref,' . PHP_EOL .
' */',
[
3 => ['Unexpected trailing comma (,)'],
],
];

yield [
'<?php' . PHP_EOL .
'/**' . PHP_EOL .
' * @phpstan-ignore' . PHP_EOL .
' */',
[
3 => ['Missing identifier'],
],
];

yield [
'<?php' . PHP_EOL .
'test(); // @phpstan-ignore return.ref,' . PHP_EOL,
[
2 => ['Unexpected trailing comma (,)'],
],
];

yield [
'<?php' . PHP_EOL .
'test(); // @phpstan-ignore test (comment),' . PHP_EOL,
[
2 => ['Unexpected trailing comma (,)'],
],
];

yield [
'<?php' . PHP_EOL .
'test(); // @phpstan-ignore ,' . PHP_EOL,
[
2 => ['First token is not an identifier'],
],
];

yield [
'<?php' . PHP_EOL .
'test(); // @phpstan-ignore return.ref, return.non )foo',
Expand Down

0 comments on commit 49498ce

Please sign in to comment.