Skip to content

Commit

Permalink
RichParser - fix @phpstan-ignore with trait in the same file
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 10, 2024
1 parent c55aa05 commit 381c137
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Parser/RichParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ public function parseString(string $sourceCode): array
}

foreach ($traitCollectingVisitor->traits as $trait) {
$trait->setAttribute('linesToIgnore', array_filter($linesToIgnore, static fn (int $line): bool => $line >= $trait->getStartLine() && $line <= $trait->getEndLine(), ARRAY_FILTER_USE_KEY));
$preexisting = $trait->getAttribute('linesToIgnore', []);
$filteredLinesToIgnore = array_filter($linesToIgnore, static fn (int $line): bool => $line >= $trait->getStartLine() && $line <= $trait->getEndLine(), ARRAY_FILTER_USE_KEY);
foreach ($preexisting as $line => $ignores) {
$filteredLinesToIgnore[$line] = $ignores;
}
$trait->setAttribute('linesToIgnore', $filteredLinesToIgnore);
}

return $nodes;
Expand Down
39 changes: 39 additions & 0 deletions tests/PHPStan/Parser/RichParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,45 @@ public function dataLinesToIgnore(): iterable
2 => ['identifier'],
],
];

yield [
'<?php' . PHP_EOL .
PHP_EOL .
'class MyClass {' . PHP_EOL .
' use MyTrait;' . PHP_EOL .
PHP_EOL .
' public mixed $myProperty;' . PHP_EOL .
PHP_EOL .
' function myFunction(): void {' . PHP_EOL .
' // @phpstan-ignore variable.undefined' . PHP_EOL .
' $this->myProperty = $b;' . PHP_EOL .
' }' . PHP_EOL .
'}',
[
10 => ['variable.undefined'],
],
];

yield [
'<?php' . PHP_EOL .
PHP_EOL .
'trait MyTrait {' . PHP_EOL .
'}' . PHP_EOL .
PHP_EOL .
'class MyClass {' . PHP_EOL .
' use MyTrait;' . PHP_EOL .
PHP_EOL .
' public mixed $myProperty;' . PHP_EOL .
PHP_EOL .
' function myFunction(): void {' . PHP_EOL .
' // @phpstan-ignore variable.undefined' . PHP_EOL .
' $this->myProperty = $b;' . PHP_EOL .
' }' . PHP_EOL .
'}',
[
13 => ['variable.undefined'],
],
];
}

/**
Expand Down

0 comments on commit 381c137

Please sign in to comment.