Passed
Push — master ( b678cc...5a9a7b )
by Pieter
03:14
created

GroupedObjectAccess::getMethodArguments()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 3
rs 10
1
<?php
2
3
4
namespace W2w\Lib\ApieObjectAccessNormalizer\ObjectAccess;
5
6
use ReflectionClass;
7
use ReflectionMethod;
8
use Symfony\Component\PropertyInfo\Type;
9
10
class GroupedObjectAccess implements ObjectAccessSupportedInterface
11
{
12
    /**
13
     * @var ObjectAccessInterface
14
     */
15
    private $fallback;
16
17
    /**
18
     * @var ObjectAccessInterface[]
19
     */
20
    private $specificObjectAccess;
21
22
    /**
23
     * @param ObjectAccessInterface $fallback
24
     * @param ObjectAccessInterface[] $specificObjectAccess
25
     */
26
    public function __construct(ObjectAccessInterface $fallback, array $specificObjectAccess)
27
    {
28
        $this->fallback = $fallback;
29
        $this->specificObjectAccess = $specificObjectAccess;
30
    }
31
32
    /**
33
     * Returns the correct ObjectAccessInterface instance.
34
     *
35
     * @param ReflectionClass $reflectionClass
36
     * @return ObjectAccessInterface
37
     */
38
    private function findObjectAccessForClass(ReflectionClass $reflectionClass): ObjectAccessInterface
39
    {
40
        if (isset($this->specificObjectAccess[$reflectionClass->name])) {
41
            return $this->specificObjectAccess[$reflectionClass->name];
42
        }
43
        foreach ($this->specificObjectAccess as $key => $objectAccess) {
44
            if ($objectAccess instanceof ObjectAccessSupportedInterface && $objectAccess->isSupported($reflectionClass)) {
45
                return $objectAccess;
46
            }
47
            if (is_a($reflectionClass->name, $key, true)) {
48
                return $objectAccess;
49
            }
50
        }
51
        return $this->fallback;
52
    }
53
54
    /**
55
     * {@inheritDoc}
56
     */
57
    public function getGetterFields(ReflectionClass $reflectionClass): array
58
    {
59
        return $this->findObjectAccessForClass($reflectionClass)->getGetterFields($reflectionClass);
60
    }
61
62
    /**
63
     * {@inheritDoc}
64
     */
65
    public function getSetterFields(ReflectionClass $reflectionClass): array
66
    {
67
        return $this->findObjectAccessForClass($reflectionClass)->getSetterFields($reflectionClass);
68
    }
69
70
    /**
71
     * {@inheritDoc}
72
     */
73
    public function getGetterTypes(ReflectionClass $reflectionClass, string $fieldName): array
74
    {
75
        return $this->findObjectAccessForClass($reflectionClass)->getGetterTypes($reflectionClass, $fieldName);
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81
    public function getSetterTypes(ReflectionClass $reflectionClass, string $fieldName): array
82
    {
83
        return $this->findObjectAccessForClass($reflectionClass)->getSetterTypes($reflectionClass, $fieldName);
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89
    public function getConstructorArguments(ReflectionClass $reflectionClass): array
90
    {
91
        return $this->findObjectAccessForClass($reflectionClass)->getConstructorArguments($reflectionClass);
92
    }
93
94
    /**
95
     * {@inheritDoc}
96
     */
97
    public function getValue(object $instance, string $fieldName)
98
    {
99
        return $this->findObjectAccessForClass(new ReflectionClass($instance))->getValue($instance, $fieldName);
100
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105
    public function setValue(object $instance, string $fieldName, $value)
106
    {
107
        return $this->findObjectAccessForClass(new ReflectionClass($instance))->setValue($instance, $fieldName, $value);
108
    }
109
110
    /**
111
     * {@inheritDoc}
112
     */
113
    public function instantiate(ReflectionClass $reflectionClass, array $constructorArgs): object
114
    {
115
        return $this->findObjectAccessForClass($reflectionClass)->instantiate($reflectionClass, $constructorArgs);
116
    }
117
118
    /**
119
     * {@inheritDoc}
120
     */
121
    public function getDescription(ReflectionClass $reflectionClass, string $fieldName, bool $preferGetters): ?string
122
    {
123
        return $this->findObjectAccessForClass($reflectionClass)->getDescription($reflectionClass, $fieldName, $preferGetters);
124
    }
125
126
    /**
127
     * {@inheritDoc}
128
     */
129
    public function isSupported(ReflectionClass $reflectionClass): bool
130
    {
131
        $instance = $this->findObjectAccessForClass($reflectionClass);
132
        if ($instance instanceof ObjectAccessSupportedInterface) {
133
            return $instance->isSupported($reflectionClass);
134
        }
135
        return true;
136
    }
137
138
    /**
139
     * {@inheritDoc}
140
     */
141
    public function getMethodArguments(ReflectionMethod $method, ?ReflectionClass $reflectionClass = null): array
142
    {
143
        return $this->findObjectAccessForClass($reflectionClass)->getMethodArguments($method, $reflectionClass);
0 ignored issues
show
Bug introduced by
It seems like $reflectionClass can also be of type null; however, parameter $reflectionClass of W2w\Lib\ApieObjectAccess...dObjectAccessForClass() does only seem to accept ReflectionClass, maybe add an additional type check? ( Ignorable by Annotation )

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

143
        return $this->findObjectAccessForClass(/** @scrutinizer ignore-type */ $reflectionClass)->getMethodArguments($method, $reflectionClass);
Loading history...
144
    }
145
}
146