Error Identifier: method.parentMethodFinalByPhpDoc
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);
class ParentClass
{
/**
* @final
*/
public function doFoo(): void
{
}
}
class ChildClass extends ParentClass
{
public function doFoo(): void
{
}
}
Why is it reported? #
A method overrides a parent method that is marked as @final in PHPDoc. The @final annotation indicates that the method author intended it to not be overridden, even though it is not enforced at the PHP language level with the final keyword.
How to fix it #
Do not override the @final method. Use composition or a different approach:
<?php declare(strict_types = 1);
class ChildClass extends ParentClass
{
- public function doFoo(): void
+ public function doBar(): void
{
+ $this->doFoo();
+ // additional logic
}
}
How to ignore this error #
You can use the identifier method.parentMethodFinalByPhpDoc to ignore this error using a comment:
// @phpstan-ignore method.parentMethodFinalByPhpDoc
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: method.parentMethodFinalByPhpDoc