Passed
Pull Request — master (#21)
by
unknown
02:22
created

ClassConfigFactory::getMethodConfigs()   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 ReflectionParameter;
13
use Yiisoft\Proxy\Config\ClassConfig;
14
use Yiisoft\Proxy\Config\MethodConfig;
15
use Yiisoft\Proxy\Config\ParameterConfig;
16
use Yiisoft\Proxy\Config\TypeConfig;
17
18
final class ClassConfigFactory
19
{
20 3
    public function getIntergaceConfig(string $interfaceName): ClassConfig
21
    {
22
        try {
23 3
            $reflection = new ReflectionClass($interfaceName);
24 1
        } catch (ReflectionException) {
25 1
            throw new InvalidArgumentException("$interfaceName must exist.");
26
        }
27
28 2
        if (!$reflection->isInterface()) {
29 1
            throw new InvalidArgumentException("$interfaceName is not an interface.");
30
        }
31
32 1
        return new ClassConfig(
33
            isInterface: true,
34 1
            namespace: $reflection->getNamespaceName(),
35 1
            modifiers: Reflection::getModifierNames($reflection->getModifiers()),
36 1
            name: $reflection->getName(),
37 1
            shortName: $reflection->getShortName(),
38 1
            parent: (string) $reflection->getParentClass(),
39 1
            interfaces: $reflection->getInterfaceNames(),
40 1
            methods: $this->getMethodConfigs($reflection),
41
        );
42
    }
43
44
    /**
45
     * @return MethodConfig[]
46
     */
47 1
    private function getMethodConfigs(ReflectionClass $reflection): array
48
    {
49 1
        $methods = [];
50 1
        foreach ($reflection->getMethods() as $method) {
51 1
            $methods[$method->getName()] = $this->getMethodConfig($method);
52
        }
53
54 1
        return $methods;
55
    }
56
57 1
    private function getMethodConfig(ReflectionMethod $method): MethodConfig
58
    {
59 1
        return new MethodConfig(
60 1
            modifiers: Reflection::getModifierNames($method->getModifiers()),
61 1
            name: $method->getName(),
62 1
            parameters: $this->getMethodParameterConfigs($method),
63 1
            hasReturnType: $method->hasReturnType(),
64 1
            returnType: $this->getMethodTypeConfig($method),
65
        );
66
    }
67
68
    /**
69
     * @return ParameterConfig[]
70
     */
71 1
    private function getMethodParameterConfigs(ReflectionMethod $method): array
72
    {
73 1
        $parameters = [];
74 1
        foreach ($method->getParameters() as $param) {
75 1
            $parameters[$param->getName()] = $this->getMethodParameterConfig($param);
76
        }
77
78 1
        return $parameters;
79
    }
80
81 1
    private function getMethodParameterConfig(ReflectionParameter $param): ParameterConfig
82
    {
83 1
        return new ParameterConfig(
84 1
            hasType: $param->hasType(),
85 1
            type: $this->getMethodParameterTypeConfig($param),
86 1
            name: $param->getName(),
87 1
            allowsNull: $param->allowsNull(),
88 1
            isDefaultValueAvailable: $param->isDefaultValueAvailable(),
89 1
            isDefaultValueConstant: $param->isDefaultValueAvailable()
90 1
                ? $param->isDefaultValueConstant()
91 1
                : null,
92 1
            defaultValueConstantName: $param->isOptional()
93 1
                ? $param->getDefaultValueConstantName()
94 1
                : null,
95 1
            defaultValue: $param->isOptional()
96 1
                ? $param->getDefaultValue()
97 1
                : null,
98
        );
99
    }
100
101 1
    private function getMethodParameterTypeConfig(ReflectionParameter $param): ?TypeConfig
102
    {
103 1
        $type = $param->getType();
104 1
        if (!$type) {
105 1
            return null;
106
        }
107
108 1
        return new TypeConfig(
109 1
            name: $type->getName(),
0 ignored issues
show
Bug introduced by
The method getName() does not exist on ReflectionType. It seems like you code against a sub-type of ReflectionType such as ReflectionNamedType. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
            name: $type->/** @scrutinizer ignore-call */ getName(),
Loading history...
110 1
            allowsNull: $type->allowsNull(),
111
        );
112
    }
113
114 1
    private function getMethodTypeConfig(ReflectionMethod $method): ?TypeConfig
115
    {
116 1
        $returnType = $method->getReturnType();
117 1
        if (!$returnType) {
118 1
            return null;
119
        }
120
121 1
        return new TypeConfig(
122 1
            name: $returnType->getName(),
123 1
            allowsNull: $returnType->allowsNull(),
124
        );
125
    }
126
}
127