GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( fbc2a3...222a71 )
by SignpostMarv
02:40
created

StaticMethodCollector::CollectInterfaces()   C

Complexity

Conditions 15
Paths 44

Size

Total Lines 54
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 24
CRAP Score 15.3085

Importance

Changes 0
Metric Value
cc 15
eloc 27
nc 44
nop 1
dl 0
loc 54
ccs 24
cts 27
cp 0.8889
crap 15.3085
rs 6.7097
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
/**
5
* @author SignpostMarv
6
*/
7
8
namespace SignpostMarv\DaftInterfaceCollector;
9
10
use Closure;
11
use Generator;
12
use ReflectionClass;
13
use ReflectionMethod;
14
use ReflectionType;
15
use Traversable;
16
17
class StaticMethodCollector
18
{
19
    /**
20
    * @var array<string, array<string, string[]>>
21
    */
22
    private $staticMethods = [];
23
24
    /**
25
    * @var string[]
26
    */
27
    private $interfaces = [];
28
29
    /**
30
    * @var array<int, string>
31
    */
32
    private $processedSources = [];
33
34
    /**
35
    * @var string[]
36
    */
37
    private $alreadyYielded = [];
38
39
    /**
40
    * @var bool
41
    */
42
    private $autoReset;
43
44 4
    public function __construct(array $staticMethods, array $interfaces, bool $autoReset = true)
45
    {
46
        /**
47
        * @var array<string, array> $filteredMethods
48
        */
49 4
        $filteredMethods = [];
50
51 4
        foreach ($this->FilterArrayOfInterfaceOffsets($staticMethods) as $interface => $methods) {
52 4
            $filteredMethods[$interface] = $this->FilterMethods($interface, $methods);
53
        }
54
55
        /**
56
        * @var array<string, array<string, string[]>> $filteredMethods
57
        */
58 4
        $filteredMethods = $this->FilterNonZeroArray($filteredMethods);
59
60 4
        $this->staticMethods = $filteredMethods;
61
62
        /**
63
        * @var string[] $filteredInterfaces
64
        */
65 4
        $filteredInterfaces = $this->FilterArrayOfInterfaces($interfaces);
66
67 4
        $this->interfaces = $filteredInterfaces;
68
69 4
        $this->autoReset = $autoReset;
70 4
    }
71
72 4
    public function Collect(string ...$implementations) : Generator
73
    {
74 4
        if ($this->autoReset) {
75 2
            $this->processedSources = [];
76 2
            $this->alreadyYielded = [];
77
        }
78
79 4
        yield from $this->CollectInterfaces(...$implementations);
80 4
    }
81
82 4
    protected function CollectInterfaces(string ...$implementations) : Generator
83
    {
84
        /**
85
        * @var string[] $interfaces
86
        */
87 4
        $interfaces = array_keys($this->staticMethods);
88 4
        foreach (array_filter($implementations, 'class_exists') as $implementation) {
89
            if (
90 4
                in_array($implementation, $this->processedSources, true) ||
91 4
                in_array($implementation, $this->alreadyYielded, true)
92
            ) {
93 2
                continue;
94
            }
95 4
            $this->processedSources[] = $implementation;
96
97
            /**
98
            * @var string $interface
99
            */
100 4
            foreach ($this->interfaces as $interface) {
101 4
                if (is_a($implementation, $interface, true)) {
102
                    yield $implementation;
103
                    $this->alreadyYielded[] = $implementation;
104 4
                    break;
105
                }
106
            }
107
108 4
            foreach ($interfaces as $interface) {
109 4
                if (is_a($implementation, $interface, true)) {
110
                    /**
111
                    * @var array<int, string> $types
112
                    */
113 4
                    foreach ($this->staticMethods[$interface] as $method => $types) {
114
                        /**
115
                        * @var iterable<string> $methodResult
116
                        */
117 4
                        $methodResult = $implementation::$method();
118
119
                        /**
120
                        * @var string $result
121
                        */
122 4
                        foreach ($methodResult as $result) {
123 4
                            if (in_array($result, $this->alreadyYielded, true)) {
124
                                continue;
125
                            }
126 4
                            foreach ($types as $type) {
127 4
                                if (is_a($result, $type, true)) {
128 4
                                    yield $result;
129 4
                                    $this->alreadyYielded[] = $result;
130 4
                                    continue;
131
                                }
132
                            }
133 4
                            foreach ($interfaces as $checkResultWithInterface) {
134 4
                                if (is_a($result, $checkResultWithInterface, true)) {
135 4
                                    yield from $this->CollectInterfaces($result);
136
                                }
137
                            }
138
                        }
139
                    }
140
                }
141
            }
142
        }
143 4
    }
144
145
    /**
146
    * @return string[]|array<string, mixed>
147
    */
148 4
    private function FilterArrayOfInterfaces(array $interfaces, int $flag = 0) : array
149
    {
150 4
        $strings = array_filter($interfaces, 'is_string', $flag);
151
152 4
        return array_filter($strings, 'interface_exists', $flag);
153
    }
154
155
    /**
156
    * @return array<string, array>
157
    */
158 4
    private function FilterArrayOfInterfaceOffsets(array $interfaces) : array
159
    {
160
        /**
161
        * @var array<string, array> $strings
162
        */
163 4
        $strings = $this->FilterArrayOfInterfaces($interfaces, ARRAY_FILTER_USE_KEY);
164
165 4
        return array_filter($strings, 'is_array');
166
    }
167
168 4
    private function MakeMethodFilter(string $interface) : Closure
169
    {
170
        return function (string $maybe) use ($interface) : bool {
171 4
            $ref = new ReflectionClass($interface);
172
173
            return
174 4
                $ref->hasMethod($maybe) &&
175 4
                $this->FilterReflectionMethod($ref->getMethod($maybe));
176 4
        };
177
    }
178
179 4
    private function FilterReflectionMethod(ReflectionMethod $refMethod) : bool
180
    {
181
        return
182 4
            $refMethod->isStatic() &&
183 4
            $refMethod->isPublic() &&
184 4
            0 === $refMethod->getNumberOfRequiredParameters() &&
185 4
            $this->FilterReflectionReturnType($refMethod->getReturnType());
186
    }
187
188 4
    private function FilterReflectionReturnType(? ReflectionType $refReturn) : bool
189
    {
190 4
        $refReturnName = is_null($refReturn) ? '' : $refReturn->__toString();
0 ignored issues
show
Deprecated Code introduced by
The function ReflectionType::__toString() has been deprecated: 7.1.0:8.0.0 Please use getName() ( Ignorable by Annotation )

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

190
        $refReturnName = is_null($refReturn) ? '' : /** @scrutinizer ignore-deprecated */ $refReturn->__toString();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
191
192 4
        return 'array' === $refReturnName || is_a($refReturnName, Traversable::class, true);
193
    }
194
195
    /**
196
    * @return array<string, string[]>
197
    */
198 4
    private function FilterMethods(string $interface, array $methods) : array
199
    {
200
        /**
201
        * @var array<string, string[]>
202
        */
203 4
        $filteredMethods = $this->FilterNonZeroArray(array_map(
204 4
            [$this, 'FilterArrayOfInterfaces'],
205 4
            array_filter(
206 4
                array_filter($methods, 'is_string', ARRAY_FILTER_USE_KEY),
207 4
                $this->MakeMethodFilter($interface),
208 4
                ARRAY_FILTER_USE_KEY
209
            )
210
        ));
211
212 4
        return $filteredMethods;
213
    }
214
215
    /**
216
    * @var array[]
217
    */
218 4
    private function FilterNonZeroArray(array $in) : array
219
    {
220 4
        return array_filter(
221 4
            $in,
222
            function (array $val) : bool {
223 4
                return count($val) > 0;
224 4
            }
225
        );
226
    }
227
}
228