Menu

← Back to attribute.*

Error Identifier: attribute.trait

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]
trait MyAttribute
{
	public function getValue(): string
	{
		return 'value';
	}
}

Why is it reported? #

PHP requires attribute classes to be non-abstract classes. A trait cannot be used as an attribute class because PHP needs to instantiate the attribute when it is applied, and traits cannot be instantiated. Using #[\Attribute] on a trait is invalid and will cause a runtime error.

In the example above, MyAttribute is declared as a trait with the #[\Attribute] attribute, which is not allowed.

How to fix it #

Change the trait to a class:

 <?php declare(strict_types = 1);
 
 #[\Attribute]
-trait MyAttribute
+class MyAttribute
 {
-	public function getValue(): string
+	public function __construct(
+		public readonly string $value = 'value',
+	)
 	{
-		return 'value';
 	}
 }

How to ignore this error #

You can use the identifier attribute.trait to ignore this error using a comment:

// @phpstan-ignore attribute.trait
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.trait

Rules that report this error #

  • PHPStan\Rules\Classes\NonClassAttributeClassRule [1]
  • PHPStan\Rules\Classes\TraitAttributeClassRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.