Error Identifier: attribute.interface
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]
interface MyAttribute
{
public function getValue(): string;
}
Why is it reported? #
PHP requires attribute classes to be non-abstract classes. An interface cannot be used as an attribute class because PHP needs to instantiate the attribute when it is applied, and interfaces cannot be instantiated. Using #[\Attribute] on an interface is invalid and will cause a runtime error.
In the example above, MyAttribute is declared as an interface with the #[\Attribute] attribute, which is not allowed.
How to fix it #
Change the interface to a class:
<?php declare(strict_types = 1);
#[\Attribute]
-interface MyAttribute
+class MyAttribute
{
- public function getValue(): string;
+ public function __construct(
+ public readonly string $value,
+ ) {
+ }
}
How to ignore this error #
You can use the identifier attribute.interface to ignore this error using a comment:
// @phpstan-ignore attribute.interface
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.interface
Rules that report this error #
- PHPStan\Rules\Classes\NonClassAttributeClassRule [1]