Error Identifier: method.finalPrivate
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 Foo
{
final private function doSomething(): void
{
}
}
Why is it reported? #
A private method is marked as final, which is unnecessary. The final keyword prevents a method from being overridden in child classes, but private methods are not visible to child classes and therefore can never be overridden. Marking a private method as final has no practical effect.
As of PHP 8.0, PHP itself emits a deprecation notice for this combination.
How to fix it #
Remove the final keyword from the private method.
<?php declare(strict_types = 1);
class Foo
{
- final private function doSomething(): void
+ private function doSomething(): void
{
}
}
How to ignore this error #
You can use the identifier method.finalPrivate to ignore this error using a comment:
// @phpstan-ignore method.finalPrivate
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.finalPrivate
Rules that report this error #
- PHPStan\Rules\Methods\FinalPrivateMethodRule [1]