Menu

← Back to attribute.*

Error Identifier: attribute.target

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(\Attribute::TARGET_PROPERTY)]
class MyPropertyAttribute
{
}

#[MyPropertyAttribute]
class Foo
{
}

Why is it reported? #

PHP attributes declare which targets they can be applied to via the Attribute::TARGET_* flags. In the example above, MyPropertyAttribute is declared with TARGET_PROPERTY, meaning it can only be placed on class properties. Applying it to a class declaration is invalid and PHP will throw an Error at runtime when the attribute is instantiated via reflection.

How to fix it #

Move the attribute to a valid target:

 <?php declare(strict_types = 1);
 
 #[\Attribute(\Attribute::TARGET_PROPERTY)]
 class MyPropertyAttribute
 {
 }

-#[MyPropertyAttribute]
 class Foo
 {
+	#[MyPropertyAttribute]
+	public string $name;
 }

Or expand the allowed targets of the attribute class:

 <?php declare(strict_types = 1);
 
-#[\Attribute(\Attribute::TARGET_PROPERTY)]
+#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_CLASS)]
 class MyPropertyAttribute
 {
 }

 #[MyPropertyAttribute]
 class Foo
 {
 }

How to ignore this error #

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

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

Rules that report this error #

  • PHPStan\Rules\Classes\ClassAttributesRule [1]
  • PHPStan\Rules\Classes\ClassConstantAttributesRule [1]
  • PHPStan\Rules\Constants\ConstantAttributesRule [1]
  • PHPStan\Rules\EnumCases\EnumCaseAttributesRule [1]
  • PHPStan\Rules\Functions\ArrowFunctionAttributesRule [1]
  • PHPStan\Rules\Functions\ClosureAttributesRule [1]
  • PHPStan\Rules\Functions\ExistingClassesInArrowFunctionTypehintsRule [1] [2]
  • PHPStan\Rules\Functions\ExistingClassesInClosureTypehintsRule [1] [2]
  • PHPStan\Rules\Functions\ExistingClassesInTypehintsRule [1] [2]
  • PHPStan\Rules\Functions\FunctionAttributesRule [1]
  • PHPStan\Rules\Functions\ParamAttributesRule [1]
  • PHPStan\Rules\Methods\ExistingClassesInTypehintsRule [1] [2]
  • PHPStan\Rules\Methods\MethodAttributesRule [1]
  • PHPStan\Rules\Properties\ExistingClassesInPropertyHookTypehintsRule [1] [2]
  • PHPStan\Rules\Properties\PropertyAttributesRule [1]
  • PHPStan\Rules\Properties\PropertyHookAttributesRule [1] [2]
  • PHPStan\Rules\Traits\TraitAttributesRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.