Error Identifier: attribute.class
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);
use Attribute;
#[Attribute]
interface MyAttribute
{
}
Why is it reported? #
Only classes can be used as PHP attribute classes. Interfaces, traits, and enums cannot be marked with the #[Attribute] attribute. PHP requires attribute classes to be non-abstract classes that can be instantiated.
In the example above, MyAttribute is an interface, which cannot serve as an attribute class.
How to fix it #
Change the type declaration to a class:
<?php declare(strict_types = 1);
use Attribute;
#[Attribute]
-interface MyAttribute
+class MyAttribute
{
}
How to ignore this error #
You can use the identifier attribute.class to ignore this error using a comment:
// @phpstan-ignore attribute.class
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.class
Rules that report this error #
- PHPStan\Rules\Classes\NonClassAttributeClassRule [1]