-
Notifications
You must be signed in to change notification settings - Fork 506
Introduce IgnoreErrorExtension #3783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ba67f0b
IgnoreErrorExtension
ruudk f0f284e
Rename ignore to shouldIgnore
ruudk c4d5dc6
Skip non ignorable errors
ruudk 69700cb
Remove $tempCollectorErrors filter
ruudk 0a59f41
Add IgnoreErrorExtensionProvider service
ruudk cfe9f20
Remove filtering for files
ruudk 76fa491
Fix tests
ruudk 43919cb
Fix CS
ruudk cef37c0
Add e2e test that demonstrates IgnoreErrorExtension
ruudk 7f37ecc
Add tests for collectors
ruudk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/vendor | ||
/composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"autoload": { | ||
"psr-4": { | ||
"App\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
parameters: | ||
ignoreErrors: | ||
- | ||
message: '#^This is an error from a rule that uses a collector$#' | ||
identifier: class.name | ||
count: 1 | ||
path: src/ClassCollector.php | ||
|
||
- | ||
message: '#^This is an error from a rule that uses a collector$#' | ||
identifier: class.name | ||
count: 1 | ||
path: src/ClassRule.php | ||
|
||
- | ||
message: '#^This is an error from a rule that uses a collector$#' | ||
identifier: class.name | ||
count: 1 | ||
path: src/ControllerActionReturnTypeIgnoreExtension.php | ||
|
||
- | ||
message: '#^This is an error from a rule that uses a collector$#' | ||
identifier: class.name | ||
count: 1 | ||
path: src/ControllerClassNameIgnoreExtension.php | ||
|
||
- | ||
message: '#^Method App\\HomepageController\:\:contactAction\(\) has parameter \$someUnrelatedError with no type specified\.$#' | ||
identifier: missingType.parameter | ||
count: 1 | ||
path: src/HomepageController.php | ||
|
||
- | ||
message: '#^Method App\\HomepageController\:\:getSomething\(\) return type has no value type specified in iterable type array\.$#' | ||
identifier: missingType.iterableValue | ||
count: 1 | ||
path: src/HomepageController.php | ||
|
||
- | ||
message: '#^Method App\\HomepageController\:\:homeAction\(\) has parameter \$someUnrelatedError with no type specified\.$#' | ||
identifier: missingType.parameter | ||
count: 1 | ||
path: src/HomepageController.php | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
includes: | ||
- phpstan-baseline.neon | ||
|
||
parameters: | ||
level: 9 | ||
paths: | ||
- src | ||
|
||
services: | ||
- | ||
class: App\ClassCollector | ||
tags: | ||
- phpstan.collector | ||
- | ||
class: App\ClassRule | ||
tags: | ||
- phpstan.rules.rule | ||
- | ||
class: App\ControllerActionReturnTypeIgnoreExtension | ||
tags: | ||
- phpstan.ignoreErrorExtension | ||
- | ||
class: App\ControllerClassNameIgnoreExtension | ||
tags: | ||
- phpstan.ignoreErrorExtension |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace App; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Collectors\Collector; | ||
|
||
/** | ||
* @implements Collector<Node\Stmt\Class_, array{string, int}> | ||
*/ | ||
final class ClassCollector implements Collector | ||
{ | ||
public function getNodeType(): string | ||
{ | ||
return Node\Stmt\Class_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope) : ?array | ||
{ | ||
if ($node->name === null) { | ||
return null; | ||
} | ||
|
||
return [$node->name->name, $node->getStartLine()]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App; | ||
|
||
use Override; | ||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\CollectedDataNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** | ||
* @implements Rule<CollectedDataNode> | ||
*/ | ||
final class ClassRule implements Rule | ||
{ | ||
#[Override] | ||
public function getNodeType() : string | ||
{ | ||
return CollectedDataNode::class; | ||
} | ||
|
||
#[Override] | ||
public function processNode(Node $node, Scope $scope) : array | ||
{ | ||
$errors = []; | ||
|
||
foreach ($node->get(ClassCollector::class) as $file => $data) { | ||
foreach ($data as [$className, $line]) { | ||
$errors[] = RuleErrorBuilder::message('This is an error from a rule that uses a collector') | ||
->file($file) | ||
->line($line) | ||
->identifier('class.name') | ||
->build(); | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
e2e/ignore-error-extension/src/ControllerActionReturnTypeIgnoreExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Error; | ||
use PHPStan\Analyser\IgnoreErrorExtension; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InClassMethodNode; | ||
|
||
// This extension will ignore "missingType.iterableValue" errors for public Action methods inside Controller classes. | ||
final class ControllerActionReturnTypeIgnoreExtension implements IgnoreErrorExtension | ||
{ | ||
public function shouldIgnore(Error $error, Node $node, Scope $scope) : bool | ||
{ | ||
if ($error->getIdentifier() !== 'missingType.iterableValue') { | ||
return false; | ||
} | ||
|
||
// @phpstan-ignore phpstanApi.instanceofAssumption | ||
if (! $node instanceof InClassMethodNode) { | ||
return false; | ||
} | ||
|
||
if (! str_ends_with($node->getClassReflection()->getName(), 'Controller')) { | ||
return false; | ||
} | ||
|
||
if (! str_ends_with($node->getMethodReflection()->getName(), 'Action')) { | ||
return false; | ||
} | ||
|
||
if (! $node->getMethodReflection()->isPublic()) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
e2e/ignore-error-extension/src/ControllerClassNameIgnoreExtension.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Error; | ||
use PHPStan\Analyser\IgnoreErrorExtension; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\CollectedDataNode; | ||
|
||
// This extension will ignore "class.name" errors for classes with names ending with "Controller". | ||
// These errors are reported by the ClassRule which triggers on CollectedDataNode coming from ClassCollector. | ||
final class ControllerClassNameIgnoreExtension implements IgnoreErrorExtension | ||
{ | ||
public function shouldIgnore(Error $error, Node $node, Scope $scope) : bool | ||
{ | ||
if ($error->getIdentifier() !== 'class.name') { | ||
return false; | ||
} | ||
|
||
// @phpstan-ignore phpstanApi.instanceofAssumption | ||
if (!$node instanceof CollectedDataNode) { | ||
return false; | ||
} | ||
|
||
if (!str_ends_with($error->getFile(), 'Controller.php')) { | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App; | ||
|
||
final class HomepageController | ||
{ | ||
public function homeAction($someUnrelatedError = false): array | ||
{ | ||
return [ | ||
'title' => 'Homepage', | ||
'something' => $this->getSomething(), | ||
]; | ||
} | ||
|
||
public function contactAction($someUnrelatedError): array | ||
{ | ||
return [ | ||
'title' => 'Contact', | ||
'something' => $this->getSomething(), | ||
]; | ||
} | ||
|
||
private function getSomething(): array | ||
{ | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Analyser; | ||
|
||
use PhpParser\Node; | ||
|
||
/** | ||
* This is the extension interface to implement if you want to ignore errors | ||
* based on the node and scope. | ||
* | ||
* To register it in the configuration file use the `phpstan.ignoreErrorExtension` service tag: | ||
* | ||
* ``` | ||
* services: | ||
* - | ||
* class: App\PHPStan\MyExtension | ||
* tags: | ||
* - phpstan.ignoreErrorExtension | ||
* ``` | ||
* | ||
* Learn more: https://phpstan.org | ||
* | ||
* @api | ||
*/ | ||
interface IgnoreErrorExtension | ||
{ | ||
|
||
public const EXTENSION_TAG = 'phpstan.ignoreErrorExtension'; | ||
|
||
public function shouldIgnore(Error $error, Node $node, Scope $scope): bool; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Analyser; | ||
|
||
use PHPStan\DependencyInjection\Container; | ||
|
||
final class IgnoreErrorExtensionProvider | ||
{ | ||
|
||
public function __construct(private Container $container) | ||
{ | ||
} | ||
|
||
/** | ||
* @return IgnoreErrorExtension[] | ||
*/ | ||
public function getExtensions(): array | ||
{ | ||
return $this->container->getServicesByTag(IgnoreErrorExtension::EXTENSION_TAG); | ||
} | ||
|
||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if I comment this change, all tests are passing. Please add a collector + CollectedDataNode rule with an error to the E2E test, and ignore it with an extension, so we can verify this code does what it should.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! I added the tests. While doing it, I wonder, what the value of this would be. Since the Node points to the CollectedDataNode, that contains all data.
I think in real world scenario's, this is not interesting, but maybe I don't see it yet.