Error Identifier: phpParser.classRenamed
Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
/** @implements Rule<Node> */
class MyRule implements Rule
{
public function getNodeType(): string
{
return \PhpParser\Node\Expr\ArrayItem::class;
}
public function processNode(Node $node, Scope $scope): array
{
return [];
}
}
Why is it reported? #
The code references a PHP-Parser class name that was renamed in PHP-Parser v5. PHPStan uses PHP-Parser v5, where several node classes were moved to new locations. Using the old class names will cause errors because these classes no longer exist under their original names.
The renamed classes include:
PhpParser\Node\Scalar\LNumber->PhpParser\Node\Scalar\Int_PhpParser\Node\Scalar\DNumber->PhpParser\Node\Scalar\Float_PhpParser\Node\Scalar\Encapsed->PhpParser\Node\Scalar\InterpolatedStringPhpParser\Node\Scalar\EncapsedStringPart->PhpParser\Node\InterpolatedStringPartPhpParser\Node\Expr\ArrayItem->PhpParser\Node\ArrayItemPhpParser\Node\Expr\ClosureUse->PhpParser\Node\ClosureUsePhpParser\Node\Stmt\DeclareDeclare->PhpParser\Node\DeclareItemPhpParser\Node\Stmt\PropertyProperty->PhpParser\Node\PropertyItemPhpParser\Node\Stmt\StaticVar->PhpParser\Node\StaticVarPhpParser\Node\Stmt\UseUse->PhpParser\Node\UseItem
How to fix it #
Replace the old class name with the new one from PHP-Parser v5:
/** @implements Rule<Node> */
class MyRule implements Rule
{
public function getNodeType(): string
{
- return \PhpParser\Node\Expr\ArrayItem::class;
+ return \PhpParser\Node\ArrayItem::class;
}
public function processNode(Node $node, Scope $scope): array
{
return [];
}
}
See the PHP-Parser v5 upgrade guide for the full list of renamed classes.
How to ignore this error #
You can use the identifier phpParser.classRenamed to ignore this error using a comment:
// @phpstan-ignore phpParser.classRenamed
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: phpParser.classRenamed
Rules that report this error #
- PHPStan\Rules\Api\OldPhpParser4ClassRule [1]