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 ( 97f366...05dbda )
by SignpostMarv
02:37
created

StaticMethodCollector::shouldContainInterfaces()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

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

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