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 Base
{
public const FOO = 1;
}
class Child extends Base
{
#[\Override]
public const FOO = 2;
#[\Override]
public const BAR = 3; // ERROR: Constant Child::BAR has #[\Override] attribute but does not override any constant.
}
Why is it reported? #
The #[\Override] attribute on a class constant (PHP 8.6+) declares that the constant overrides a constant from a parent class or an implemented interface. If no parent class or interface declares a constant with the same name, the attribute is incorrect and PHP throws a fatal error.
This typically happens when the constant in the parent class was renamed or removed, or when the overriding constant has a typo in its name. Private constants of the parent class cannot be overridden, so they do not count either.
How to fix it #
If the constant is not meant to override anything, remove the attribute:
class Child extends Base
{
#[\Override]
public const FOO = 2;
- #[\Override]
public const BAR = 3;
}
If the constant is supposed to override a parent constant, fix the name so it matches:
class Child extends Base
{
#[\Override]
- public const FOOO = 2;
+ public const FOO = 2;
}
🔧 This error is auto-fixable. PHPStan can fix it for you. Run the analysis with the --fix option to rewrite the affected code:
vendor/bin/phpstan analyse --fix
Non-ignorable error #
This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.