Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

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

### Fixed

- Adjusted `Files\NoPhpstanIgnoreRule` to traverse into all nodes ([#1011]), by [@localheinz]

## [`2.13.0`][2.13.0]

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

[@cosmastech]: https://github.com/cosmastech
[@enumag]: https://github.com/enumag
Expand Down
1 change: 1 addition & 0 deletions composer-require-checker.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"PhpParser\\Node\\Stmt\\Nop",
"PhpParser\\Node\\Stmt\\Switch_",
"PhpParser\\Node\\UnionType",
"PhpParser\\NodeFinder",
"PHPStan\\Analyser\\Scope",
"PHPStan\\Node\\FileNode",
"PHPStan\\PhpDoc\\ResolvedPhpDocBlock",
Expand Down
11 changes: 9 additions & 2 deletions src/Files/NoPhpstanIgnoreRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Ergebnis\PHPStan\Rules\ErrorIdentifier;
use PhpParser\Node;
use PhpParser\NodeFinder;
use PHPStan\Analyser;
use PHPStan\Node\FileNode;
use PHPStan\Rules;
Expand All @@ -33,10 +34,16 @@ public function processNode(
Node $node,
Analyser\Scope $scope
): array {
$nodeFinder = new NodeFinder();

$foundNodes = $nodeFinder->find($node->getNodes(), static function (Node $node): bool {
return true;
});

$errors = [];

foreach ($node->getNodes() as $nodeInFile) {
foreach ($nodeInFile->getComments() as $comment) {
foreach ($foundNodes as $foundNode) {
foreach ($foundNode->getComments() as $comment) {
foreach (\explode("\n", $comment->getText()) as $index => $line) {
if (0 === \preg_match('/@phpstan-ignore(-line|-next-line)?(?=\s|$|,)/', $line, $matches)) {
continue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Ergebnis\PHPStan\Rules\Test\Fixture\Files\NoPhpstanIgnoreRule\Failure;

class ClassWithPhpstanIgnoreComments
{
public function foo(): void
{
// @phpstan-ignore variable.undefined
echo $undefined;

/* @phpstan-ignore variable.undefined */
echo $undefined;

/** @phpstan-ignore variable.undefined */
echo $undefined;

echo $undefined; // @phpstan-ignore-line

echo $undefined; /* @phpstan-ignore-line */

// @phpstan-ignore-next-line
echo $undefined;

/* @phpstan-ignore-next-line */
echo $undefined;

/** @phpstan-ignore-next-line */
echo $undefined;
}
}
64 changes: 64 additions & 0 deletions test/Integration/Files/NoPhpStanIgnoreRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,42 @@ public function testNoIgnoreRule(): void
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore", fix the error or use the baseline instead.',
11,
],
[
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore", fix the error or use the baseline instead.',
11,
],
[
'No error with identifier variable.undefined is reported on line 12.',
12,
],
[
'No error with identifier variable.undefined is reported on line 12.',
12,
],
[
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore", fix the error or use the baseline instead.',
14,
],
[
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore", fix the error or use the baseline instead.',
15,
],
[
'No error with identifier variable.undefined is reported on line 15.',
15,
],
[
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore", fix the error or use the baseline instead.',
17,
],
[
'No error with identifier variable.undefined is reported on line 17.',
17,
],
[
'No error with identifier variable.undefined is reported on line 18.',
18,
],
[
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-line", fix the error or use the baseline instead.',
19,
Expand All @@ -73,6 +97,14 @@ public function testNoIgnoreRule(): void
'No error to ignore is reported on line 19.',
19,
],
[
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-line", fix the error or use the baseline instead.',
20,
],
[
'No error to ignore is reported on line 20.',
20,
],
[
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-line", fix the error or use the baseline instead.',
21,
Expand All @@ -81,6 +113,18 @@ public function testNoIgnoreRule(): void
'No error to ignore is reported on line 21.',
21,
],
[
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-line", fix the error or use the baseline instead.',
22,
],
[
'No error to ignore is reported on line 22.',
22,
],
[
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-next-line", fix the error or use the baseline instead.',
24,
],
[
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-line", fix the error or use the baseline instead.',
25,
Expand All @@ -89,6 +133,14 @@ public function testNoIgnoreRule(): void
'No error to ignore is reported on line 25.',
25,
],
[
'No error to ignore is reported on line 25.',
25,
],
[
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-next-line", fix the error or use the baseline instead.',
27,
],
[
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-next-line", fix the error or use the baseline instead.',
27,
Expand All @@ -97,10 +149,22 @@ public function testNoIgnoreRule(): void
'No error to ignore is reported on line 28.',
28,
],
[
'No error to ignore is reported on line 28.',
28,
],
[
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-next-line", fix the error or use the baseline instead.',
30,
],
[
'Errors reported by phpstan/phpstan should not be ignored via "@phpstan-ignore-next-line", fix the error or use the baseline instead.',
30,
],
[
'No error to ignore is reported on line 31.',
31,
],
[
'No error to ignore is reported on line 31.',
31,
Expand Down