Passed
Pull Request — master (#39)
by
unknown
02:08
created

ClassConfigFactory::getMethodParameterConfigs()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
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 Yiisoft\Proxy\Config\ClassConfig;
15
use Yiisoft\Proxy\Config\MethodConfig;
16
use Yiisoft\Proxy\Config\ParameterConfig;
17
use Yiisoft\Proxy\Config\TypeConfig;
18
19
final class ClassConfigFactory
20
{
21 8
    public function getClassConfig(string $interfaceName): ClassConfig
22
    {
23
        try {
24 8
            $reflection = new ReflectionClass($interfaceName);
25 1
        } catch (ReflectionException) {
26 1
            throw new InvalidArgumentException("$interfaceName must exist.");
27
        }
28
29 7
        return new ClassConfig(
30 7
            isInterface: $reflection->isInterface(),
31 7
            namespace: $reflection->getNamespaceName(),
32 7
            modifiers: Reflection::getModifierNames($reflection->getModifiers()),
33 7
            name: $reflection->getName(),
34 7
            shortName: $reflection->getShortName(),
35 7
            parent: (string) $reflection->getParentClass(),
36 7
            interfaces: $reflection->getInterfaceNames(),
37 7
            methods: $this->getMethodConfigs($reflection),
38
        );
39
    }
40
41
    /**
42
     * @return MethodConfig[]
43
     */
44 7
    private function getMethodConfigs(ReflectionClass $reflection): array
45
    {
46 7
        $methods = [];
47 7
        foreach ($reflection->getMethods() as $method) {
48 7
            $methods[$method->getName()] = $this->getMethodConfig($method);
49
        }
50
51 7
        return $methods;
52
    }
53
54 7
    private function getMethodConfig(ReflectionMethod $method): MethodConfig
55
    {
56 7
        return new MethodConfig(
57 7
            modifiers: Reflection::getModifierNames($method->getModifiers()),
58 7
            name: $method->getName(),
59 7
            parameters: $this->getMethodParameterConfigs($method),
60 7
            returnType: $this->getMethodTypeConfig($method),
61
        );
62
    }
63
64
    /**
65
     * @return ParameterConfig[]
66
     */
67 7
    private function getMethodParameterConfigs(ReflectionMethod $method): array
68
    {
69 7
        $parameters = [];
70 7
        foreach ($method->getParameters() as $param) {
71 5
            $parameters[$param->getName()] = $this->getMethodParameterConfig($param);
72
        }
73
74 7
        return $parameters;
75
    }
76
77 5
    private function getMethodParameterConfig(ReflectionParameter $param): ParameterConfig
78
    {
79 5
        return new ParameterConfig(
80 5
            type: $this->getMethodParameterTypeConfig($param),
81 5
            name: $param->getName(),
82 5
            allowsNull: $param->allowsNull(),
83 5
            isDefaultValueAvailable: $param->isDefaultValueAvailable(),
84 5
            isDefaultValueConstant: $param->isDefaultValueAvailable()
85 2
                ? $param->isDefaultValueConstant()
86 5
                : null,
87 5
            defaultValueConstantName: $param->isOptional()
88 2
                ? $param->getDefaultValueConstantName()
89 5
                : null,
90 5
            defaultValue: $param->isOptional()
91 2
                ? $param->getDefaultValue()
92 5
                : null,
93
        );
94
    }
95
96 5
    private function getMethodParameterTypeConfig(ReflectionParameter $param): ?TypeConfig
97
    {
98
        /** @var ReflectionNamedType $type */
99 5
        $type = $param->getType();
100 5
        if (!$type) {
0 ignored issues
show
introduced by
$type is of type ReflectionNamedType, thus it always evaluated to true.
Loading history...
101 2
            return null;
102
        }
103
104 5
        return new TypeConfig(
105 5
            name: $type->getName(),
106 5
            allowsNull: $type->allowsNull(),
107
        );
108
    }
109
110 7
    private function getMethodTypeConfig(ReflectionMethod $method): ?TypeConfig
111
    {
112
        /** @var ReflectionNamedType $returnType */
113 7
        $returnType = $method->getReturnType();
114 7
        if (!$returnType) {
0 ignored issues
show
introduced by
$returnType is of type ReflectionNamedType, thus it always evaluated to true.
Loading history...
115 2
            return null;
116
        }
117
118 7
        return new TypeConfig(
119 7
            name: $returnType->getName(),
120 7
            allowsNull: $returnType->allowsNull(),
121
        );
122
    }
123
}
124