rfc:target-aware-attributes

PHP RFC: Target aware attributes

Userland attributes (i.e. attributes not provided by PHP itself or an extension) are created with ReflectionAttribute::newInstance() and have their constructors called with the arguments provided to the attribute. However, they lack addition context as to what they are being applied to. While it is possible to hack around with debug_backtrace to identify the original ReflectionAttribute instance, and thus the type of construct the attribute is applied to (class, class constant, etc.) there is no way to identify which class (or class constant, etc.) the attribute is being applied to.

This RFC proposes the addition of:

  • ReflectionTargetAwareAttribute, an interface that attributes can implement to indicate that they want to be told whan they are targetting
  • ReflectionAttributeTarget, a common interface for the different attribute target reflection classes
  • ReflectionAttribute::getReflectionTarget() allows identifying the target of an attribute represented by a ReflectionAttribute instance

Any attribute that implements ReflectionTargetAwareAttribute will have its setAttributeTarget() method called directly after the constructor is invoked by ReflectionAttribute::newInstance(), allowing attributes to identify the target they are applied to.

For the purposes of this RFC, an “item” is something that can have attributes applied to it, including classes, functions, parameters, constants, etc.

Introduction

Attributes were first introduced in attributes_v2 in PHP 8.0, with the current syntax adopted shorter_attribute_syntax_change. Userland attributes provide a way to add metadata to items in a structured way that can be read by external tools using reflection.

However, userland attributes, when created via ReflectionAttribute::newInstance(), only know about the arguments passed to the attribute when it was applied. Backtracing hacking can be used to identify the ReflectionAttribute object that was used, and from there ReflectionAttribute::getTarget() could be used to identify what type of item the attribute was applied to, but not which specific item.

Various workarounds exist in userland, such as:

  • the Crell/AttributeUtils composer library, which provides interfaces (e.g. FromReflectionClass) that indicate an attribute wants to be told what item (e.g. for FromReflectionClass, what class) the attribute was applied to. This information is provided after the attribute is constructed, and only when the attribute is loaded through the library.
  • adding manual ::tryFrom static constructors to use (e.g. Symfony/console's attributes like Option)

However, these rely on using an external library or creating attributes separately from ReflectionAttribute::newInstance().

This RFC proposes a new interface, ReflectionTargetAwareAttribute, that, when implemented, causes ReflectionAttribute::newInstance() to call the setAttributeTarget() method after the constructor. These would allow attributes to add custom validation or otherwise make use of the information about what item they were applied to:

<?php
#[Attribute]
class Demo implements ReflectionTargetAwareAttribute {
    public function setAttributeTarget(ReflectionAttributeTarget $target): void {
        // will echo the same ReflectionClass that `echo $case;` does below
        // this is a contrived example, real world code would do something useful
        echo $target;
    }
}
 
#[Demo("class")]
class WithDemo {}
 
$case = new ReflectionClass(WithDemo::class);
echo $case;
echo "\n";
$case->getAttributes()[0]->newInstance();
 
?>

Proposal

Add a new interface, ReflectionAttributeTarget, for reflection types that have attributes on them.

Add a new interface, ReflectionTargetAwareAttribute, for attributes to indicate that they want to know what they are targeting.

To make that information also available on the ReflectionAttribute instance, add ReflectionAttribute::getReflectionTarget() to the existing ReflectionAttribute class.

The type of the ReflectionAttributeTarget is highly variable, as there are many types of item to which an attribute may be attached, and there is not a 1:1 correspondence between the Attribute::TARGET_* constant used and the resulting reflection object type. For example, a Attribute::TARGET_CLASS_CONSTANT attribute could be applied to a class constant, unit enum case, or backed enum case, which would result in ReflectionClassConstant, ReflectionEnumUnitCase, or ReflectionEnumBackedCase, respectively.

To avoid a huge union type, and to account for potential future expansions of attribute targets (e.g. attributes-on-constants) or new reflection subclasses, this RFC introduces a new interface, ReflectionAttributeTarget, which represents “things that can have attributes” or “things that an attribute can be applied to”.

Thus, the additions in this RFC look like the following (classes that implement Reflector did so previously, and are just updated to also implement ReflectionAttributeTarget)

<?php
 
interface ReflectionAttributeTarget extends Reflector
{
    public function getAttributes(?string $name = null, int $flags = 0): array {}
}
 
interface ReflectionTargetAwareAttribute
{
    public function setAttributeTarget(ReflectionAttributeTarget $target): void {}
}
 
// ...
 
abstract class ReflectionFunctionAbstract implements Reflector, ReflectionAttributeTarget { /* ... */ }
class ReflectionClass implements Reflector, ReflectionAttributeTarget { /* ... */ }
class ReflectionProperty implements Reflector, ReflectionAttributeTarget { /* ... */ }
class ReflectionClassConstant implements Reflector, ReflectionAttributeTarget { /* ... */ }
class ReflectionParameter implements Reflector, ReflectionAttributeTarget { /* ... */ }
class ReflectionConstant implements Reflector, ReflectionAttributeTarget { /* ... */ }
 
// Already exists, just adding a new method
class ReflectionAttribute implements Reflector
{
    // ...
    public function getReflectionTarget(): ReflectionAttributeTarget {}
}
 
?>

When using the target in setAttributeTarget(), the developer may either assert() the type of the reflection object and/or use an instanceof check, as appropriate, if the logic depends on the type, or to provide information to static analysis tools and IDEs. For example, if an attribute may be placed on a class or method, it could check that like so:

#[Attribute(Attribute::TARGET_CLASS)]
class SingleTargetAttribute implements ReflectionTargetAwareAttribute {
    public function setAttributeTarget(ReflectionAttributeTarget $target): void {
        // SA tools won't know what type $target is,
        // beyond ReflectionAttributeTarget
 
        assert($target instanceof ReflectionClass);
        // Now SA tools know that $target is ReflectionClass, and can
        // offer appropriate code completion and validation.
 
        // More stuff here.
    }
}
 
 
#[Attribute(Attribute::TARGET_CLASS|Attribute::TARGET_METHOD)]
class MultiUseAttribute implements ReflectionTargetAwareAttribute {
    public function setAttributeTarget(ReflectionAttributeTarget $target): void {
        if ($target instanceof ReflectionClass) {
            // Do class-specific logic here.
        } else if ($target instanceof ReflectionMethod) {
            // Do method-specific logic here.
        }
        // Do logic common to all attributes here.
    }
}

Examples

Simple example:

<?php
 
#[Attribute(Attribute::TARGET_CLASS)]
class StableInterface implements ReflectionTargetAwareAttribute {
    public function setAttributeTarget(ReflectionAttributeTarget $target): void {
        // For SA tools:
        assert($target instanceof ReflectionClass);
 
        // Should only be applied to interfaces
        if (!$target->isInterface()) {
            throw new Exception("#[StableInterface] is only for interfaces!");
        }
 
        // Some global function that exists elsewhere
        record_stable_interface($target->getName());
    }
}
 
?>

The above attribute, when instantiated, will confirm that it is being applied to an interface (rather than a class, trait, or enum) and then record the name of the targeted interface. This could then be used by tools to automatically generate a list of interfaces in a library that are considered stable, perhaps with the help of the olvlvl/composer-attribute-collector library.

Returned classes

ReflectionAttribute::getReflectionTarget() is documented on a type level to return ReflectionAttributeTarget, but the actual underlying class will be:

Flag Applied to Return type
Attribute::TARGET_CLASS Class ReflectionClass
Attribute::TARGET_CLASS Trait ReflectionClass
Attribute::TARGET_CLASS Interface ReflectionClass
Attribute::TARGET_CLASS Enum ReflectionEnum
Attribute::TARGET_METHOD Class method ReflectionMethod
Attribute::TARGET_CLASS_CONSTANT Class constant ReflectionClassConstant
Attribute::TARGET_CLASS_CONSTANT Enum case (unit) ReflectionEnumUnitCase
Attribute::TARGET_CLASS_CONSTANT Enum case (backed) ReflectionEnumBackedCase
Attribute::TARGET_PROPERTY Class property ReflectionProperty
Attribute::TARGET_FUNCTION Global function ReflectionFunction
Attribute::TARGET_PARAMETER Function parameter ReflectionParameter
Attribute::TARGET_PARAMETER Method parameter ReflectionParameter
Attribute::TARGET_CONSTANT Global constant ReflectionConstant

Note that

  • ReflectionEnum is a subclass of ReflectionClass
  • ReflectionEnumBackedCase is a subclass of ReflectionEnumUnitCase which is a subclass of ReflectionClassConstant

To be clear, even when the ReflectionAttribute was retrieved from a different type, the return type will be the type listed in the table above. For example, using ReflectionClassConstant with an enum case works fine, but ReflectionAttribute::getReflectionTarget() will return the enum-specific subclass. Similarly, ReflectionObject can be used for classes and enums, but ReflectionClass or ReflectionEnum will be returned from ReflectionAttribute::getReflectionTarget()

Backward Incompatible Changes

None

Proposed PHP Version(s)

Next minor version (PHP 8.7).

RFC Impact

To the Ecosystem

Attribute-related libraries may want to make use of the new feature.

To Existing Extensions

Only ext/reflection is affected - all of this is implemented in reflection.

To SAPIs

None

Open Issues

None

Voting Choices

Add ReflectionAttributeTarget, ReflectionTargetAwareAttribute, and ReflectionAttribute::getReflectionTarget()?


Primary Vote requiring a 2/3 majority to accept the RFC:

TODO
Real name Yes No Abstain
Final result: 0 0 0
This poll has been closed.

Patches and Tests

Implementation

After the RFC is implemented, this section should contain:

  1. the version(s) it was merged into
  2. a link to the git commit(s)
  3. a link to the PHP manual entry for the feature

References

Rejected Features

Keep this updated with features that were discussed on the mail lists.

ReflectionAttribute::getCurrent()

A previous version of this RFC proposed that, instead of an interface to opt-in to a setter method being called, a static ReflectionAttribute::getCurrent() method be added to allow retrieving the current ReflectionAttribute when called from within an attribute's constructor. After feedback on the mailing list, this was switched to an interface and a setter method.

Changelog

If there are major changes to the initial proposal, please include a short summary with a date or a link to the mailing list announcement here, as not everyone has access to the wikis' version history.

  • v0.1: created
  • v0.2: updates with improvements from Crell
  • v0.3: switch from having just a static method, ReflectionAttribute::getCurrent(): ReflectionAttributeTarget, to a static method, ReflectionAttribute::getCurrent(): ReflectionAttribute and an instance method ReflectionAttribute::getReflectionTarget(): ReflectionAttributeTarget
  • v0.4: in light of initial votes and feedback, both on the mailing list and elsewhere, RFC vote cancelled pending overhaul
  • v0.5: overhaul to interface and setter method
rfc/target-aware-attributes.txt · Last modified: by daniels