Passed
Pull Request — master (#46)
by
unknown
02:46
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 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 7
ccs 6
cts 6
cp 1
crap 1
rs 10
c 1
b 0
f 0
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 9
    public function getClassConfig(string $className): ClassConfig
37
    {
38
        try {
39 9
            $reflection = new ReflectionClass($className);
40 1
        } catch (ReflectionException) {
41 1
            throw new InvalidArgumentException("$className must exist.");
42
        }
43
44 8
        return new ClassConfig(
45 8
            isInterface: $reflection->isInterface(),
46 8
            namespace: $reflection->getNamespaceName(),
47 8
            modifiers: Reflection::getModifierNames($reflection->getModifiers()),
48 8
            name: $reflection->getName(),
49 8
            shortName: $reflection->getShortName(),
50 8
            parent: (string) $reflection->getParentClass(),
51 8
            interfaces: $reflection->getInterfaceNames(),
52 8
            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 8
    private function getMethodConfigs(ReflectionClass $class): array
64
    {
65 8
        $methods = [];
66 8
        foreach ($class->getMethods() as $method) {
67 8
            $methods[$method->getName()] = $this->getMethodConfig($method);
68
        }
69
70 8
        return $methods;
71
    }
72
73
    /**
74
     * Gets single method config for individual method reflection.
75
     *
76
     * @param ReflectionMethod $method Reflection of a method.
77
     *
78
     * @return MethodConfig Single method config.
79
     */
80 8
    private function getMethodConfig(ReflectionMethod $method): MethodConfig
81
    {
82 8
        return new MethodConfig(
83 8
            modifiers: Reflection::getModifierNames($method->getModifiers()),
84 8
            name: $method->getName(),
85 8
            parameters: $this->getMethodParameterConfigs($method),
86 8
            returnType: $this->getMethodReturnTypeConfig($method),
87
        );
88
    }
89
90
    /**
91
     * Gets the complete set of parameter configs for a given method reflection.
92
     *
93
     * @param ReflectionMethod $method Reflection of a method.
94
     *
95
     * @return ParameterConfig[] List of parameter configs. The order is maintained.
96
     */
97 8
    private function getMethodParameterConfigs(ReflectionMethod $method): array
98
    {
99 8
        $parameters = [];
100 8
        foreach ($method->getParameters() as $param) {
101 5
            $parameters[$param->getName()] = $this->getMethodParameterConfig($param);
102
        }
103
104 8
        return $parameters;
105
    }
106
107
    /**
108
     * Gets single parameter config for individual method's parameter reflection.
109
     *
110
     * @param ReflectionParameter $param Reflection of a method's parameter.
111
     *
112
     * @return ParameterConfig Single parameter config.
113
     */
114 5
    private function getMethodParameterConfig(ReflectionParameter $param): ParameterConfig
115
    {
116 5
        return new ParameterConfig(
117 5
            type: $this->getMethodParameterTypeConfig($param),
118 5
            name: $param->getName(),
119 5
            allowsNull: $param->allowsNull(),
120 5
            isDefaultValueAvailable: $param->isDefaultValueAvailable(),
121 5
            isDefaultValueConstant: $param->isDefaultValueAvailable()
122 2
                ? $param->isDefaultValueConstant()
123 5
                : null,
124 5
            defaultValueConstantName: $param->isOptional()
125 2
                ? $param->getDefaultValueConstantName()
126 5
                : null,
127 5
            defaultValue: $param->isOptional()
128 2
                ? $param->getDefaultValue()
129 5
                : null,
130
        );
131
    }
132
133
    /**
134
     * Gets single type config for individual method's parameter reflection.
135
     *
136
     * @param ReflectionParameter $param Reflection pf a method's parameter.
137
     *
138
     * @return TypeConfig|null Single type config. `null` is returned when type is not specified.
139
     */
140 5
    private function getMethodParameterTypeConfig(ReflectionParameter $param): ?TypeConfig
141
    {
142 5
        $type = $param->getType();
143 5
        if (!$type) {
144 2
            return null;
145
        }
146
147 5
        if ($type instanceof ReflectionUnionType) {
148 2
            $name = $this->getUnionType($type);
149
        } else {
150
            /** @var ReflectionNamedType $type */
151 5
            $name = $type->getName();
152
        }
153
154 5
        return new TypeConfig(
155
            name: $name,
156 5
            allowsNull: $type->allowsNull(),
157
        );
158
    }
159
160
    /**
161
     * Gets single return type config for individual method reflection.
162
     *
163
     * @param ReflectionMethod $method Reflection of a method.
164
     *
165
     * @return TypeConfig|null Single type config. `null` is returned when return type is not specified.
166
     */
167 8
    private function getMethodReturnTypeConfig(ReflectionMethod $method): ?TypeConfig
168
    {
169 8
        $returnType = $method->getReturnType();
170 8
        if (!$returnType && method_exists($method, 'getTentativeReturnType')) {
171
            $returnType = $method->getTentativeReturnType();
172
        }
173
174 8
        if (!$returnType) {
175 3
            return null;
176
        }
177
178 7
        $name = $returnType instanceof ReflectionUnionType
179 3
            ? $this->getUnionType($returnType)
180 7
            : $returnType->getName();
181
182 7
        return new TypeConfig(
183
            name: $name,
184 7
            allowsNull: $returnType->allowsNull(),
185
        );
186
    }
187
188 5
    private function getUnionType(ReflectionUnionType $type): string
189
    {
190 5
        $types = array_map(
191 5
            static fn (ReflectionNamedType $namedType) => $namedType->getName(),
192 5
            $type->getTypes()
193
        );
194
195 5
        return implode('|', $types);
196
    }
197
}
198