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 ( da8116...871f09 )
by SignpostMarv
02:31
created

StaticMethodCollector::FilterNonZeroArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

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

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