Error Identifier: method.missingOverride
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
{
public function doFoo(): void
{
}
}
class Bar extends Foo
{
public function doFoo(): void // ERROR: Method Bar::doFoo() overrides method Foo::doFoo() but is missing the #[\Override] attribute.
{
}
}
Why is it reported? #
A method overrides a parent method but does not have the #[\Override] attribute. The #[\Override] attribute (introduced in PHP 8.3) explicitly marks methods that are intended to override a parent method. When required, it ensures that overriding methods are clearly documented and helps catch accidental breakage if the parent method is renamed or removed.
This check is controlled by the checkMissingOverrideMethodAttribute configuration option.
How to fix it #
Add the #[\Override] attribute to the method:
<?php declare(strict_types = 1);
class Bar extends Foo
{
+ #[\Override]
public function doFoo(): void
{
}
}
How to ignore this error #
You can use the identifier method.missingOverride to ignore this error using a comment:
// @phpstan-ignore method.missingOverride
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.missingOverride
Rules that report this error #
- PHPStan\Rules\Methods\OverridingMethodRule [1]