Error Identifier: attribute.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);
#[\Attribute]
enum Suit
{
case Hearts;
case Diamonds;
}
Why is it reported? #
PHP attributes can only be applied to classes (not abstract). An enum cannot be used as an #[\Attribute] class because PHP requires attribute classes to be non-abstract, non-enum, non-interface, non-trait classes. Marking an enum with #[\Attribute] has no effect and would cause an error if the enum were ever used as an attribute on another declaration.
How to fix it #
Convert the attribute to a regular class:
<?php declare(strict_types = 1);
-#[\Attribute]
-enum Suit
+#[\Attribute]
+class Suit
{
- case Hearts;
- case Diamonds;
+ public function __construct(
+ public readonly string $value,
+ ) {
+ }
}
How to ignore this error #
You can use the identifier attribute.enum to ignore this error using a comment:
// @phpstan-ignore attribute.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: attribute.enum
Rules that report this error #
- PHPStan\Rules\Classes\NonClassAttributeClassRule [1]