Passed
Push — master ( 7c747f...11d3a2 )
by
unknown
03:14 queued 59s
created

ClassConfigFactory::getMethodConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 3
b 0
f 0
nc 1
nop 2
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Proxy;
6
7
use InvalidArgumentException;
8
use Reflection;
9
use ReflectionClass;
10
use ReflectionException;
11
use ReflectionMethod;
12
use ReflectionNamedType;
13
use ReflectionParameter;
14
use ReflectionUnionType;
15
use Yiisoft\Proxy\Config\ClassConfig;
16
use Yiisoft\Proxy\Config\MethodConfig;
17
use Yiisoft\Proxy\Config\ParameterConfig;
18
use Yiisoft\Proxy\Config\TypeConfig;
19
20
/**
21
 * A factory for creating class configs ({@see ClassConfig}). Uses PHP `Reflection` to get the necessary metadata.
22
 *
23
 * @link https://www.php.net/manual/en/book.reflection.php
24
 */
25
final class ClassConfigFactory
26
{
27
    /**
28
     * Gets single class config based for individual class.
29
     *
30
     * @param string $className Full class or interface name (including namespace).
31
     *
32
     * @throws InvalidArgumentException In case class or interface does not exist.
33
     *
34
     * @return ClassConfig Class config with all related configs (methods, parameters, types) linked.
35
     */
36 12
    public function getClassConfig(string $className): ClassConfig
37
    {
38
        try {
39 12
            $reflection = new ReflectionClass($className);
40 1
        } catch (ReflectionException) {
41 1
            throw new InvalidArgumentException("$className must exist.");
42
        }
43
44 11
        return new ClassConfig(
45 11
            isInterface: $reflection->isInterface(),
46 11
            namespace: $reflection->getNamespaceName(),
47 11
            modifiers: Reflection::getModifierNames($reflection->getModifiers()),
48 11
            name: $reflection->getName(),
49 11
            shortName: $reflection->getShortName(),
50 11
            parent: (string) $reflection->getParentClass(),
51 11
            interfaces: $reflection->getInterfaceNames(),
52 11
            methods: $this->getMethodConfigs($reflection),
53
        );
54
    }
55
56
    /**
57
     * Gets the complete set of method configs for a given class reflection.
58
     *
59
     * @param ReflectionClass $class Reflection of a class.
60
     *
61
     * @return MethodConfig[] List of method configs. The order is maintained.
62
     */
63 11
    private function getMethodConfigs(ReflectionClass $class): array
64
    {
65 11
        $methods = [];
66 11
        foreach ($class->getMethods() as $method) {
67 10
            $methods[$method->getName()] = $this->getMethodConfig($class, $method);
68
        }
69
70 11
        return $methods;
71
    }
72
73
    /**
74
     * Gets single method config for individual class / method reflection pair.
75
     *
76
     * @param ReflectionClass $class Reflection of a class.
77
     * @param ReflectionMethod $method Reflection of a method.
78
     *
79
     * @return MethodConfig Single method config.
80
     */
81 10
    private function getMethodConfig(ReflectionClass $class, ReflectionMethod $method): MethodConfig
82
    {
83 10
        return new MethodConfig(
84 10
            modifiers: $this->getMethodModifiers($class, $method),
85 10
            name: $method->getName(),
86 10
            parameters: $this->getMethodParameterConfigs($method),
87 10
            returnType: $this->getMethodReturnTypeConfig($method),
88
        );
89
    }
90
91
    /**
92
     * Gets the set of method modifiers for a given class / method reflection pair
93
     *
94
     * @param ReflectionClass $class Reflection of a class.
95
     * @param ReflectionMethod $method Reflection of a method.
96
     *
97
     * @return string[] List of method modifiers.
98
     */
99 10
    private function getMethodModifiers(ReflectionClass $class, ReflectionMethod $method): array
100
    {
101 10
        $modifiers = Reflection::getModifierNames($method->getModifiers());
102 10
        if (!$class->isInterface()) {
103 5
            return $modifiers;
104
        }
105
106 5
        return array_values(array_filter($modifiers, static fn (string $modifier) => $modifier !== 'abstract'));
107
    }
108
109
    /**
110
     * Gets the complete set of parameter configs for a given method reflection.
111
     *
112
     * @param ReflectionMethod $method Reflection of a method.
113
     *
114
     * @return ParameterConfig[] List of parameter configs. The order is maintained.
115
     */
116 10
    private function getMethodParameterConfigs(ReflectionMethod $method): array
117
    {
118 10
        $parameters = [];
119 10
        foreach ($method->getParameters() as $param) {
120 6
            $parameters[$param->getName()] = $this->getMethodParameterConfig($param);
121
        }
122
123 10
        return $parameters;
124
    }
125
126
    /**
127
     * Gets single parameter config for individual method's parameter reflection.
128
     *
129
     * @param ReflectionParameter $param Reflection of a method's parameter.
130
     *
131
     * @return ParameterConfig Single parameter config.
132
     */
133 6
    private function getMethodParameterConfig(ReflectionParameter $param): ParameterConfig
134
    {
135 6
        return new ParameterConfig(
136 6
            type: $this->getMethodParameterTypeConfig($param),
137 6
            name: $param->getName(),
138 6
            isDefaultValueAvailable: $param->isDefaultValueAvailable(),
139 6
            isDefaultValueConstant: $param->isDefaultValueAvailable()
140 2
                ? $param->isDefaultValueConstant()
141 6
                : null,
142 6
            defaultValueConstantName: $param->isOptional()
143 2
                ? $param->getDefaultValueConstantName()
144 6
                : null,
145 6
            defaultValue: $param->isOptional()
146 2
                ? $param->getDefaultValue()
147 6
                : null,
148
        );
149
    }
150
151
    /**
152
     * Gets single type config for individual method's parameter reflection.
153
     *
154
     * @param ReflectionParameter $param Reflection pf a method's parameter.
155
     *
156
     * @return TypeConfig|null Single type config. `null` is returned when type is not specified.
157
     */
158 6
    private function getMethodParameterTypeConfig(ReflectionParameter $param): ?TypeConfig
159
    {
160 6
        $type = $param->getType();
161 6
        if (!$type) {
162 2
            return null;
163
        }
164
165 6
        if ($type instanceof ReflectionUnionType) {
166 2
            $name = $this->getUnionType($type);
167
        } else {
168
            /** @var ReflectionNamedType $type */
169 6
            $name = $type->getName();
170
        }
171
172 6
        return new TypeConfig(
173
            name: $name,
174 6
            allowsNull: $type->allowsNull(),
175
        );
176
    }
177
178
    /**
179
     * Gets single return type config for individual method reflection.
180
     *
181
     * @param ReflectionMethod $method Reflection of a method.
182
     *
183
     * @return TypeConfig|null Single type config. `null` is returned when return type is not specified.
184
     */
185 10
    private function getMethodReturnTypeConfig(ReflectionMethod $method): ?TypeConfig
186
    {
187 10
        $returnType = $method->getReturnType();
188 10
        if (!$returnType && method_exists($method, 'getTentativeReturnType')) {
189
            $returnType = $method->getTentativeReturnType();
190
        }
191
192 10
        if (!$returnType) {
193 4
            return null;
194
        }
195
196 9
        $name = $returnType instanceof ReflectionUnionType
197 3
            ? $this->getUnionType($returnType)
198 9
            : $returnType->getName();
199
200 9
        return new TypeConfig(
201
            name: $name,
202 9
            allowsNull: $returnType->allowsNull(),
203
        );
204
    }
205
206 5
    private function getUnionType(ReflectionUnionType $type): string
207
    {
208 5
        $types = array_map(
209 5
            static fn (ReflectionNamedType $namedType) => $namedType->getName(),
210 5
            $type->getTypes()
211
        );
212
213 5
        return implode('|', $types);
214
    }
215
}
216