|
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 ReflectionNamedType; |
|
15
|
|
|
use ReflectionType; |
|
16
|
|
|
use Traversable; |
|
17
|
|
|
|
|
18
|
|
|
class StaticMethodCollector |
|
19
|
|
|
{ |
|
20
|
|
|
const DEFAULT_INT_ARRAY_FILTER_FLAG = 0; |
|
21
|
|
|
|
|
22
|
|
|
const DEFAULT_BOOL_AUTORESET = true; |
|
23
|
|
|
|
|
24
|
|
|
const INT_FILTER_NON_EMPTY_ARRAY = 0; |
|
25
|
|
|
|
|
26
|
|
|
const EXPECTED_NUMBER_OF_REQUIRED_PARAMETERS = 0; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var array<int, string> |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $processedSources = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string[] |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $alreadyYielded = []; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var bool |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $autoReset; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var array<string, array<string, array<int, string>>> |
|
45
|
|
|
*/ |
|
46
|
|
|
private $staticMethods = []; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string[] |
|
50
|
|
|
*/ |
|
51
|
|
|
private $interfaces = []; |
|
52
|
|
|
|
|
53
|
|
|
public function __construct( |
|
54
|
|
|
array $staticMethods, |
|
55
|
|
|
array $interfaces, |
|
56
|
|
|
bool $autoReset = self::DEFAULT_BOOL_AUTORESET |
|
57
|
|
|
) { |
|
58
|
|
|
$filteredMethods = []; |
|
59
|
|
|
|
|
60
|
|
|
foreach ($this->FilterArrayOfInterfaceOffsets($staticMethods) as $interface => $methods) { |
|
61
|
|
|
$filteredMethods[$interface] = $this->FilterMethods($interface, $methods); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
$this->staticMethods = $this->FilterNonZeroArray($filteredMethods); |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @var string[] |
|
68
|
|
|
*/ |
|
69
|
|
|
$filteredInterfaces = $this->FilterArrayOfInterfacesOrClasses($interfaces); |
|
70
|
|
|
|
|
71
|
|
|
$this->interfaces = $filteredInterfaces; |
|
72
|
|
|
|
|
73
|
|
|
$this->autoReset = $autoReset; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
4 |
|
public function Collect(string ...$implementations) : Generator |
|
77
|
|
|
{ |
|
78
|
4 |
|
if ($this->autoReset) { |
|
79
|
2 |
|
$this->processedSources = []; |
|
80
|
2 |
|
$this->alreadyYielded = []; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
4 |
|
yield from $this->CollectInterfaces(...$implementations); |
|
84
|
4 |
|
} |
|
85
|
|
|
|
|
86
|
8 |
|
protected function CollectInterfaces(string ...$implementations) : Generator |
|
87
|
|
|
{ |
|
88
|
|
|
foreach ( |
|
89
|
8 |
|
array_filter( |
|
90
|
8 |
|
$implementations, |
|
91
|
|
|
function (string $implementation) : bool { |
|
92
|
|
|
return |
|
93
|
8 |
|
! static::IsStringInArray($implementation, $this->processedSources); |
|
94
|
8 |
|
} |
|
95
|
|
|
) as $implementation |
|
96
|
|
|
) { |
|
97
|
8 |
|
$this->processedSources[] = $implementation; |
|
98
|
8 |
|
yield from $this->CollectInterfacesFromImplementationCheckInterfaces($implementation); |
|
99
|
8 |
|
yield from $this->CollectInterfacesFromImplementation($implementation); |
|
100
|
|
|
} |
|
101
|
8 |
|
} |
|
102
|
|
|
|
|
103
|
8 |
|
final protected function CollectInterfacesFromImplementationCheckInterfaces( |
|
104
|
|
|
string $implementation |
|
105
|
|
|
) : Generator { |
|
106
|
8 |
|
$checking = array_filter( |
|
107
|
8 |
|
$this->interfaces, |
|
108
|
|
|
function (string $interface) use ($implementation) : bool { |
|
109
|
8 |
|
return static::IsStringA($implementation, $interface); |
|
110
|
8 |
|
} |
|
111
|
|
|
); |
|
112
|
|
|
|
|
113
|
|
|
if ( |
|
114
|
8 |
|
count($checking) > self::INT_FILTER_NON_EMPTY_ARRAY && |
|
115
|
8 |
|
! static::IsStringInArray($implementation, $this->alreadyYielded) |
|
116
|
|
|
) { |
|
117
|
|
|
yield $implementation; |
|
118
|
|
|
$this->alreadyYielded[] = $implementation; |
|
119
|
|
|
} |
|
120
|
8 |
|
} |
|
121
|
|
|
|
|
122
|
8 |
|
final protected function CollectInterfacesFromImplementation(string $implementation) : Generator |
|
123
|
|
|
{ |
|
124
|
8 |
|
$interfaces = array_keys($this->staticMethods); |
|
125
|
|
|
|
|
126
|
8 |
|
foreach ($this->FilterIsA($implementation, $interfaces) as $interface) { |
|
127
|
8 |
|
foreach ($this->staticMethods[$interface] as $method => $types) { |
|
128
|
8 |
|
yield from $this->CollectInterfacesFromImplementationTypes( |
|
129
|
8 |
|
$implementation, |
|
130
|
8 |
|
$method, |
|
131
|
8 |
|
$types |
|
132
|
|
|
); |
|
133
|
|
|
} |
|
134
|
|
|
} |
|
135
|
8 |
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param array<int, string> $types |
|
139
|
|
|
*/ |
|
140
|
8 |
|
final protected function CollectInterfacesFromImplementationTypes( |
|
141
|
|
|
string $implementation, |
|
142
|
|
|
string $method, |
|
143
|
|
|
array $types |
|
144
|
|
|
) : Generator { |
|
145
|
|
|
/** |
|
146
|
|
|
* @var iterable<string> |
|
147
|
|
|
*/ |
|
148
|
8 |
|
$methodResult = $implementation::$method(); |
|
149
|
|
|
|
|
150
|
8 |
|
foreach ($methodResult as $result) { |
|
151
|
8 |
|
if (static::IsStringInArray($result, $this->alreadyYielded)) { |
|
152
|
2 |
|
continue; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
8 |
|
foreach ($this->FilterIsA($result, $types) as $type) { |
|
156
|
8 |
|
yield $result; |
|
157
|
8 |
|
$this->alreadyYielded[] = $result; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
8 |
|
yield from $this->CollectInterfaces($result); |
|
161
|
|
|
} |
|
162
|
8 |
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* @param array<int, string> $interfaces |
|
166
|
|
|
* |
|
167
|
|
|
* @return array<int, string> |
|
168
|
|
|
*/ |
|
169
|
8 |
|
final protected function FilterIsA(string $implementation, array $interfaces) : array |
|
170
|
|
|
{ |
|
171
|
|
|
/** |
|
172
|
|
|
* @var array<int, string> |
|
173
|
|
|
*/ |
|
174
|
|
|
$out = array_filter($interfaces, function (string $interface) use ($implementation) : bool { |
|
175
|
8 |
|
return static::IsStringA($implementation, $interface); |
|
176
|
8 |
|
}); |
|
177
|
|
|
|
|
178
|
8 |
|
return $out; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
/** |
|
182
|
|
|
* @return string[]|array<string, mixed> |
|
183
|
|
|
*/ |
|
184
|
|
|
final protected function FilterArrayOfInterfaces( |
|
185
|
|
|
array $interfaces, |
|
186
|
|
|
int $flag = self::DEFAULT_INT_ARRAY_FILTER_FLAG |
|
187
|
|
|
) : array { |
|
188
|
|
|
$strings = array_filter($interfaces, 'is_string', $flag); |
|
189
|
|
|
|
|
190
|
|
|
return array_filter($strings, 'interface_exists', $flag); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* @return string[] |
|
195
|
|
|
*/ |
|
196
|
|
|
final protected function FilterArrayOfInterfacesOrClasses(array $interfaces) : array |
|
197
|
|
|
{ |
|
198
|
|
|
/** |
|
199
|
|
|
* @var string[] |
|
200
|
|
|
*/ |
|
201
|
|
|
$strings = array_filter($interfaces, 'is_string'); |
|
202
|
|
|
|
|
203
|
|
|
return array_filter($strings, function (string $maybe) : bool { |
|
204
|
|
|
return interface_exists($maybe) || class_exists($maybe); |
|
205
|
|
|
}); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* @return array<string, array> |
|
210
|
|
|
*/ |
|
211
|
|
|
final protected function FilterArrayOfInterfaceOffsets(array $interfaces) : array |
|
212
|
|
|
{ |
|
213
|
|
|
/** |
|
214
|
|
|
* @var array<string, array> |
|
215
|
|
|
*/ |
|
216
|
|
|
$strings = $this->FilterArrayOfInterfaces($interfaces, ARRAY_FILTER_USE_KEY); |
|
217
|
|
|
|
|
218
|
|
|
return array_filter($strings, 'is_array'); |
|
219
|
|
|
} |
|
220
|
|
|
|
|
221
|
|
|
final protected function MakeMethodFilter(string $interface) : Closure |
|
222
|
|
|
{ |
|
223
|
|
|
return function (string $maybe) use ($interface) : bool { |
|
224
|
|
|
$ref = new ReflectionClass($interface); |
|
225
|
|
|
|
|
226
|
|
|
return |
|
227
|
|
|
$ref->hasMethod($maybe) && |
|
228
|
|
|
$this->FilterReflectionMethod($ref->getMethod($maybe)); |
|
229
|
|
|
}; |
|
230
|
|
|
} |
|
231
|
|
|
|
|
232
|
|
|
final protected function FilterReflectionMethod(ReflectionMethod $refMethod) : bool |
|
233
|
|
|
{ |
|
234
|
|
|
return |
|
235
|
|
|
$refMethod->isStatic() && |
|
236
|
|
|
$refMethod->isPublic() && |
|
237
|
|
|
self::EXPECTED_NUMBER_OF_REQUIRED_PARAMETERS === $refMethod->getNumberOfRequiredParameters() && |
|
238
|
|
|
$this->FilterReflectionReturnType($refMethod->getReturnType()); |
|
239
|
|
|
} |
|
240
|
|
|
|
|
241
|
|
|
final protected function FilterReflectionReturnType(? ReflectionType $refReturn) : bool |
|
242
|
|
|
{ |
|
243
|
|
|
$refReturnName = ($refReturn instanceof ReflectionNamedType) ? $refReturn->getName() : ''; |
|
244
|
|
|
|
|
245
|
|
|
return 'array' === $refReturnName || static::IsStringA($refReturnName, Traversable::class); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
/** |
|
249
|
|
|
* @return array<string, string[]> |
|
250
|
|
|
*/ |
|
251
|
|
|
final protected function FilterMethods(string $interface, array $methods) : array |
|
252
|
|
|
{ |
|
253
|
|
|
/** |
|
254
|
|
|
* @var array<string, string[]> |
|
255
|
|
|
*/ |
|
256
|
|
|
$filteredMethods = $this->FilterNonZeroArray(array_map( |
|
257
|
|
|
[$this, 'FilterArrayOfInterfacesOrClasses'], |
|
258
|
|
|
array_filter( |
|
259
|
|
|
array_filter($methods, 'is_string', ARRAY_FILTER_USE_KEY), |
|
260
|
|
|
$this->MakeMethodFilter($interface), |
|
261
|
|
|
ARRAY_FILTER_USE_KEY |
|
262
|
|
|
) |
|
263
|
|
|
)); |
|
264
|
|
|
|
|
265
|
|
|
return $filteredMethods; |
|
266
|
|
|
} |
|
267
|
|
|
|
|
268
|
|
|
/** |
|
269
|
|
|
* @return array<string, array<string, array<int, string>>> |
|
270
|
|
|
*/ |
|
271
|
|
|
final protected function FilterNonZeroArray(array $in) : array |
|
272
|
|
|
{ |
|
273
|
|
|
/** |
|
274
|
|
|
* @var array<string, array<string, array<int, string>>> |
|
275
|
|
|
*/ |
|
276
|
|
|
$out = array_filter( |
|
277
|
|
|
$in, |
|
278
|
|
|
function (array $val) : bool { |
|
279
|
|
|
return count($val) > self::INT_FILTER_NON_EMPTY_ARRAY; |
|
280
|
|
|
} |
|
281
|
|
|
); |
|
282
|
|
|
|
|
283
|
|
|
return $out; |
|
284
|
|
|
} |
|
285
|
|
|
|
|
286
|
8 |
|
protected static function IsStringInArray(string $maybe, array $array) : bool |
|
287
|
|
|
{ |
|
288
|
8 |
|
return in_array($maybe, $array, true); |
|
289
|
|
|
} |
|
290
|
|
|
|
|
291
|
8 |
|
protected static function IsStringA(string $maybe, string $thing) : bool |
|
292
|
|
|
{ |
|
293
|
8 |
|
return is_a($maybe, $thing, true); |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
|