AttributeTargetMismatch
error native on by default PHP ≥ 8.0Group: Probable bugs
Use the attribute on a permitted target.
Example
Finding:
<?php
#[Attribute(Attribute::TARGET_METHOD)] class Marker {} #[Marker] class Product {}Reported:
- line 2: Use the attribute on a permitted target.
Detection
- Resolve the attribute class and its builtin Attribute annotation; constant-fold the target bitmask (default TARGET_ALL). Report an application on a class, function, method, property, class constant or parameter whose corresponding bit is absent. This predicts failure of ReflectionAttribute::newInstance, not immediate parse failure. PHP 8.5 property hooks are method targets.
- Emit one finding per violating operation. Flow facts require a reachable path with no intervening mutation, escaped alias or unknown call involving the tracked value. When the proof is inconclusive, nothing is reported.
Exceptions (no report)
A constructor-promoted parameter is also a property declaration. Accept attributes permitting either parameter or property targets at that combined declaration. A parameter is promoted when it carries any promoting modifier: a visibility keyword (public, protected, private), readonly (PHP 8.1), an asymmetric set-visibility such as private(set) (PHP 8.4), or final (PHP 8.5). The modifier does not have to be a visibility keyword. A parameter carrying none of these is a plain parameter and needs the parameter target.
- Skip unresolved attribute declarations and unknown bitmasks. Attribute::IS_REPEATABLE is not a target bit.
- Exclude shadowed builtin symbols, unresolved required types, incomplete syntax and unsupported PHP versions.
Fix
No automatic fix. Choosing a repair requires runtime information, error-handling policy or a semantic decision.
PHP versions
Minimum PHP 8.0.
Configure
In custos.json:
{
"rules": {
"AttributeTargetMismatch": {
"enabled": false
}
}
}Suppress
Before the statement or declaration (or the first statement of the file):
// @custos-ignore AttributeTargetMismatch