Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ClassReflection
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
2 / 2
6
100.00% covered (success)
100.00%
1 / 1
 isInstantiableSubclassOf
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getAttributeArguments
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
4
1<?php
2
3declare(strict_types=1);
4
5/**
6 * Fast Forward Development Tools for PHP projects.
7 *
8 * This file is part of fast-forward/dev-tools project.
9 *
10 * @author   Felipe SayĆ£o Lobato Abreu <github@mentordosnerds.com>
11 * @license  https://opensource.org/licenses/MIT MIT License
12 *
13 * @see      https://github.com/php-fast-forward/
14 * @see      https://github.com/php-fast-forward/dev-tools
15 * @see      https://github.com/php-fast-forward/dev-tools/issues
16 * @see      https://php-fast-forward.github.io/dev-tools/
17 * @see      https://datatracker.ietf.org/doc/html/rfc2119
18 */
19
20namespace FastForward\DevTools\Reflection;
21
22use ReflectionClass;
23use ReflectionMethod;
24
25/**
26 * Centralizes small reflection lookups used by DevTools runtime metadata.
27 *
28 * This helper keeps command discovery code focused on command behavior instead of
29 * raw reflection boilerplate.
30 */
31 class ClassReflection
32{
33    /**
34     * Detects whether a class can be instantiated as a subclass of another class.
35     *
36     * @param class-string $className the class being inspected
37     * @param class-string $parentClass the required parent class or interface
38     *
39     * @return bool true when the class is instantiable and extends or implements the expected parent
40     */
41    public static function isInstantiableSubclassOf(string $className, string $parentClass): bool
42    {
43        $reflection = new ReflectionClass($className);
44
45        return $reflection->isInstantiable() && $reflection->isSubclassOf($parentClass);
46    }
47
48    /**
49     * Returns the first matching attribute arguments normalized by constructor parameter name.
50     *
51     * Positional arguments are mapped to their constructor parameter names so callers do not
52     * need to understand how the attribute was declared at the call site.
53     *
54     * @param class-string $className the class being inspected
55     * @param class-string $attributeClass the attribute class being read
56     *
57     * @return array<string, mixed>|null the normalized argument map, or null when the attribute is absent
58     */
59    public static function getAttributeArguments(string $className, string $attributeClass): ?array
60    {
61        $reflection = new ReflectionClass($className);
62        $attribute = $reflection->getAttributes($attributeClass)[0] ?? null;
63
64        if (null === $attribute) {
65            return null;
66        }
67
68        $arguments = $attribute->getArguments();
69        $constructor = new ReflectionMethod($attributeClass, '__construct');
70        $normalizedArguments = [];
71
72        foreach ($constructor->getParameters() as $parameter) {
73            $normalizedArguments[$parameter->getName()] = $arguments[$parameter->getName()]
74                ?? $arguments[$parameter->getPosition()]
75                ?? ($parameter->isDefaultValueAvailable() ? $parameter->getDefaultValue() : null);
76        }
77
78        return $normalizedArguments;
79    }
80}