Error Identifier: classImplements.enum
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 Suit
{
case Hearts;
case Diamonds;
}
class Card implements Suit // error
{
}
Why is it reported? #
In PHP, a class can only implement interfaces. Enums are not interfaces and cannot be used in an implements clause. This is a fundamental language constraint – the implements keyword is reserved exclusively for interface types.
How to fix it #
Extract an interface that the enum implements, then have your class implement that interface instead:
<?php declare(strict_types = 1);
+interface HasLabel
+{
+ public function label(): string;
+}
+
-enum Suit
+enum Suit implements HasLabel
{
case Hearts;
case Diamonds;
+
+ public function label(): string
+ {
+ return $this->name;
+ }
}
-class Card implements Suit
+class Card implements HasLabel
{
+ public function label(): string
+ {
+ return 'Joker';
+ }
}
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\ExistingClassesInClassImplementsRule [1]