Error Identifier: enumImplements.trait
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);
trait FooTrait
{
}
enum MyEnum implements FooTrait
{
case A;
case B;
}
Why is it reported? #
An enum’s implements clause must list interfaces, not traits. Traits are used with the use keyword inside the enum body, not in the implements clause. This is a PHP language-level error: the implements keyword is reserved for interfaces.
How to fix it #
If the intent is to use the trait in the enum, move it to a use statement inside the enum body:
<?php declare(strict_types = 1);
trait FooTrait
{
}
-enum MyEnum implements FooTrait
+enum MyEnum
{
+ use FooTrait;
+
case A;
case B;
}
If the intent is to implement an interface, replace the trait reference with the correct interface:
<?php declare(strict_types = 1);
-trait FooTrait
+interface FooInterface
{
}
-enum MyEnum implements FooTrait
+enum MyEnum implements FooInterface
{
case A;
case B;
}
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\ExistingClassesInEnumImplementsRule [1]