Error Identifier: constant.attributesNotSupported
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);
// Analysed with PHP version set to < 8.5
#[\Attribute(\Attribute::TARGET_CONSTANT)]
class MyAttr {}
#[MyAttr]
const FOO = 1; // error
Why is it reported? #
Attributes on global constants are a feature introduced in PHP 8.5. When the configured PHP version for analysis is lower than 8.5, placing attributes on global constants is not supported and will result in a runtime error.
How to fix it #
If you need attributes on global constants, set the phpVersion parameter in your PHPStan configuration to 8.5 or later:
parameters:
phpVersion: 80500
Alternatively, if you cannot upgrade, avoid using attributes on global constants and consider using class constants instead, which support attributes since PHP 8.0:
<?php declare(strict_types = 1);
-#[MyAttr]
-const FOO = 1;
+class Config
+{
+ #[MyAttr]
+ const FOO = 1;
+}
Non-ignorable error #
This error cannot be ignored using @phpstan-ignore or the ignoreErrors configuration. Non-ignorable errors indicate code that would cause a crash or a fatal error at runtime, or a fundamental problem in the analysed code that must be addressed.
Rules that report this error #
- PHPStan\Rules\Constants\ConstantAttributesRule [1]