website/src/developing-extensions/ignore-error-extensions.md
Sometimes, you might want to ignore errors based on the Error, Node and Scope.
For example:
missingType.iterableValue on controller methods (actions): when the method is public and it has #[Route] attribute or the class has #[AsController] attribute.should return int but returns int|null on getId for entities when the class has the #[Entity] attribute.never returns null so it can be removed from the return type when the method has #[GraphQL\Field] attribute.missingCheckedExceptionInThrows partially, only for specific classes.You can create an extension that implements IgnoreErrorExtension.
use PhpParser\Node;
use PHPStan\Analyser\Error;
use PHPStan\Analyser\IgnoreErrorExtension;
use PHPStan\Analyser\Scope;
// 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;
}
if (!$scope->isInClass()) {
return false;
}
$classReflection = $scope->getClassReflection();
$methodReflection = $scope->getFunction();
if ($methodReflection === null) {
return false;
}
if (!str_ends_with($classReflection->getName(), 'Controller')) {
return false;
}
if (!str_ends_with($methodReflection->getName(), 'Action')) {
return false;
}
if (!$node->getMethodReflection()->isPublic()) {
return false;
}
return true;
}
}
The implementation needs to be registered in your configuration file:
services:
-
class: MyApp\PHPStan\ControllerActionReturnTypeIgnoreExtension
tags:
- phpstan.ignoreErrorExtension