|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
/** |
|
5
|
|
|
* @author SignpostMarv |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
namespace SignpostMarv\DaftInterfaceCollector; |
|
9
|
|
|
|
|
10
|
|
|
use Generator; |
|
11
|
|
|
use ReflectionClass; |
|
12
|
|
|
use ReflectionMethod; |
|
13
|
|
|
use Traversable; |
|
14
|
|
|
|
|
15
|
|
|
class StaticMethodCollector |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var array<string, array<int, string>> |
|
19
|
|
|
*/ |
|
20
|
|
|
private $staticMethods; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string[] |
|
24
|
|
|
*/ |
|
25
|
|
|
private $interfaces = []; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var array<int, string> |
|
29
|
|
|
*/ |
|
30
|
|
|
private $processedSources = []; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var string[] |
|
34
|
|
|
*/ |
|
35
|
|
|
private $alreadyYielded = []; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var bool |
|
39
|
|
|
*/ |
|
40
|
|
|
private $autoResetProcessedSources; |
|
41
|
|
|
|
|
42
|
4 |
|
public function __construct( |
|
43
|
|
|
array $staticMethods, |
|
44
|
|
|
array $interfaces, |
|
45
|
|
|
bool $autoResetProcessedSources = true |
|
46
|
|
|
) { |
|
47
|
4 |
|
$staticMethods = array_filter( |
|
48
|
4 |
|
$staticMethods, |
|
49
|
|
|
/** |
|
50
|
|
|
* @param mixed $maybe |
|
51
|
|
|
*/ |
|
52
|
|
|
function ($maybe) : bool { |
|
53
|
4 |
|
return is_string($maybe) && interface_exists($maybe); |
|
54
|
4 |
|
}, |
|
55
|
4 |
|
ARRAY_FILTER_USE_KEY |
|
56
|
|
|
); |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var array<string, array<int, string>> $filtered |
|
60
|
|
|
*/ |
|
61
|
4 |
|
$filtered = []; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @var string $interface |
|
65
|
|
|
* @var array<string, array<int, string>> $methods |
|
66
|
|
|
*/ |
|
67
|
4 |
|
foreach ($staticMethods as $interface => $methods) { |
|
68
|
4 |
|
$ref = new ReflectionClass($interface); |
|
69
|
|
|
|
|
70
|
4 |
|
$filtered[$interface] = $this->FilterMethods($ref, $methods); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @var array<string, array<int, string>> $filtered |
|
75
|
|
|
*/ |
|
76
|
|
|
$filtered = array_filter($filtered, function (array $methods) : bool { |
|
77
|
4 |
|
return count($methods) > 0; |
|
78
|
4 |
|
}); |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @var string[] $filteredInterfaces |
|
82
|
|
|
*/ |
|
83
|
4 |
|
$filteredInterfaces = array_filter( |
|
84
|
4 |
|
$interfaces, |
|
85
|
|
|
/** |
|
86
|
|
|
* @param mixed $maybe |
|
87
|
|
|
*/ |
|
88
|
|
|
function ($maybe) : bool { |
|
89
|
4 |
|
return is_string($maybe) && interface_exists($maybe); |
|
90
|
4 |
|
} |
|
91
|
|
|
); |
|
92
|
|
|
|
|
93
|
4 |
|
$this->interfaces = $filteredInterfaces; |
|
94
|
|
|
|
|
95
|
4 |
|
$this->staticMethods = $filtered; |
|
96
|
4 |
|
$this->autoResetProcessedSources = $autoResetProcessedSources; |
|
97
|
4 |
|
} |
|
98
|
|
|
|
|
99
|
4 |
|
public function Collect(string ...$implementations) : Generator |
|
100
|
|
|
{ |
|
101
|
4 |
|
if ($this->autoResetProcessedSources) { |
|
102
|
2 |
|
$this->processedSources = []; |
|
103
|
2 |
|
$this->alreadyYielded = []; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
4 |
|
if ($this->autoResetProcessedSources) { |
|
107
|
2 |
|
$this->processedSources = []; |
|
108
|
2 |
|
$this->alreadyYielded = []; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
4 |
|
yield from $this->CollectInterfaces(...$implementations); |
|
112
|
4 |
|
} |
|
113
|
|
|
|
|
114
|
4 |
|
protected function CollectInterfaces(string ...$implementations) : Generator |
|
115
|
|
|
{ |
|
116
|
|
|
/** |
|
117
|
|
|
* @var string[] $interfaces |
|
118
|
|
|
*/ |
|
119
|
4 |
|
$interfaces = array_keys($this->staticMethods); |
|
120
|
4 |
|
foreach (array_filter($implementations, 'class_exists') as $implementation) { |
|
121
|
|
|
if ( |
|
122
|
4 |
|
in_array($implementation, $this->processedSources, true) || |
|
123
|
4 |
|
in_array($implementation, $this->alreadyYielded, true) |
|
124
|
|
|
) { |
|
125
|
2 |
|
continue; |
|
126
|
|
|
} |
|
127
|
4 |
|
$this->processedSources[] = $implementation; |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @var string $interface |
|
131
|
|
|
*/ |
|
132
|
4 |
|
foreach ($this->interfaces as $interface) { |
|
133
|
4 |
|
if (is_a($implementation, $interface, true)) { |
|
134
|
|
|
yield $implementation; |
|
135
|
|
|
$this->alreadyYielded[] = $implementation; |
|
136
|
4 |
|
break; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
4 |
|
foreach ($interfaces as $interface) { |
|
141
|
4 |
|
if (is_a($implementation, $interface, true)) { |
|
142
|
|
|
/** |
|
143
|
|
|
* @var array<int, string> $types |
|
144
|
|
|
*/ |
|
145
|
4 |
|
foreach ($this->staticMethods[$interface] as $method => $types) { |
|
146
|
|
|
/** |
|
147
|
|
|
* @var iterable<string> $methodResult |
|
148
|
|
|
*/ |
|
149
|
4 |
|
$methodResult = $implementation::$method(); |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @var string $result |
|
153
|
|
|
*/ |
|
154
|
4 |
|
foreach ($methodResult as $result) { |
|
155
|
4 |
|
if (in_array($result, $this->alreadyYielded, true)) { |
|
156
|
|
|
continue; |
|
157
|
|
|
} |
|
158
|
4 |
|
foreach ($types as $type) { |
|
|
|
|
|
|
159
|
4 |
|
if (is_a($result, $type, true)) { |
|
160
|
4 |
|
yield $result; |
|
161
|
4 |
|
$this->alreadyYielded[] = $result; |
|
162
|
4 |
|
continue; |
|
163
|
|
|
} |
|
164
|
|
|
} |
|
165
|
4 |
|
foreach ($interfaces as $checkResultWithInterface) { |
|
166
|
4 |
|
if (is_a($result, $checkResultWithInterface, true)) { |
|
167
|
4 |
|
yield from $this->CollectInterfaces($result); |
|
168
|
|
|
} |
|
169
|
|
|
} |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
4 |
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* @param mixed $maybe |
|
179
|
|
|
*/ |
|
180
|
|
|
protected function shouldContainInterfaces($maybe) : bool |
|
181
|
|
|
{ |
|
182
|
|
|
return is_string($maybe) && interface_exists($maybe); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @param array<string, array<int, string>> $methods |
|
187
|
|
|
*/ |
|
188
|
4 |
|
private function FilterMethods(ReflectionClass $ref, array $methods) : array |
|
189
|
|
|
{ |
|
190
|
4 |
|
$methods = array_filter( |
|
191
|
4 |
|
$methods, |
|
192
|
|
|
/** |
|
193
|
|
|
* @param mixed $maybe |
|
194
|
|
|
*/ |
|
195
|
|
|
function ($maybe) use ($ref) : bool { |
|
196
|
4 |
|
if (is_string($maybe) && $ref->hasMethod($maybe)) { |
|
197
|
|
|
/** |
|
198
|
|
|
* @var ReflectionMethod $refMethod |
|
199
|
|
|
*/ |
|
200
|
4 |
|
$refMethod = $ref->getMethod($maybe); |
|
201
|
|
|
|
|
202
|
|
|
if ( |
|
203
|
4 |
|
$refMethod->isStatic() && |
|
204
|
4 |
|
$refMethod->isPublic() && |
|
205
|
4 |
|
0 === $refMethod->getNumberOfRequiredParameters() && |
|
206
|
4 |
|
$refMethod->hasReturnType() |
|
207
|
|
|
) { |
|
208
|
|
|
/** |
|
209
|
|
|
* @var \ReflectionType $refReturn |
|
210
|
|
|
*/ |
|
211
|
4 |
|
$refReturn = $refMethod->getReturnType(); |
|
212
|
|
|
|
|
213
|
|
|
return |
|
214
|
4 |
|
'array' === $refReturn->__toString() || |
|
|
|
|
|
|
215
|
4 |
|
is_a((string) $refReturn->__toString(), Traversable::class, true); |
|
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
return false; |
|
220
|
4 |
|
}, |
|
221
|
4 |
|
ARRAY_FILTER_USE_KEY |
|
222
|
|
|
); |
|
223
|
|
|
|
|
224
|
4 |
|
return array_map( |
|
225
|
|
|
function (array $methodInterfaces) : array { |
|
226
|
4 |
|
return array_filter( |
|
227
|
4 |
|
$methodInterfaces, |
|
228
|
|
|
/** |
|
229
|
|
|
* @param mixed $maybe |
|
230
|
|
|
*/ |
|
231
|
|
|
function ($maybe) : bool { |
|
232
|
4 |
|
return is_string($maybe) && interface_exists($maybe); |
|
233
|
4 |
|
} |
|
234
|
|
|
); |
|
235
|
4 |
|
}, |
|
236
|
4 |
|
$methods |
|
237
|
|
|
); |
|
238
|
|
|
} |
|
239
|
|
|
} |
|
240
|
|
|
|