forked from vimeo/psalm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
EventDispatcher.php
415 lines (350 loc) · 13 KB
/
EventDispatcher.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
<?php
namespace Psalm\Internal;
use Psalm\Plugin\EventHandler\AddTaintsInterface;
use Psalm\Plugin\EventHandler\AfterAnalysisInterface;
use Psalm\Plugin\EventHandler\AfterClassLikeAnalysisInterface;
use Psalm\Plugin\EventHandler\AfterClassLikeExistenceCheckInterface;
use Psalm\Plugin\EventHandler\AfterClassLikeVisitInterface;
use Psalm\Plugin\EventHandler\AfterCodebasePopulatedInterface;
use Psalm\Plugin\EventHandler\AfterEveryFunctionCallAnalysisInterface;
use Psalm\Plugin\EventHandler\AfterExpressionAnalysisInterface;
use Psalm\Plugin\EventHandler\AfterFileAnalysisInterface;
use Psalm\Plugin\EventHandler\AfterFunctionCallAnalysisInterface;
use Psalm\Plugin\EventHandler\AfterFunctionLikeAnalysisInterface;
use Psalm\Plugin\EventHandler\AfterMethodCallAnalysisInterface;
use Psalm\Plugin\EventHandler\AfterStatementAnalysisInterface;
use Psalm\Plugin\EventHandler\BeforeAddIssueInterface;
use Psalm\Plugin\EventHandler\BeforeFileAnalysisInterface;
use Psalm\Plugin\EventHandler\Event\AddRemoveTaintsEvent;
use Psalm\Plugin\EventHandler\Event\AfterAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\AfterClassLikeAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\AfterClassLikeExistenceCheckEvent;
use Psalm\Plugin\EventHandler\Event\AfterClassLikeVisitEvent;
use Psalm\Plugin\EventHandler\Event\AfterCodebasePopulatedEvent;
use Psalm\Plugin\EventHandler\Event\AfterEveryFunctionCallAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\AfterExpressionAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\AfterFileAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\AfterFunctionCallAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\AfterFunctionLikeAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\AfterMethodCallAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\AfterStatementAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\BeforeAddIssueEvent;
use Psalm\Plugin\EventHandler\Event\BeforeFileAnalysisEvent;
use Psalm\Plugin\EventHandler\Event\StringInterpreterEvent;
use Psalm\Plugin\EventHandler\RemoveTaintsInterface;
use Psalm\Plugin\EventHandler\StringInterpreterInterface;
use Psalm\Type\Atomic\TLiteralString;
use function array_merge;
use function count;
use function is_bool;
use function is_subclass_of;
/**
* @internal
*/
class EventDispatcher
{
/**
* Static methods to be called after method checks have completed
*
* @var list<class-string<AfterMethodCallAnalysisInterface>>
*/
private $after_method_checks = [];
/**
* Static methods to be called after project function checks have completed
*
* Called after function calls to functions defined in the project.
*
* Allows influencing the return type and adding of modifications.
*
* @var list<class-string<AfterFunctionCallAnalysisInterface>>
*/
public $after_function_checks = [];
/**
* Static methods to be called after every function call
*
* Called after each function call, including php internal functions.
*
* Cannot change the call or influence its return type
*
* @var list<class-string<AfterEveryFunctionCallAnalysisInterface>>
*/
public $after_every_function_checks = [];
/**
* Static methods to be called after expression checks have completed
*
* @var list<class-string<AfterExpressionAnalysisInterface>>
*/
public $after_expression_checks = [];
/**
* Static methods to be called after statement checks have completed
*
* @var list<class-string<AfterStatementAnalysisInterface>>
*/
public $after_statement_checks = [];
/**
* Static methods to be called after method checks have completed
*
* @var list<class-string<StringInterpreterInterface>>
*/
public $string_interpreters = [];
/**
* Static methods to be called after classlike exists checks have completed
*
* @var list<class-string<AfterClassLikeExistenceCheckInterface>>
*/
public $after_classlike_exists_checks = [];
/**
* Static methods to be called after classlike checks have completed
*
* @var list<class-string<AfterClassLikeAnalysisInterface>>
*/
public $after_classlike_checks = [];
/**
* Static methods to be called after classlikes have been scanned
*
* @var list<class-string<AfterClassLikeVisitInterface>>
*/
private $after_visit_classlikes = [];
/**
* Static methods to be called after codebase has been populated
*
* @var list<class-string<AfterCodebasePopulatedInterface>>
*/
public $after_codebase_populated = [];
/**
* @var list<class-string<BeforeAddIssueInterface>>
*/
private array $before_add_issue = [];
/**
* Static methods to be called after codebase has been populated
*
* @var list<class-string<AfterAnalysisInterface>>
*/
public $after_analysis = [];
/**
* Static methods to be called after a file has been analyzed
*
* @var list<class-string<AfterFileAnalysisInterface>>
*/
public $after_file_checks = [];
/**
* Static methods to be called before a file is analyzed
*
* @var list<class-string<BeforeFileAnalysisInterface>>
*/
public $before_file_checks = [];
/**
* Static methods to be called after functionlike checks have completed
*
* @var list<class-string<AfterFunctionLikeAnalysisInterface>>
*/
public $after_functionlike_checks = [];
/**
* Static methods to be called to see if taints should be added
*
* @var list<class-string<AddTaintsInterface>>
*/
public $add_taints_checks = [];
/**
* Static methods to be called to see if taints should be removed
*
* @var list<class-string<RemoveTaintsInterface>>
*/
public $remove_taints_checks = [];
/**
* @param class-string $class
*/
public function registerClass(string $class): void
{
if (is_subclass_of($class, AfterMethodCallAnalysisInterface::class)) {
$this->after_method_checks[] = $class;
}
if (is_subclass_of($class, AfterFunctionCallAnalysisInterface::class)) {
$this->after_function_checks[] = $class;
}
if (is_subclass_of($class, AfterEveryFunctionCallAnalysisInterface::class)) {
$this->after_every_function_checks[] = $class;
}
if (is_subclass_of($class, AfterExpressionAnalysisInterface::class)) {
$this->after_expression_checks[] = $class;
}
if (is_subclass_of($class, AfterStatementAnalysisInterface::class)) {
$this->after_statement_checks[] = $class;
}
if (is_subclass_of($class, StringInterpreterInterface::class)) {
$this->string_interpreters[] = $class;
}
if (is_subclass_of($class, AfterClassLikeExistenceCheckInterface::class)) {
$this->after_classlike_exists_checks[] = $class;
}
if (is_subclass_of($class, AfterClassLikeAnalysisInterface::class)) {
$this->after_classlike_checks[] = $class;
}
if (is_subclass_of($class, AfterClassLikeVisitInterface::class)) {
$this->after_visit_classlikes[] = $class;
}
if (is_subclass_of($class, AfterCodebasePopulatedInterface::class)) {
$this->after_codebase_populated[] = $class;
}
if (is_subclass_of($class, BeforeAddIssueInterface::class)) {
$this->before_add_issue[] = $class;
}
if (is_subclass_of($class, AfterAnalysisInterface::class)) {
$this->after_analysis[] = $class;
}
if (is_subclass_of($class, AfterFileAnalysisInterface::class)) {
$this->after_file_checks[] = $class;
}
if (is_subclass_of($class, BeforeFileAnalysisInterface::class)) {
$this->before_file_checks[] = $class;
}
if (is_subclass_of($class, AfterFunctionLikeAnalysisInterface::class)) {
$this->after_functionlike_checks[] = $class;
}
if (is_subclass_of($class, AddTaintsInterface::class)) {
$this->add_taints_checks[] = $class;
}
if (is_subclass_of($class, RemoveTaintsInterface::class)) {
$this->remove_taints_checks[] = $class;
}
}
public function hasAfterMethodCallAnalysisHandlers(): bool
{
return count($this->after_method_checks) > 0;
}
public function dispatchAfterMethodCallAnalysis(AfterMethodCallAnalysisEvent $event): void
{
foreach ($this->after_method_checks as $handler) {
$handler::afterMethodCallAnalysis($event);
}
}
public function dispatchAfterFunctionCallAnalysis(AfterFunctionCallAnalysisEvent $event): void
{
foreach ($this->after_function_checks as $handler) {
$handler::afterFunctionCallAnalysis($event);
}
}
public function dispatchAfterEveryFunctionCallAnalysis(AfterEveryFunctionCallAnalysisEvent $event): void
{
foreach ($this->after_every_function_checks as $handler) {
$handler::afterEveryFunctionCallAnalysis($event);
}
}
public function dispatchAfterExpressionAnalysis(AfterExpressionAnalysisEvent $event): ?bool
{
foreach ($this->after_expression_checks as $handler) {
if ($handler::afterExpressionAnalysis($event) === false) {
return false;
}
}
return null;
}
public function dispatchAfterStatementAnalysis(AfterStatementAnalysisEvent $event): ?bool
{
foreach ($this->after_statement_checks as $handler) {
if ($handler::afterStatementAnalysis($event) === false) {
return false;
}
}
return null;
}
public function dispatchStringInterpreter(StringInterpreterEvent $event): ?TLiteralString
{
foreach ($this->string_interpreters as $handler) {
if ($type = $handler::getTypeFromValue($event)) {
return $type;
}
}
return null;
}
public function dispatchAfterClassLikeExistenceCheck(AfterClassLikeExistenceCheckEvent $event): void
{
foreach ($this->after_classlike_exists_checks as $handler) {
$handler::afterClassLikeExistenceCheck($event);
}
}
public function dispatchAfterClassLikeAnalysis(AfterClassLikeAnalysisEvent $event): ?bool
{
foreach ($this->after_classlike_checks as $handler) {
if ($handler::afterStatementAnalysis($event) === false) {
return false;
}
}
return null;
}
public function hasAfterClassLikeVisitHandlers(): bool
{
return count($this->after_visit_classlikes) > 0;
}
public function dispatchAfterClassLikeVisit(AfterClassLikeVisitEvent $event): void
{
foreach ($this->after_visit_classlikes as $handler) {
$handler::afterClassLikeVisit($event);
}
}
public function dispatchAfterCodebasePopulated(AfterCodebasePopulatedEvent $event): void
{
foreach ($this->after_codebase_populated as $handler) {
$handler::afterCodebasePopulated($event);
}
}
public function dispatchBeforeAddIssue(BeforeAddIssueEvent $event): ?bool
{
foreach ($this->before_add_issue as $handler) {
$result = $handler::beforeAddIssue($event);
if (is_bool($result)) {
return $result;
}
}
return null;
}
public function dispatchAfterAnalysis(AfterAnalysisEvent $event): void
{
foreach ($this->after_analysis as $handler) {
$handler::afterAnalysis($event);
}
}
public function dispatchAfterFileAnalysis(AfterFileAnalysisEvent $event): void
{
foreach ($this->after_file_checks as $handler) {
$handler::afterAnalyzeFile($event);
}
}
public function dispatchBeforeFileAnalysis(BeforeFileAnalysisEvent $event): void
{
foreach ($this->before_file_checks as $handler) {
$handler::beforeAnalyzeFile($event);
}
}
public function dispatchAfterFunctionLikeAnalysis(AfterFunctionLikeAnalysisEvent $event): ?bool
{
foreach ($this->after_functionlike_checks as $handler) {
if ($handler::afterStatementAnalysis($event) === false) {
return false;
}
}
return null;
}
/**
* @return list<string>
*/
public function dispatchAddTaints(AddRemoveTaintsEvent $event): array
{
$added_taints = [];
foreach ($this->add_taints_checks as $handler) {
$added_taints = array_merge($added_taints, $handler::addTaints($event));
}
return $added_taints;
}
/**
* @return list<string>
*/
public function dispatchRemoveTaints(AddRemoveTaintsEvent $event): array
{
$removed_taints = [];
foreach ($this->remove_taints_checks as $handler) {
$removed_taints = array_merge($removed_taints, $handler::removeTaints($event));
}
return $removed_taints;
}
}