Skip to content

Commit 73bf9d7

Browse files
committed
Fix: Traverse all nodes in Files\NoPhpstanIgnoreRule
1 parent 05cc2bd commit 73bf9d7

File tree

5 files changed

+112
-2
lines changed

5 files changed

+112
-2
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
88

99
For a full diff see [`2.13.0...main`][2.13.0...main].
1010

11+
### Fixed
12+
13+
- Adjusted `Files\NoPhpstanIgnoreRule` to traverse into all nodes ([#1011]), by [@localheinz]
14+
1115
## [`2.13.0`][2.13.0]
1216

1317
For a full diff see [`2.12.0...2.13.0`][2.12.0...2.13.0].
@@ -752,6 +756,7 @@ For a full diff see [`362c7ea...0.1.0`][362c7ea...0.1.0].
752756
[#977]: https://github.com/ergebnis/phpstan-rules/pull/977
753757
[#1009]: https://github.com/ergebnis/phpstan-rules/pull/1009
754758
[#1010]: https://github.com/ergebnis/phpstan-rules/pull/1010
759+
[#1011]: https://github.com/ergebnis/phpstan-rules/pull/1011
755760

756761
[@cosmastech]: https://github.com/cosmastech
757762
[@enumag]: https://github.com/enumag

composer-require-checker.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"PhpParser\\Node\\Stmt\\Nop",
4040
"PhpParser\\Node\\Stmt\\Switch_",
4141
"PhpParser\\Node\\UnionType",
42+
"PhpParser\\NodeFinder",
4243
"PHPStan\\Analyser\\Scope",
4344
"PHPStan\\Node\\FileNode",
4445
"PHPStan\\PhpDoc\\ResolvedPhpDocBlock",

src/Files/NoPhpstanIgnoreRule.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use Ergebnis\PHPStan\Rules\ErrorIdentifier;
1717
use PhpParser\Node;
18+
use PhpParser\NodeFinder;
1819
use PHPStan\Analyser;
1920
use PHPStan\Node\FileNode;
2021
use PHPStan\Rules;
@@ -33,10 +34,16 @@ public function processNode(
3334
Node $node,
3435
Analyser\Scope $scope
3536
): array {
37+
$nodeFinder = new NodeFinder();
38+
39+
$foundNodes = $nodeFinder->find($node->getNodes(), static function (Node $node): bool {
40+
return true;
41+
});
42+
3643
$errors = [];
3744

38-
foreach ($node->getNodes() as $nodeInFile) {
39-
foreach ($nodeInFile->getComments() as $comment) {
45+
foreach ($foundNodes as $foundNode) {
46+
foreach ($foundNode->getComments() as $comment) {
4047
foreach (\explode("\n", $comment->getText()) as $index => $line) {
4148
if (0 === \preg_match('/@phpstan-ignore(-line|-next-line)?(?=\s|$|,)/', $line, $matches)) {
4249
continue;
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ergebnis\PHPStan\Rules\Test\Fixture\Files\NoPhpstanIgnoreRule\Failure;
6+
7+
class ClassWithPhpstanIgnoreComments
8+
{
9+
public function foo(): void
10+
{
11+
// @phpstan-ignore variable.undefined
12+
echo $undefined;
13+
14+
/* @phpstan-ignore variable.undefined */
15+
echo $undefined;
16+
17+
/** @phpstan-ignore variable.undefined */
18+
echo $undefined;
19+
20+
echo $undefined; // @phpstan-ignore-line
21+
22+
echo $undefined; /* @phpstan-ignore-line */
23+
24+
// @phpstan-ignore-next-line
25+
echo $undefined;
26+
27+
/* @phpstan-ignore-next-line */
28+
echo $undefined;
29+
30+
/** @phpstan-ignore-next-line */
31+
echo $undefined;
32+
}
33+
}

test/Integration/Files/NoPhpStanIgnoreRuleTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,42 @@ public function testNoIgnoreRule(): void
5353
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore", fix the error or use the baseline instead.',
5454
11,
5555
],
56+
[
57+
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore", fix the error or use the baseline instead.',
58+
11,
59+
],
60+
[
61+
'No error with identifier variable.undefined is reported on line 12.',
62+
12,
63+
],
5664
[
5765
'No error with identifier variable.undefined is reported on line 12.',
5866
12,
5967
],
6068
[
6169
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore", fix the error or use the baseline instead.',
70+
14,
71+
],
72+
[
73+
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore", fix the error or use the baseline instead.',
74+
15,
75+
],
76+
[
77+
'No error with identifier variable.undefined is reported on line 15.',
6278
15,
6379
],
80+
[
81+
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore", fix the error or use the baseline instead.',
82+
17,
83+
],
6484
[
6585
'No error with identifier variable.undefined is reported on line 17.',
6686
17,
6787
],
88+
[
89+
'No error with identifier variable.undefined is reported on line 18.',
90+
18,
91+
],
6892
[
6993
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-line", fix the error or use the baseline instead.',
7094
19,
@@ -73,6 +97,14 @@ public function testNoIgnoreRule(): void
7397
'No error to ignore is reported on line 19.',
7498
19,
7599
],
100+
[
101+
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-line", fix the error or use the baseline instead.',
102+
20,
103+
],
104+
[
105+
'No error to ignore is reported on line 20.',
106+
20,
107+
],
76108
[
77109
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-line", fix the error or use the baseline instead.',
78110
21,
@@ -81,6 +113,18 @@ public function testNoIgnoreRule(): void
81113
'No error to ignore is reported on line 21.',
82114
21,
83115
],
116+
[
117+
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-line", fix the error or use the baseline instead.',
118+
22,
119+
],
120+
[
121+
'No error to ignore is reported on line 22.',
122+
22,
123+
],
124+
[
125+
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-next-line", fix the error or use the baseline instead.',
126+
24,
127+
],
84128
[
85129
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-line", fix the error or use the baseline instead.',
86130
25,
@@ -89,6 +133,14 @@ public function testNoIgnoreRule(): void
89133
'No error to ignore is reported on line 25.',
90134
25,
91135
],
136+
[
137+
'No error to ignore is reported on line 25.',
138+
25,
139+
],
140+
[
141+
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-next-line", fix the error or use the baseline instead.',
142+
27,
143+
],
92144
[
93145
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-next-line", fix the error or use the baseline instead.',
94146
27,
@@ -97,10 +149,22 @@ public function testNoIgnoreRule(): void
97149
'No error to ignore is reported on line 28.',
98150
28,
99151
],
152+
[
153+
'No error to ignore is reported on line 28.',
154+
28,
155+
],
100156
[
101157
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-next-line", fix the error or use the baseline instead.',
102158
30,
103159
],
160+
[
161+
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-next-line", fix the error or use the baseline instead.',
162+
30,
163+
],
164+
[
165+
'No error to ignore is reported on line 31.',
166+
31,
167+
],
104168
[
105169
'No error to ignore is reported on line 31.',
106170
31,

0 commit comments

Comments
 (0)