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 ( 7b7813...fbc2a3 )
by SignpostMarv
02:51
created

FilterArrayOfInterfaceOffsets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 = $this->FilterArrayOfInterfaceOffsets(
50 4
            array_filter($staticMethods, 'is_array')
51
        );
52
53 4
        foreach ($filteredMethods as $interface => $methods) {
54 4
            $filteredMethods[$interface] = $this->FilterMethods($interface, $methods);
55
        }
56
57
        /**
58
        * @var array<string, array<string, string[]>> $filteredMethods
59
        */
60 4
        $filteredMethods = $this->FilterNonZeroArray($filteredMethods);
61
62 4
        $this->staticMethods = $filteredMethods;
63
64
        /**
65
        * @var string[] $filteredInterfaces
66
        */
67 4
        $filteredInterfaces = $this->FilterArrayOfInterfaces($interfaces);
68
69 4
        $this->interfaces = $filteredInterfaces;
70
71 4
        $this->autoReset = $autoReset;
72 4
    }
73
74 4
    public function Collect(string ...$implementations) : Generator
75
    {
76 4
        if ($this->autoReset) {
77 2
            $this->processedSources = [];
78 2
            $this->alreadyYielded = [];
79
        }
80
81 4
        yield from $this->CollectInterfaces(...$implementations);
82 4
    }
83
84 4
    protected function CollectInterfaces(string ...$implementations) : Generator
85
    {
86
        /**
87
        * @var string[] $interfaces
88
        */
89 4
        $interfaces = array_keys($this->staticMethods);
90 4
        foreach (array_filter($implementations, 'class_exists') as $implementation) {
91
            if (
92 4
                in_array($implementation, $this->processedSources, true) ||
93 4
                in_array($implementation, $this->alreadyYielded, true)
94
            ) {
95 2
                continue;
96
            }
97 4
            $this->processedSources[] = $implementation;
98
99
            /**
100
            * @var string $interface
101
            */
102 4
            foreach ($this->interfaces as $interface) {
103 4
                if (is_a($implementation, $interface, true)) {
104
                    yield $implementation;
105
                    $this->alreadyYielded[] = $implementation;
106 4
                    break;
107
                }
108
            }
109
110 4
            foreach ($interfaces as $interface) {
111 4
                if (is_a($implementation, $interface, true)) {
112
                    /**
113
                    * @var array<int, string> $types
114
                    */
115 4
                    foreach ($this->staticMethods[$interface] as $method => $types) {
116
                        /**
117
                        * @var iterable<string> $methodResult
118
                        */
119 4
                        $methodResult = $implementation::$method();
120
121
                        /**
122
                        * @var string $result
123
                        */
124 4
                        foreach ($methodResult as $result) {
125 4
                            if (in_array($result, $this->alreadyYielded, true)) {
126
                                continue;
127
                            }
128 4
                            foreach ($types as $type) {
129 4
                                if (is_a($result, $type, true)) {
130 4
                                    yield $result;
131 4
                                    $this->alreadyYielded[] = $result;
132 4
                                    continue;
133
                                }
134
                            }
135 4
                            foreach ($interfaces as $checkResultWithInterface) {
136 4
                                if (is_a($result, $checkResultWithInterface, true)) {
137 4
                                    yield from $this->CollectInterfaces($result);
138
                                }
139
                            }
140
                        }
141
                    }
142
                }
143
            }
144
        }
145 4
    }
146
147
    /**
148
    * @return string[]|array<string, mixed>
149
    */
150 4
    private function FilterArrayOfInterfaces(array $interfaces, int $flag = 0) : array
151
    {
152 4
        $strings = array_filter($interfaces, 'is_string', $flag);
153
154 4
        return array_filter($strings, 'interface_exists', $flag);
155
    }
156
157
    /**
158
    * @return array<string, mixed>
159
    */
160 4
    private function FilterArrayOfInterfaceOffsets(array $interfaces) : array
161
    {
162
        /**
163
        * @var array<string, mixed> $strings
164
        */
165 4
        $strings = $this->FilterArrayOfInterfaces($interfaces, ARRAY_FILTER_USE_KEY);
166
167 4
        return $strings;
168
    }
169
170 4
    private function MakeMethodFilter(string $interface) : Closure
171
    {
172
        return function (string $maybe) use ($interface) : bool {
173 4
            $ref = new ReflectionClass($interface);
174
175
            return
176 4
                $ref->hasMethod($maybe) &&
177 4
                $this->FilterReflectionMethod($ref->getMethod($maybe));
178 4
        };
179
    }
180
181 4
    private function FilterReflectionMethod(ReflectionMethod $refMethod) : bool
182
    {
183
        return
184 4
            $refMethod->isStatic() &&
185 4
            $refMethod->isPublic() &&
186 4
            0 === $refMethod->getNumberOfRequiredParameters() &&
187 4
            $this->FilterReflectionReturnType($refMethod->getReturnType());
188
    }
189
190 4
    private function FilterReflectionReturnType(? ReflectionType $refReturn) : bool
191
    {
192 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

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