Skip to content

Commit 381c137

Browse files
committed
RichParser - fix @phpstan-ignore with trait in the same file
1 parent c55aa05 commit 381c137

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

src/Parser/RichParser.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,12 @@ public function parseString(string $sourceCode): array
102102
}
103103

104104
foreach ($traitCollectingVisitor->traits as $trait) {
105-
$trait->setAttribute('linesToIgnore', array_filter($linesToIgnore, static fn (int $line): bool => $line >= $trait->getStartLine() && $line <= $trait->getEndLine(), ARRAY_FILTER_USE_KEY));
105+
$preexisting = $trait->getAttribute('linesToIgnore', []);
106+
$filteredLinesToIgnore = array_filter($linesToIgnore, static fn (int $line): bool => $line >= $trait->getStartLine() && $line <= $trait->getEndLine(), ARRAY_FILTER_USE_KEY);
107+
foreach ($preexisting as $line => $ignores) {
108+
$filteredLinesToIgnore[$line] = $ignores;
109+
}
110+
$trait->setAttribute('linesToIgnore', $filteredLinesToIgnore);
106111
}
107112

108113
return $nodes;

tests/PHPStan/Parser/RichParserTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,45 @@ public function dataLinesToIgnore(): iterable
261261
2 => ['identifier'],
262262
],
263263
];
264+
265+
yield [
266+
'<?php' . PHP_EOL .
267+
PHP_EOL .
268+
'class MyClass {' . PHP_EOL .
269+
' use MyTrait;' . PHP_EOL .
270+
PHP_EOL .
271+
' public mixed $myProperty;' . PHP_EOL .
272+
PHP_EOL .
273+
' function myFunction(): void {' . PHP_EOL .
274+
' // @phpstan-ignore variable.undefined' . PHP_EOL .
275+
' $this->myProperty = $b;' . PHP_EOL .
276+
' }' . PHP_EOL .
277+
'}',
278+
[
279+
10 => ['variable.undefined'],
280+
],
281+
];
282+
283+
yield [
284+
'<?php' . PHP_EOL .
285+
PHP_EOL .
286+
'trait MyTrait {' . PHP_EOL .
287+
'}' . PHP_EOL .
288+
PHP_EOL .
289+
'class MyClass {' . PHP_EOL .
290+
' use MyTrait;' . PHP_EOL .
291+
PHP_EOL .
292+
' public mixed $myProperty;' . PHP_EOL .
293+
PHP_EOL .
294+
' function myFunction(): void {' . PHP_EOL .
295+
' // @phpstan-ignore variable.undefined' . PHP_EOL .
296+
' $this->myProperty = $b;' . PHP_EOL .
297+
' }' . PHP_EOL .
298+
'}',
299+
[
300+
13 => ['variable.undefined'],
301+
],
302+
];
264303
}
265304

266305
/**

0 commit comments

Comments
 (0)