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 ( 05dbda...19d6e5 )
by SignpostMarv
02:58
created

StaticMethodCollector   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 190
Duplicated Lines 0 %

Test Coverage

Coverage 95.71%

Importance

Changes 0
Metric Value
dl 0
loc 190
ccs 67
cts 70
cp 0.9571
rs 9.3999
c 0
b 0
f 0
wmc 33

8 Methods

Rating   Name   Duplication   Size   Complexity  
A Collect() 0 8 2
A FilterArrayOfInterfaces() 0 5 1
A FilterMethods() 0 19 3
C CollectInterfaces() 0 54 15
A MakeMethodFilter() 0 6 2
A FilterReflectionReturnType() 0 5 3
B __construct() 0 29 3
A FilterReflectionMethod() 0 7 4
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, array<int, 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 $autoResetProcessedSources;
43
44 4
    public function __construct(
45
        array $staticMethods,
46
        array $interfaces,
47
        bool $autoResetProcessedSources = true
48
    ) {
49
        /**
50
        * @var string $interface
51
        * @var array<string, array<int, string>> $methods
52
        */
53
        foreach (
54 4
            $this->FilterArrayOfInterfaces(
55 4
                $staticMethods,
56 4
                ARRAY_FILTER_USE_KEY
57
            ) as $interface => $methods
58
        ) {
59 4
            $filteredMethods = $this->FilterMethods(new ReflectionClass($interface), $methods);
60 4
            if (count($filteredMethods) > 0) {
61 4
                $this->staticMethods[$interface] = $filteredMethods;
62
            }
63
        }
64
65
        /**
66
        * @var string[] $filteredInterfaces
67
        */
68 4
        $filteredInterfaces = $this->FilterArrayOfInterfaces($interfaces);
69
70 4
        $this->interfaces = $filteredInterfaces;
71
72 4
        $this->autoResetProcessedSources = $autoResetProcessedSources;
73 4
    }
74
75 4
    public function Collect(string ...$implementations) : Generator
76
    {
77 4
        if ($this->autoResetProcessedSources) {
78 2
            $this->processedSources = [];
79 2
            $this->alreadyYielded = [];
80
        }
81
82 4
        yield from $this->CollectInterfaces(...$implementations);
83 4
    }
84
85 4
    protected function CollectInterfaces(string ...$implementations) : Generator
86
    {
87
        /**
88
        * @var string[] $interfaces
89
        */
90 4
        $interfaces = array_keys($this->staticMethods);
91 4
        foreach (array_filter($implementations, 'class_exists') as $implementation) {
92
            if (
93 4
                in_array($implementation, $this->processedSources, true) ||
94 4
                in_array($implementation, $this->alreadyYielded, true)
95
            ) {
96 2
                continue;
97
            }
98 4
            $this->processedSources[] = $implementation;
99
100
            /**
101
            * @var string $interface
102
            */
103 4
            foreach ($this->interfaces as $interface) {
104 4
                if (is_a($implementation, $interface, true)) {
105
                    yield $implementation;
106
                    $this->alreadyYielded[] = $implementation;
107 4
                    break;
108
                }
109
            }
110
111 4
            foreach ($interfaces as $interface) {
112 4
                if (is_a($implementation, $interface, true)) {
113
                    /**
114
                    * @var array<int, string> $types
115
                    */
116 4
                    foreach ($this->staticMethods[$interface] as $method => $types) {
117
                        /**
118
                        * @var iterable<string> $methodResult
119
                        */
120 4
                        $methodResult = $implementation::$method();
121
122
                        /**
123
                        * @var string $result
124
                        */
125 4
                        foreach ($methodResult as $result) {
126 4
                            if (in_array($result, $this->alreadyYielded, true)) {
127
                                continue;
128
                            }
129 4
                            foreach ($types as $type) {
130 4
                                if (is_a($result, $type, true)) {
131 4
                                    yield $result;
132 4
                                    $this->alreadyYielded[] = $result;
133 4
                                    continue;
134
                                }
135
                            }
136 4
                            foreach ($interfaces as $checkResultWithInterface) {
137 4
                                if (is_a($result, $checkResultWithInterface, true)) {
138 4
                                    yield from $this->CollectInterfaces($result);
139
                                }
140
                            }
141
                        }
142
                    }
143
                }
144
            }
145
        }
146 4
    }
147
148
    /**
149
    * @return string[]|array<string, mixed>
150
    */
151 4
    private function FilterArrayOfInterfaces(array $interfaces, int $flag = 0) : array
152
    {
153 4
        $strings = array_filter($interfaces, 'is_string', $flag);
154
155 4
        return array_filter($strings, 'interface_exists', $flag);
156
    }
157
158 4
    private function MakeMethodFilter(ReflectionClass $ref) : Closure
159
    {
160
        return function (string $maybe) use ($ref) : bool {
161
            return
162 4
                $ref->hasMethod($maybe) &&
163 4
                $this->FilterReflectionMethod($ref->getMethod($maybe));
164 4
        };
165
    }
166
167 4
    private function FilterReflectionMethod(ReflectionMethod $refMethod) : bool
168
    {
169
        return
170 4
            $refMethod->isStatic() &&
171 4
            $refMethod->isPublic() &&
172 4
            0 === $refMethod->getNumberOfRequiredParameters() &&
173 4
            $this->FilterReflectionReturnType($refMethod->getReturnType());
174
    }
175
176 4
    private function FilterReflectionReturnType(? ReflectionType $refReturn) : bool
177
    {
178 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

178
        $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...
179
180 4
        return 'array' === $refReturnName || is_a($refReturnName, Traversable::class, true);
181
    }
182
183
    /**
184
    * @param array<string, array<int, string>> $methods
185
    *
186
    * @return array<string, array<int, string>>
187
    */
188 4
    private function FilterMethods(ReflectionClass $ref, array $methods) : array
189
    {
190 4
        $filteredMethods = [];
191
192
        foreach (
193 4
            array_filter(
194 4
                array_filter($methods, 'is_string', ARRAY_FILTER_USE_KEY),
195 4
                $this->MakeMethodFilter($ref),
196 4
                ARRAY_FILTER_USE_KEY
197
            ) as $method => $interfaces
198
        ) {
199 4
            $methodInterfaces = $this->FilterArrayOfInterfaces($interfaces);
200
201 4
            if (count($methodInterfaces) > 0) {
202 4
                $filteredMethods[$method] = $interfaces;
203
            }
204
        }
205
206 4
        return $filteredMethods;
207
    }
208
}
209