Error Identifier: requireImplements.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 SomeEnum
{
case Foo;
case Bar;
}
/**
* @phpstan-require-implements SomeEnum
*/
trait MyTrait {}
Why is it reported? #
The @phpstan-require-implements PHPDoc tag can only reference interfaces. Enums are not interfaces and cannot be “implemented” by a class. When a trait uses @phpstan-require-implements, it declares that any class using the trait must implement the specified interface. An enum cannot serve this role because PHP does not allow classes to implement enums.
How to fix it #
Replace the enum reference with an interface that the enum implements:
<?php declare(strict_types = 1);
+interface SomeEnumInterface {}
+
+enum SomeEnum: string implements SomeEnumInterface
+{
+ case Foo = 'foo';
+ case Bar = 'bar';
+}
+
/**
- * @phpstan-require-implements SomeEnum
+ * @phpstan-require-implements SomeEnumInterface
*/
trait MyTrait {}
Or remove the @phpstan-require-implements tag if the constraint is not needed:
<?php declare(strict_types = 1);
-/**
- * @phpstan-require-implements SomeEnum
- */
trait MyTrait {}
How to ignore this error #
You can use the identifier requireImplements.enum to ignore this error using a comment:
// @phpstan-ignore requireImplements.enum
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: requireImplements.enum
Rules that report this error #
- PHPStan\Rules\PhpDoc\RequireImplementsDefinitionTraitRule [1]