Error Identifier: enum.caseType
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);
enum Status: int
{
case Active = 'active';
case Inactive = 2;
}
Why is it reported? #
A backed enum declares the type of its case values in the enum declaration (int in this example). Every case value must match that type. The value 'active' is a string, which does not match the declared int backing type. PHP will throw a compile-time error for this mismatch.
How to fix it #
Change the case value to match the declared backing type:
enum Status: int
{
- case Active = 'active';
+ case Active = 1;
case Inactive = 2;
}
Alternatively, change the backing type if string values are intended:
-enum Status: int
+enum Status: string
{
case Active = 'active';
- case Inactive = 2;
+ case Inactive = 'inactive';
}
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\Classes\EnumSanityRule [1]