Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
AttributeCollector
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
1 / 1
 build
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <dev@noladev.com>
8 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Code\Reflection\Support;
16
17use Pop\Code\Generator\AttributeGenerator;
18
19/**
20 * Attribute collector class
21 *
22 * Builds an AttributeGenerator from a \ReflectionAttribute. Deliberately does not attempt any
23 * `use`-import wiring for the attribute's own class — only a top-level *Reflection::parse() call has
24 * direct access to the construct's namespace object, so that stays inline in each caller. A caller
25 * that does have that access (and a Reflection\Support\NamespaceImportResolver to go with it, for
26 * same-short-name collision avoidance) can override the computed name via $name.
27 *
28 * @category   Pop
29 * @package    Pop\Code
30 * @author     Nick Sagona, III <dev@noladev.com>
31 * @copyright  Copyright (c) 2009-2027 NOLA Interactive, LLC.
32 * @license    https://www.popphp.org/license     New BSD License
33 * @version    6.0.0
34 */
35class AttributeCollector
36{
37
38    /**
39     * Build an AttributeGenerator from a ReflectionAttribute
40     *
41     * @param  \ReflectionAttribute $reflectionAttribute
42     * @param  ?string              $name  overrides the short name this would otherwise compute
43     * @return AttributeGenerator
44     */
45    public static function build(\ReflectionAttribute $reflectionAttribute, ?string $name = null): AttributeGenerator
46    {
47        if ($name === null) {
48            $fqcn  = $reflectionAttribute->getName();
49            $parts = explode('\\', $fqcn);
50            $name  = str_contains($fqcn, '\\') ? end($parts) : '\\' . $fqcn;
51        }
52
53        $attribute = new AttributeGenerator($name);
54
55        foreach ($reflectionAttribute->getArguments() as $key => $value) {
56            if (is_string($key)) {
57                $attribute->addArgument($value, $key);
58            } else {
59                $attribute->addArgument($value);
60            }
61        }
62
63        return $attribute;
64    }
65
66}