A convenient and near zero-cost way to retrieve targets of PHP 8 attributes
A convenient and near zero-cost way to retrieve targets of PHP 8 attributes
composer-attribute-collector is a Composer plugin that statically discovers PHP 8 attribute targets and retrieves them at near-zero cost, without runtime reflection. After the autoloader dump, it collects attributes and generates a static file for fast access. This provides a convenient way to discover attribute-backed classes, methods, or properties—ideal for codebase analysis. (For known targets, traditional reflection remains an option.)
[!NOTE] Currently, the plugin supports class, method, property, and parameter targets. You're welcome to contribute if you're interested in expending its support.
[!WARNING] Attributes used on functions are ignored at this time.
The following example demonstrates how targets and their attributes can be retrieved:
…Here are a few steps to get you started.
The collector automatically scans autoload paths of the root composer.json for a
zero-configuration experience. You can override them via
extra.composer-attribute-collector.include.
{
"extra": {
"composer-attribute-collector": {
"include": [
"src"
]
}
}
}Check the Configuration options for more details.
Use Composer to install the plugin.
You will be asked if you trust the plugin and wish to activate it, select y to proceed.
composer require olvlvl/composer-attribute-collectorYou should see log messages similar to this:
Generating autoload files
Generating attributes file
Generated attributes file in 9.137 ms
Generated autoload files[!TIP] See the Frequently Asked Questions section to automatically refresh the "attributes" file during development.
You can require the "attributes" file using require_once 'vendor/attributes.php'; but you might
prefer to use Composer's autoloading feature:
{
"autoload": {
"files": [
"vendor/attributes.php"
]
}
}Here are a few ways you can configure the plugin.
The collector automatically scans autoload paths of the root composer.json, but you can override
them via the include property.
The specified paths are relative to the composer.json file, and the {vendor} placeholder is
replaced with the path to the vendor folder.
{
"extra": {
"composer-attribute-collector": {
"include": [
"path-or-file/to/include"
]
}
}
}Use the exclude property to exclude some files or paths from scanning. This is handy when files
cause issues or have side effects.
The specified paths are relative to the composer.json file, and the {vendor} placeholder is
replaced with the path to the vendor folder. Entries follow Composer's exclude-from-classmap
glob syntax: * matches anything but /, ** matches anything, and ** is implicitly added
to the end of each path.
{
"extra": {
"composer-attribute-collector": {
"exclude": [
"path-or-file/to/exclude",
"tests/**"
]
}
}
}By default, the plugin generates an embedded representation of the collected attributes, with their
arguments, which requires no reflection. This can be an issue with PHP 8.1's New in initializers
or PHP 8.5's Closures in constant expressions.
Use the reference strategy for maximum compatibility; the generated file will only contain
references, and attributes will be created using reflection.
{
"extra": {
"composer-attribute-collector": {
"strategy": "reference"
}
}
}The plugin is able to maintain a cache to reuse discoveries between runs. To enable the cache,
set the environment variable COMPOSER_ATTRIBUTE_COLLECTOR_USE_CACHE to 1, yes, or true.
Cache items are persisted in the .composer-attribute-collector directory, you might want to add
it to your .gitignore file.
COMPOSER_ATTRIBUTE_COLLECTOR_USE_CACHE=1 composer dump-autoloadUse cases are available to test the plugin in real conditions:
Incompatible signature The plugin is able to collect attributes, although the PSR Logger version used by Composer and the application are incompatible.
Symfony A Symfony application, created with symfony new.
Laravel A Laravel application, created with laravel new.
You don't need to generate an optimized autoloader for this to work. The plugin uses code similar to Composer to find classes. Anything that works with Composer should work with the plugin.
Yes, you can use the plugin during development, but keep in mind the "attributes" file is only
generated after the autoloader is dumped. If you modify attributes you will have to run
composer dump-autoload to refresh the "attributes" file.
As a workaround you could have watchers on the directories that contain classes with attributes to
run XDEBUG_MODE=off composer dump-autoload when you make changes. PhpStorm offers file
watchers. You could also use spatie/file-system-watcher, it only requires
PHP. If the plugin is too slow for your liking, try running the command with
COMPOSER_ATTRIBUTE_COLLECTOR_USE_CACHE=yes, it will enable caching and speed up consecutive runs.
To speed up the collection process, the plugin first looks at PHP files as plain text for hints of
attribute usage. If a class inherits its attributes from traits, properties, or methods, but doesn't
use attributes itself, it will be ignored. Use the attribute
[#\olvlvl\ComposerAttributeCollector\InheritsAttributes] to force the collection.
trait UrlTrait
{
#[UrlGetter]
public function get_url(): string
{
return '/url';
}
}
#[InheritsAttributes]
class InheritedAttributeSample
{
use UrlTrait;
}The project is continuously tested by GitHub Actions.
This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you're expected to uphold this code.
See CONTRIBUTING for details.
No open issues yet, or sync has not completed.