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 NativeTyped
{
public const int LIMIT = 1;
}
enum Foo implements NativeTyped
{
case LIMIT; // ERROR: Enum case Foo::LIMIT is not compatible with native type int of constant NativeTyped::LIMIT.
}
Why is it reported? #
An enum case is a class constant of the enum. When the enum implements an interface that declares a constant with the same name, the case overrides that constant. Since PHP 8.3, class constants can have native types, and an overriding constant must declare a compatible native type. An enum case cannot declare a type at all, so PHP rejects it when it overrides a natively typed constant, even when the type would accept the case value, like mixed or UnitEnum.
How to fix it #
Rename the enum case so it no longer overrides the typed constant:
enum Foo implements NativeTyped
{
- case LIMIT;
+ case Limit;
}
If the interface constant is meant to be overridden by enum cases, remove its native type. A PHPDoc type can still describe the expected value:
interface NativeTyped
{
- public const int LIMIT = 1;
+ /** @var int|\UnitEnum */
+ public const LIMIT = 1;
}
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.
Rules that report this error #
- PHPStan\Rules\EnumCases\OverridingEnumCaseRule [1]