Skip to content

brenoroosevelt/php-attributes

Repository files navigation

PHP Attributes

Build codecov Scrutinizer Code Quality Latest Version Software License

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.

About

Easy way to extract and handle PHP Attributes

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages