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);
interface HasDefault
{
public const DEFAULT = 'default';
}
enum Mode implements HasDefault
{
case DEFAULT; // ERROR: Enum case Mode::DEFAULT overrides constant HasDefault::DEFAULT but is missing the #[\Override] attribute.
case Custom;
}
Why is it reported? #
An enum case is a class constant of the enum. When an enum implements an interface that declares a constant with the same name, the enum case overrides that constant. Starting with PHP 8.6, the #[\Override] attribute can be placed on enum cases to make this intent explicit. If the interface constant is later renamed or removed, PHP reports an error instead of silently keeping an unrelated case.
The check is enabled automatically when checkMissingOverrideMethodAttribute or checkMissingOverridePropertyAttribute is enabled and every analysed PHP version supports #[\Override] on class constants (PHP 8.6+, see phpVersion). It can also be turned on or off explicitly with the checkMissingOverrideConstantAttribute parameter.
How to fix it #
Add the #[\Override] attribute to the enum case:
enum Mode implements HasDefault
{
+ #[\Override]
case DEFAULT;
case Custom;
}
🔧 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
To also fix the errors you have ignored, use --fix-ignored. Learn more about --fix »
How to ignore this error #
You can use the identifier enum.caseMissingOverride to ignore this error using a comment:
// @phpstan-ignore enum.caseMissingOverride
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: enum.caseMissingOverride