Error Identifier: method.override
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
{
#[\Override]
public function doSomething(): void
{
}
}
Why is it reported? #
A method has the #[\Override] attribute, but it does not actually override any method from a parent class or implemented interface. The #[\Override] attribute (introduced in PHP 8.3) is meant to signal that a method is intentionally overriding a parent method. If no parent method exists, the attribute is incorrect and the error is reported to help catch typos in method names or incorrect class hierarchies.
How to fix it #
Remove the #[\Override] attribute if the method is not intended to override a parent method, or fix the class hierarchy so that the parent method exists.
<?php declare(strict_types = 1);
class Foo
{
- #[\Override]
public function doSomething(): void
{
}
}
Or ensure the class extends the correct parent:
<?php declare(strict_types = 1);
-class Foo
+class Foo extends Bar
{
#[\Override]
public function doSomething(): void
{
}
}
How to ignore this error #
You can use the identifier method.override to ignore this error using a comment:
// @phpstan-ignore method.override
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.override
Rules that report this error #
- PHPStan\Rules\Methods\OverridingMethodRule [1]