brenoroosevelt / php-attributes
Easy way to handle PHP Attributes
1.0.0
2021-08-05 18:18 UTC
Requires
- php: ^8
- brenoroosevelt/flex-fqcn-finder: ^1
Requires (Dev)
- phpstan/phpstan: ^0.12.90
- phpunit/phpunit: ^9.0
- squizlabs/php_codesniffer: 3.5.*
README
Easy way to extract and handle PHP Attributes.
Requirements
- PHP >= 8.1
Install
composer require brenoroosevelt/php-attributes
Usage
Instead of doing like this:
<?php $myAttribute = Attr::class; $attributes = []; $relfectionClass = new ReflectionClass(MyClass::class); foreach ($relfectionClass->getAttributes($myAttribute) as $attribute) { $attributes[] = $attribute; } foreach ($relfectionClass->getMethods() as $methods) { foreach ($methods->getAttributes($myAttribute) as $attribute) { $attributes[] = $attribute; } } foreach ($relfectionClass->getProperties() as $property) { /** ... */ } foreach ($relfectionClass->getReflectionConstants() as $property) { /** ... */ } $instances = array_map(fn(ReflectionAttribute $attr) => $attr->newInstance(), $attributes);
With this package you can simplify:
<?php use BrenoRoosevelt\PhpAttributes\Attributes; $instances = Attributes::extract(MyAttr::class)->fromClass(MyClass::class)->getInstances();
Explaining parameters in detail:
<?php use BrenoRoosevelt\PhpAttributes\ParsedAttribtubeCollection; $extract = Attributes::extract( // $attribute: the attribute name (string) // default values is NULL (search for all attributes) Attribute::class, // $flag: flags to filter attributes. // default values is 0 (no filter will be applied) ReflectionAttribute::IS_INSTANCEOF );
All these methods will return a collection of ParsedAttribute
.
fromClass(string|object|array $objectOrClass): Collection
fromProperties(string|object|array $objectOrClass, string ...$property)
fromMethods(string|object|array $objectOrClass, string ...$method)
fromClassConstants(string|object|array $objectOrClass, string ...$constant)
fromParameters(string|object|array $objectOrClass, string $method, string ...$parameter)
fromConstructor(string|object|array $objectOrClass)
fromConstructorParameters(string|object|array $objectOrClass, string ...$parameter)
The Collection class is immutable and fluent:
<?php /* $attributes = Attributes::extract()->from... */ // Collection $attributes->add(new ParsedAttribute(...)) // new Collection instance (immutable) $attributes->merge(new Collection); // new Collection instance (immutable) $attributes->getInstances(); // object[] array with attributes instances $attributes->getTargets(); // Reflector[] array with Reflection objects target by attributes $attributes->getAttributes(); // ReflectionAttribute[] $attributes->count(); // int $attributes->isEmpty(); // bool $attributes->first(); // null|(object) ParsedAttribute $attributes->toArray(); // ParsedAttribute[] // Iterable (ParsedAttribute[]) foreach ($attributes as $attr) { $attr->attribute(); // ReflectionAttribute $attr->target(); // ReflectionClass|ReflectionClassConstant| // ReflectionProperty|ReflectionMethod|ReflectionParameter }
Contributing
Run test suite
composer test
Run analysis
- Test suite
- Static analysis
- Coding Standard
composer check
License
This project is licensed under the terms of the MIT license. See the LICENSE file for license rights and limitations.