1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace loophp\PhptreeAstGenerator\Exporter; |
6
|
|
|
|
7
|
|
|
use Closure; |
8
|
|
|
use loophp\phptree\Exporter\ExporterInterface; |
9
|
|
|
use loophp\phptree\Modifier\Apply; |
10
|
|
|
use loophp\phptree\Modifier\Filter; |
11
|
|
|
use loophp\phptree\Node\AttributeNodeInterface; |
12
|
|
|
use loophp\phptree\Node\NodeInterface; |
13
|
|
|
use PhpParser\Node\Arg; |
14
|
|
|
use PhpParser\Node\Const_; |
15
|
|
|
use PhpParser\Node\Expr; |
16
|
|
|
use PhpParser\Node\Identifier; |
17
|
|
|
use PhpParser\Node\Name; |
18
|
|
|
use PhpParser\Node\NullableType; |
19
|
|
|
use PhpParser\Node\Param; |
20
|
|
|
use PhpParser\Node\Scalar; |
21
|
|
|
use PhpParser\Node\Stmt; |
22
|
|
|
|
23
|
|
|
use function get_class; |
24
|
|
|
use function in_array; |
25
|
|
|
use function is_string; |
26
|
|
|
|
27
|
|
|
class FancyExporter implements ExporterInterface |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var \loophp\phptree\Exporter\ExporterInterface |
31
|
|
|
*/ |
32
|
|
|
private $exporter; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* FancyGenerator constructor. |
36
|
|
|
* |
37
|
|
|
* @param \loophp\phptree\Exporter\ExporterInterface $exporter |
38
|
|
|
*/ |
39
|
|
|
public function __construct(ExporterInterface $exporter) |
40
|
|
|
{ |
41
|
|
|
$this->exporter = $exporter; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function export(NodeInterface $node) |
48
|
|
|
{ |
49
|
|
|
$applyShapeModifier = new Apply(Closure::fromCallable([$this, 'applyShape'])); |
50
|
|
|
$applyLabelModifier = new Apply(Closure::fromCallable([$this, 'applyLabel'])); |
51
|
|
|
$removeStmtExpressionModifier = new Apply(Closure::fromCallable([$this, 'cleanObsoleteNode'])); |
52
|
|
|
$filterModifier = new Filter(Closure::fromCallable([$this, 'filterObsoleteNode'])); |
53
|
|
|
|
54
|
|
|
$node = $removeStmtExpressionModifier->modify($node); |
55
|
|
|
$node = $applyLabelModifier->modify($node); |
56
|
|
|
$node = $applyShapeModifier->modify($node); |
57
|
|
|
$node = $filterModifier->modify($node); |
58
|
|
|
|
59
|
|
|
return $this->exporter->export($node); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
protected function applyShape(AttributeNodeInterface $node): void |
63
|
|
|
{ |
64
|
|
|
$astNode = $node->getAttribute('astNode'); |
65
|
|
|
|
66
|
|
|
if (null === $astNode) { |
67
|
|
|
return; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
switch (true) { |
71
|
|
|
default: |
72
|
|
|
$node->setAttribute('shape', 'rect'); |
73
|
|
|
|
74
|
|
|
break; |
75
|
|
|
case $astNode instanceof Stmt\Namespace_: |
76
|
|
|
$node->setAttribute('shape', 'house'); |
77
|
|
|
|
78
|
|
|
break; |
79
|
|
|
case $astNode instanceof Stmt\Class_: |
80
|
|
|
$node->setAttribute('shape', 'invhouse'); |
81
|
|
|
|
82
|
|
|
break; |
83
|
|
|
case $astNode instanceof Stmt\ClassMethod: |
84
|
|
|
$node->setAttribute('shape', 'folder'); |
85
|
|
|
|
86
|
|
|
break; |
87
|
|
|
case $astNode instanceof Expr\ConstFetch: |
88
|
|
|
case $astNode instanceof Scalar\DNumber: |
89
|
|
|
case $astNode instanceof Scalar\LNumber: |
90
|
|
|
$node->setAttribute('shape', 'circle'); |
91
|
|
|
|
92
|
|
|
break; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function cleanObsoleteNode(AttributeNodeInterface $node): void |
97
|
|
|
{ |
98
|
|
|
$astNode = $node->getAttribute('astNode'); |
99
|
|
|
|
100
|
|
|
switch (true) { |
101
|
|
|
case $astNode instanceof Stmt\Class_: |
102
|
|
|
/** @var AttributeNodeInterface $child */ |
103
|
|
|
foreach ($node->children() as $key => $child) { |
104
|
|
|
if (in_array($child->getAttribute('astNode'), $astNode->implements, true)) { |
105
|
|
|
unset($node[$key]); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if (in_array($child->getAttribute('astNode'), [$astNode->extends], true)) { |
109
|
|
|
unset($node[$key]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
break; |
114
|
|
|
case $astNode instanceof Stmt\Expression: |
115
|
|
|
if (null !== $parent = $node->getParent()) { |
116
|
|
|
// Find the key of the current node in the parent. |
117
|
|
|
foreach ($parent->children() as $key => $child) { |
118
|
|
|
if ($child === $node) { |
119
|
|
|
// Replace it with it's first child. |
120
|
|
|
$parent[$key] = $node[0]; |
121
|
|
|
|
122
|
|
|
break; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
break; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
protected function filterObsoleteNode(AttributeNodeInterface $node): bool |
132
|
|
|
{ |
133
|
|
|
$removeTypes = [ |
134
|
|
|
Stmt\Use_::class, |
135
|
|
|
Stmt\Declare_::class, |
136
|
|
|
Identifier::class, |
137
|
|
|
]; |
138
|
|
|
|
139
|
|
|
return in_array(get_class($node->getAttribute('astNode')), $removeTypes, true); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
private function applyLabel(AttributeNodeInterface $node): void |
143
|
|
|
{ |
144
|
|
|
$astNode = $node->getAttribute('astNode'); |
145
|
|
|
|
146
|
|
|
if (null === $astNode) { |
147
|
|
|
return; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
switch (true) { |
151
|
|
|
case $astNode instanceof Name: |
152
|
|
|
$node->setAttribute( |
153
|
|
|
'label', |
154
|
|
|
sprintf('%s', $astNode->toString()) |
155
|
|
|
); |
156
|
|
|
|
157
|
|
|
break; |
158
|
|
|
case $astNode instanceof Stmt\Namespace_: |
159
|
|
|
$node->setAttribute( |
160
|
|
|
'label', |
161
|
|
|
sprintf('Namespace %s', addslashes($node[0]->getAttribute('label'))) |
162
|
|
|
); |
163
|
|
|
unset($node[0]); |
164
|
|
|
|
165
|
|
|
break; |
166
|
|
|
case $astNode instanceof Stmt\Class_: |
167
|
|
|
$extends = ''; |
168
|
|
|
|
169
|
|
|
if (null !== $extendNode = $astNode->extends) { |
170
|
|
|
$extends = sprintf(' extends %s', $extendNode->toString()); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$node->setAttribute( |
174
|
|
|
'label', |
175
|
|
|
sprintf( |
176
|
|
|
'Class %s%s', |
177
|
|
|
$node[0]->getAttribute('label'), |
178
|
|
|
$extends |
179
|
|
|
) |
180
|
|
|
); |
181
|
|
|
|
182
|
|
|
break; |
183
|
|
|
case $astNode instanceof Stmt\ClassMethod: |
184
|
|
|
$identifier = $node[0]->getAttribute('label'); |
185
|
|
|
|
186
|
|
|
$returnType = ''; |
187
|
|
|
|
188
|
|
|
/** @var AttributeNodeInterface $child */ |
189
|
|
|
foreach ($node->children() as $keyVar => $child) { |
190
|
|
|
if ($child->getAttribute('astNode') === $astNode->getReturnType()) { |
191
|
|
|
unset($node[$keyVar]); |
192
|
|
|
|
193
|
|
|
if ($astNode->getReturnType() instanceof NullableType) { |
194
|
|
|
$returnType = sprintf(': null'); |
195
|
|
|
} else { |
196
|
|
|
$returnType = $astNode->getReturnType(); |
197
|
|
|
|
198
|
|
|
if ($returnType instanceof Identifier) { |
199
|
|
|
$returnType = $returnType->toString(); |
200
|
|
|
} else { |
201
|
|
|
$returnType = 'void'; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
$returnType = sprintf(': %s', $returnType); |
205
|
|
|
} |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
$node->setAttribute('label', sprintf('Method %s()%s', $identifier, $returnType)); |
210
|
|
|
|
211
|
|
|
break; |
212
|
|
|
case $astNode instanceof Stmt\Function_: |
213
|
|
|
$node->setAttribute( |
214
|
|
|
'label', |
215
|
|
|
sprintf( |
216
|
|
|
'Definition of %s()', |
217
|
|
|
$node[0]->getAttribute('label') |
218
|
|
|
) |
219
|
|
|
); |
220
|
|
|
|
221
|
|
|
break; |
222
|
|
|
case $astNode instanceof Stmt\Property: |
223
|
|
|
$node->setAttribute( |
224
|
|
|
'label', |
225
|
|
|
sprintf( |
226
|
|
|
'Property %s', |
227
|
|
|
$node[0]->getAttribute('label') |
228
|
|
|
) |
229
|
|
|
); |
230
|
|
|
|
231
|
|
|
unset($node[0]); |
232
|
|
|
|
233
|
|
|
break; |
234
|
|
|
case $astNode instanceof Param: |
235
|
|
|
$degree = $node->degree(); |
236
|
|
|
$parameter = []; |
237
|
|
|
|
238
|
|
|
if (1 === $degree) { |
239
|
|
|
$parameter[] = $node[0]->getAttribute('label'); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
if (2 === $degree) { |
243
|
|
|
$parameter[] = $node[0]->getAttribute('label'); |
244
|
|
|
$parameter[] = $node[1]->getAttribute('label'); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
if (3 === $degree) { |
248
|
|
|
$parameter[] = $node[0]->getAttribute('label'); |
249
|
|
|
$parameter[] = $node[1]->getAttribute('label'); |
250
|
|
|
$parameter[] = sprintf('= %s', mb_strtolower($node[2]->getAttribute('label'))); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
$node->setAttribute( |
254
|
|
|
'label', |
255
|
|
|
sprintf('Parameter %s', implode(' ', $parameter)) |
256
|
|
|
); |
257
|
|
|
|
258
|
|
|
unset($node[0], $node[1], $node[2]); |
259
|
|
|
|
260
|
|
|
break; |
261
|
|
|
case $astNode instanceof NullableType: |
262
|
|
|
case $astNode instanceof Expr\ConstFetch: |
263
|
|
|
case $astNode instanceof Stmt\PropertyProperty: |
264
|
|
|
$node->setAttribute( |
265
|
|
|
'label', |
266
|
|
|
sprintf( |
267
|
|
|
'%s', |
268
|
|
|
$node[0]->getAttribute('label') |
269
|
|
|
) |
270
|
|
|
); |
271
|
|
|
|
272
|
|
|
unset($node[0]); |
273
|
|
|
|
274
|
|
|
break; |
275
|
|
|
case $astNode instanceof Stmt\Use_: |
276
|
|
|
$node->setAttribute( |
277
|
|
|
'label', |
278
|
|
|
sprintf('Use') |
279
|
|
|
); |
280
|
|
|
|
281
|
|
|
break; |
282
|
|
|
case $astNode instanceof Stmt\UseUse: |
283
|
|
|
$name = $node[0]; |
284
|
|
|
|
285
|
|
|
$node->setAttribute( |
286
|
|
|
'label', |
287
|
|
|
sprintf('%s', $name->getAttribute('label')) |
288
|
|
|
); |
289
|
|
|
|
290
|
|
|
break; |
291
|
|
|
case $astNode instanceof Stmt\Foreach_: |
292
|
|
|
$keyVar = ''; |
293
|
|
|
$expr = $node[0]->getAttribute('label'); |
294
|
|
|
$valueVar = $node[1]->getAttribute('label'); |
295
|
|
|
|
296
|
|
|
if (null !== $astNode->keyVar) { |
297
|
|
|
$keyVar = sprintf('%s => ', $valueVar); |
298
|
|
|
$valueVar = $node[2]->getAttribute('label'); |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
$node->setAttribute( |
302
|
|
|
'label', |
303
|
|
|
sprintf('Foreach %s as %s%s', $expr, $keyVar, $valueVar) |
304
|
|
|
); |
305
|
|
|
|
306
|
|
|
unset($node[0], $node[1], $node[2]); |
307
|
|
|
|
308
|
|
|
break; |
309
|
|
|
case $astNode instanceof Stmt\If_: |
310
|
|
|
$name = 'If | Then'; |
311
|
|
|
|
312
|
|
|
if (null !== $astNode->else) { |
313
|
|
|
$name .= ' | Else'; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
$node->setAttribute( |
317
|
|
|
'label', |
318
|
|
|
sprintf('%s', $name) |
319
|
|
|
); |
320
|
|
|
|
321
|
|
|
break; |
322
|
|
|
case $astNode instanceof Stmt\Else_: |
323
|
|
|
$node->setAttribute( |
324
|
|
|
'label', |
325
|
|
|
sprintf('Else') |
326
|
|
|
); |
327
|
|
|
|
328
|
|
|
break; |
329
|
|
|
case $astNode instanceof Stmt\ElseIf_: |
330
|
|
|
$node->setAttribute( |
331
|
|
|
'label', |
332
|
|
|
sprintf('Elseif') |
333
|
|
|
); |
334
|
|
|
|
335
|
|
|
break; |
336
|
|
|
case $astNode instanceof Stmt\Return_: |
337
|
|
|
$node->setAttribute('label', sprintf('Return')); |
338
|
|
|
|
339
|
|
|
break; |
340
|
|
|
case $astNode instanceof Identifier: |
341
|
|
|
$node->setAttribute('label', sprintf('%s', $astNode->name)); |
342
|
|
|
|
343
|
|
|
break; |
344
|
|
|
case $astNode instanceof Stmt\Declare_: |
345
|
|
|
$node->setAttribute('label', sprintf('Declare')); |
346
|
|
|
|
347
|
|
|
break; |
348
|
|
|
case $astNode instanceof Stmt\DeclareDeclare: |
349
|
|
|
$node->setAttribute( |
350
|
|
|
'label', |
351
|
|
|
sprintf( |
352
|
|
|
'Key %s, Value %s', |
353
|
|
|
$node[0]->getAttribute('label'), |
354
|
|
|
$node[1]->getAttribute('label') |
355
|
|
|
) |
356
|
|
|
); |
357
|
|
|
|
358
|
|
|
break; |
359
|
|
|
case $astNode instanceof Scalar\LNumber: |
360
|
|
|
$node->setAttribute('label', sprintf('Integer %s', $astNode->value)); |
361
|
|
|
|
362
|
|
|
break; |
363
|
|
|
case $astNode instanceof Stmt\Unset_: |
364
|
|
|
$node->setAttribute( |
365
|
|
|
'label', |
366
|
|
|
sprintf('Unset') |
367
|
|
|
); |
368
|
|
|
|
369
|
|
|
break; |
370
|
|
|
case $astNode instanceof Expr\BinaryOp\Concat: |
371
|
|
|
$node->setAttribute( |
372
|
|
|
'label', |
373
|
|
|
sprintf('Concatenation of') |
374
|
|
|
); |
375
|
|
|
|
376
|
|
|
break; |
377
|
|
|
case $astNode instanceof Expr\BinaryOp\Identical: |
378
|
|
|
$node->setAttribute('label', 'strict equal'); |
379
|
|
|
|
380
|
|
|
break; |
381
|
|
|
case $astNode instanceof Expr\AssignOp\Plus: |
382
|
|
|
case $astNode instanceof Expr\BinaryOp\Plus: |
383
|
|
|
$node->setAttribute('label', 'addition'); |
384
|
|
|
|
385
|
|
|
break; |
386
|
|
|
case $astNode instanceof Expr\AssignOp\Mul: |
387
|
|
|
case $astNode instanceof Expr\BinaryOp\Mul: |
388
|
|
|
$node->setAttribute('label', 'multiplication'); |
389
|
|
|
|
390
|
|
|
break; |
391
|
|
|
case $astNode instanceof Expr\AssignOp\Minus: |
392
|
|
|
case $astNode instanceof Expr\BinaryOp\Minus: |
393
|
|
|
$node->setAttribute('label', 'substraction'); |
394
|
|
|
|
395
|
|
|
break; |
396
|
|
|
case $astNode instanceof Expr\AssignOp\Div: |
397
|
|
|
case $astNode instanceof Expr\BinaryOp\Div: |
398
|
|
|
$node->setAttribute('label', 'division'); |
399
|
|
|
|
400
|
|
|
break; |
401
|
|
|
case $astNode instanceof Expr\BinaryOp\BooleanOr: |
402
|
|
|
$node->setAttribute('label', 'or'); |
403
|
|
|
|
404
|
|
|
break; |
405
|
|
|
case $astNode instanceof Expr\BinaryOp\NotIdentical: |
406
|
|
|
$node->setAttribute('label', 'is not equal'); |
407
|
|
|
|
408
|
|
|
break; |
409
|
|
|
case $astNode instanceof Expr\BooleanNot: |
410
|
|
|
$node->setAttribute('label', 'not'); |
411
|
|
|
|
412
|
|
|
break; |
413
|
|
|
case $astNode instanceof Expr\MethodCall: |
414
|
|
|
$node->setAttribute( |
415
|
|
|
'label', |
416
|
|
|
sprintf( |
417
|
|
|
'%s->%s()', |
418
|
|
|
$node[0]->getAttribute('label'), |
419
|
|
|
$node[1]->getAttribute('label') |
420
|
|
|
) |
421
|
|
|
); |
422
|
|
|
|
423
|
|
|
break; |
424
|
|
|
case $astNode instanceof Expr\Variable: |
425
|
|
|
$node->setAttribute( |
426
|
|
|
'label', |
427
|
|
|
sprintf('$%s', is_string($astNode->name) ? $astNode->name : 'ERROR') |
428
|
|
|
); |
429
|
|
|
|
430
|
|
|
break; |
431
|
|
|
case $astNode instanceof Expr\FuncCall: |
432
|
|
|
$node->setAttribute( |
433
|
|
|
'label', |
434
|
|
|
sprintf( |
435
|
|
|
'%s()', |
436
|
|
|
$node[0]->getAttribute('label') |
437
|
|
|
) |
438
|
|
|
); |
439
|
|
|
unset($node[0]); |
440
|
|
|
|
441
|
|
|
break; |
442
|
|
|
case $astNode instanceof Expr\PreInc: |
443
|
|
|
$node->setAttribute( |
444
|
|
|
'label', |
445
|
|
|
sprintf( |
446
|
|
|
'++%s', |
447
|
|
|
$node[0]->getAttribute('label') |
448
|
|
|
) |
449
|
|
|
); |
450
|
|
|
|
451
|
|
|
unset($node[0]); |
452
|
|
|
|
453
|
|
|
break; |
454
|
|
|
case $astNode instanceof Expr\PostInc: |
455
|
|
|
$node->setAttribute( |
456
|
|
|
'label', |
457
|
|
|
sprintf( |
458
|
|
|
'%s++', |
459
|
|
|
$node[0]->getAttribute('label') |
460
|
|
|
) |
461
|
|
|
); |
462
|
|
|
|
463
|
|
|
unset($node[0]); |
464
|
|
|
|
465
|
|
|
break; |
466
|
|
|
case $astNode instanceof Expr\Isset_: |
467
|
|
|
$variable = $node[0]->getAttribute('label'); |
468
|
|
|
|
469
|
|
|
$node->setAttribute( |
470
|
|
|
'label', |
471
|
|
|
sprintf('Is %s set ?', $variable) |
472
|
|
|
); |
473
|
|
|
|
474
|
|
|
unset($node[0]); |
475
|
|
|
|
476
|
|
|
break; |
477
|
|
|
case $astNode instanceof Expr\Array_: |
478
|
|
|
$name = 'Array of'; |
479
|
|
|
|
480
|
|
|
if (0 === $node->degree()) { |
481
|
|
|
$name = 'Empty array'; |
482
|
|
|
} |
483
|
|
|
|
484
|
|
|
$node->setAttribute( |
485
|
|
|
'label', |
486
|
|
|
sprintf('%s', $name) |
487
|
|
|
); |
488
|
|
|
|
489
|
|
|
break; |
490
|
|
|
case $astNode instanceof Expr\ArrayItem: |
491
|
|
|
$node->setAttribute( |
492
|
|
|
'label', |
493
|
|
|
sprintf('Key | Value') |
494
|
|
|
); |
495
|
|
|
|
496
|
|
|
break; |
497
|
|
|
case $astNode instanceof Expr\ArrayDimFetch: |
498
|
|
|
$name = $node[0]->getAttribute('label'); |
499
|
|
|
|
500
|
|
|
$dim = ''; |
501
|
|
|
|
502
|
|
|
if (null !== $astNode->dim) { |
503
|
|
|
$dim = $node[1]->getAttribute('label'); |
504
|
|
|
} |
505
|
|
|
|
506
|
|
|
$node->setAttribute( |
507
|
|
|
'label', |
508
|
|
|
sprintf('%s[%s]', $name, $dim) |
509
|
|
|
); |
510
|
|
|
|
511
|
|
|
unset($node[0], $node[1]); |
512
|
|
|
|
513
|
|
|
break; |
514
|
|
|
case $astNode instanceof Expr\AssignOp\Plus: |
515
|
|
|
$valueVar = $node[0]->getAttribute('label'); |
516
|
|
|
|
517
|
|
|
$node->setAttribute( |
518
|
|
|
'label', |
519
|
|
|
sprintf('Increment %s with', $valueVar) |
520
|
|
|
); |
521
|
|
|
|
522
|
|
|
unset($node[0]); |
523
|
|
|
|
524
|
|
|
break; |
525
|
|
|
case $astNode instanceof Expr\PropertyFetch: |
526
|
|
|
$variable = $node[0]->getAttribute('label'); |
527
|
|
|
$identifier = $node[1]->getAttribute('label'); |
528
|
|
|
|
529
|
|
|
unset($node[0], $node[1]); |
530
|
|
|
|
531
|
|
|
$node->setAttribute('label', sprintf('%s->%s', $variable, $identifier)); |
532
|
|
|
|
533
|
|
|
break; |
534
|
|
|
case $astNode instanceof Expr\Assign: |
535
|
|
|
$variable = $node[0]->getAttribute('label'); |
536
|
|
|
|
537
|
|
|
$node->setAttribute( |
538
|
|
|
'label', |
539
|
|
|
sprintf('Assign to %s', $variable) |
540
|
|
|
); |
541
|
|
|
|
542
|
|
|
unset($node[0]); |
543
|
|
|
|
544
|
|
|
break; |
545
|
|
|
case $astNode instanceof Scalar\DNumber: |
546
|
|
|
$node->setAttribute('label', sprintf('Decimal %s', $astNode->value)); |
547
|
|
|
|
548
|
|
|
break; |
549
|
|
|
case $astNode instanceof Stmt\Break_: |
550
|
|
|
$node->setAttribute( |
551
|
|
|
'label', |
552
|
|
|
sprintf('Break') |
553
|
|
|
); |
554
|
|
|
|
555
|
|
|
break; |
556
|
|
|
case $astNode instanceof Stmt\Continue_: |
557
|
|
|
$node->setAttribute( |
558
|
|
|
'label', |
559
|
|
|
sprintf('Continue') |
560
|
|
|
); |
561
|
|
|
|
562
|
|
|
break; |
563
|
|
|
case $astNode instanceof Stmt\TryCatch: |
564
|
|
|
$name = 'Try | Catch'; |
565
|
|
|
|
566
|
|
|
if (null !== $astNode->finally) { |
567
|
|
|
$name .= ' | Finally'; |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
$node->setAttribute( |
571
|
|
|
'label', |
572
|
|
|
sprintf('%s', $name) |
573
|
|
|
); |
574
|
|
|
|
575
|
|
|
break; |
576
|
|
|
case $astNode instanceof Stmt\Catch_: |
577
|
|
|
$node->setAttribute( |
578
|
|
|
'label', |
579
|
|
|
sprintf( |
580
|
|
|
'Catch type %s in %s', |
581
|
|
|
$node[0]->getAttribute('label'), |
582
|
|
|
$node[1]->getAttribute('label') |
583
|
|
|
) |
584
|
|
|
); |
585
|
|
|
|
586
|
|
|
unset($node[0], $node[1]); |
587
|
|
|
|
588
|
|
|
break; |
589
|
|
|
case $astNode instanceof Stmt\Finally_: |
590
|
|
|
$node->setAttribute( |
591
|
|
|
'label', |
592
|
|
|
sprintf('Finally') |
593
|
|
|
); |
594
|
|
|
|
595
|
|
|
break; |
596
|
|
|
case $astNode instanceof Stmt\Echo_: |
597
|
|
|
$node->setAttribute( |
598
|
|
|
'label', |
599
|
|
|
sprintf('Print') |
600
|
|
|
); |
601
|
|
|
|
602
|
|
|
break; |
603
|
|
|
case $astNode instanceof Scalar\Encapsed: |
604
|
|
|
$node->setAttribute( |
605
|
|
|
'label', |
606
|
|
|
sprintf('String') |
607
|
|
|
); |
608
|
|
|
|
609
|
|
|
break; |
610
|
|
|
case $astNode instanceof Scalar\EncapsedStringPart: |
611
|
|
|
$node->setAttribute( |
612
|
|
|
'label', |
613
|
|
|
sprintf('%s', $astNode->value) |
614
|
|
|
); |
615
|
|
|
|
616
|
|
|
break; |
617
|
|
|
case $astNode instanceof Expr\New_: |
618
|
|
|
$node->setAttribute( |
619
|
|
|
'label', |
620
|
|
|
sprintf( |
621
|
|
|
'New %s', |
622
|
|
|
$node[0]->getAttribute('label') |
623
|
|
|
) |
624
|
|
|
); |
625
|
|
|
|
626
|
|
|
unset($node[0]); |
627
|
|
|
|
628
|
|
|
break; |
629
|
|
|
case $astNode instanceof Expr\StaticCall: |
630
|
|
|
$node->setAttribute( |
631
|
|
|
'label', |
632
|
|
|
sprintf( |
633
|
|
|
'Static call %s::%s()', |
634
|
|
|
$node[0]->getAttribute('label'), |
635
|
|
|
$node[1]->getAttribute('label') |
636
|
|
|
) |
637
|
|
|
); |
638
|
|
|
|
639
|
|
|
unset($node[0], $node[1]); |
640
|
|
|
|
641
|
|
|
break; |
642
|
|
|
case $astNode instanceof Expr\YieldFrom: |
643
|
|
|
$node->setAttribute( |
644
|
|
|
'label', |
645
|
|
|
sprintf('Yield from') |
646
|
|
|
); |
647
|
|
|
|
648
|
|
|
break; |
649
|
|
|
case $astNode instanceof Expr\Yield_: |
650
|
|
|
$node->setAttribute( |
651
|
|
|
'label', |
652
|
|
|
sprintf('Yield') |
653
|
|
|
); |
654
|
|
|
|
655
|
|
|
break; |
656
|
|
|
case $astNode instanceof Expr\Clone_: |
657
|
|
|
$node->setAttribute( |
658
|
|
|
'label', |
659
|
|
|
sprintf('Clone of') |
660
|
|
|
); |
661
|
|
|
|
662
|
|
|
break; |
663
|
|
|
case $astNode instanceof Expr\Instanceof_: |
664
|
|
|
$node->setAttribute( |
665
|
|
|
'label', |
666
|
|
|
sprintf( |
667
|
|
|
'%s is an instance of %s', |
668
|
|
|
$node[0]->getAttribute('label'), |
669
|
|
|
$node[1]->getAttribute('label') |
670
|
|
|
) |
671
|
|
|
); |
672
|
|
|
|
673
|
|
|
unset($node[0], $node[1]); |
674
|
|
|
|
675
|
|
|
break; |
676
|
|
|
case $astNode instanceof Arg: |
677
|
|
|
$node->setAttribute( |
678
|
|
|
'label', |
679
|
|
|
sprintf('With argument') |
680
|
|
|
); |
681
|
|
|
|
682
|
|
|
break; |
683
|
|
|
case $astNode instanceof Expr\Closure: |
684
|
|
|
$node->setAttribute( |
685
|
|
|
'label', |
686
|
|
|
sprintf('Closure') |
687
|
|
|
); |
688
|
|
|
|
689
|
|
|
break; |
690
|
|
|
case $astNode instanceof Expr\Ternary: |
691
|
|
|
$node->setAttribute( |
692
|
|
|
'label', |
693
|
|
|
sprintf('Ternary') |
694
|
|
|
); |
695
|
|
|
|
696
|
|
|
break; |
697
|
|
|
case $astNode instanceof Stmt\While_: |
698
|
|
|
$node->setAttribute( |
699
|
|
|
'label', |
700
|
|
|
sprintf('While') |
701
|
|
|
); |
702
|
|
|
|
703
|
|
|
break; |
704
|
|
|
case $astNode instanceof Expr\BinaryOp\Coalesce: |
705
|
|
|
$node->setAttribute( |
706
|
|
|
'label', |
707
|
|
|
sprintf('Unless is not null') |
708
|
|
|
); |
709
|
|
|
|
710
|
|
|
break; |
711
|
|
|
case $astNode instanceof Scalar\String_: |
712
|
|
|
$node->setAttribute( |
713
|
|
|
'label', |
714
|
|
|
sprintf( |
715
|
|
|
'\'%s\'', |
716
|
|
|
addslashes($astNode->value) |
717
|
|
|
) |
718
|
|
|
); |
719
|
|
|
|
720
|
|
|
break; |
721
|
|
|
case $astNode instanceof Stmt\Nop: |
722
|
|
|
$node->setAttribute('label', sprintf('No operation')); |
723
|
|
|
|
724
|
|
|
break; |
725
|
|
|
case $astNode instanceof Stmt\Throw_: |
726
|
|
|
$node->setAttribute( |
727
|
|
|
'label', |
728
|
|
|
sprintf('Throw') |
729
|
|
|
); |
730
|
|
|
|
731
|
|
|
break; |
732
|
|
|
case $astNode instanceof Expr\Cast\Array_: |
733
|
|
|
$node->setAttribute( |
734
|
|
|
'label', |
735
|
|
|
sprintf( |
736
|
|
|
'Cast as array %s', |
737
|
|
|
$node[0]->getAttribute('label') |
738
|
|
|
) |
739
|
|
|
); |
740
|
|
|
|
741
|
|
|
unset($node[0]); |
742
|
|
|
|
743
|
|
|
break; |
744
|
|
|
case $astNode instanceof Expr\Cast\String_: |
745
|
|
|
$node->setAttribute( |
746
|
|
|
'label', |
747
|
|
|
sprintf( |
748
|
|
|
'Cast as string %s', |
749
|
|
|
$node[0]->getAttribute('label') |
750
|
|
|
) |
751
|
|
|
); |
752
|
|
|
|
753
|
|
|
unset($node[0]); |
754
|
|
|
|
755
|
|
|
break; |
756
|
|
|
case $astNode instanceof Expr\Cast\Int_: |
757
|
|
|
$node->setAttribute( |
758
|
|
|
'label', |
759
|
|
|
sprintf( |
760
|
|
|
'Cast as integer %s', |
761
|
|
|
$node[0]->getAttribute('label') |
762
|
|
|
) |
763
|
|
|
); |
764
|
|
|
|
765
|
|
|
unset($node[0]); |
766
|
|
|
|
767
|
|
|
break; |
768
|
|
|
case $astNode instanceof Expr\StaticPropertyFetch: |
769
|
|
|
case $astNode instanceof Expr\ClassConstFetch: |
770
|
|
|
$node->setAttribute( |
771
|
|
|
'label', |
772
|
|
|
sprintf( |
773
|
|
|
'%s::%s', |
774
|
|
|
$node[0]->getAttribute('label'), |
775
|
|
|
$node[1]->getAttribute('label') |
776
|
|
|
) |
777
|
|
|
); |
778
|
|
|
|
779
|
|
|
break; |
780
|
|
|
case $astNode instanceof Expr\AssignOp\Concat: |
781
|
|
|
$node->setAttribute( |
782
|
|
|
'label', |
783
|
|
|
sprintf( |
784
|
|
|
'Concat %s with', |
785
|
|
|
$node[0]->getAttribute('label') |
786
|
|
|
) |
787
|
|
|
); |
788
|
|
|
|
789
|
|
|
break; |
790
|
|
|
case $astNode instanceof Expr\BinaryOp\Smaller: |
791
|
|
|
$node->setAttribute( |
792
|
|
|
'label', |
793
|
|
|
sprintf('Is smaller') |
794
|
|
|
); |
795
|
|
|
|
796
|
|
|
break; |
797
|
|
|
case $astNode instanceof Expr\BinaryOp\SmallerOrEqual: |
798
|
|
|
$node->setAttribute( |
799
|
|
|
'label', |
800
|
|
|
sprintf('Is smaller or equal') |
801
|
|
|
); |
802
|
|
|
|
803
|
|
|
break; |
804
|
|
|
case $astNode instanceof Expr\BinaryOp\Greater: |
805
|
|
|
$node->setAttribute( |
806
|
|
|
'label', |
807
|
|
|
sprintf('Is greater') |
808
|
|
|
); |
809
|
|
|
|
810
|
|
|
break; |
811
|
|
|
case $astNode instanceof Expr\BinaryOp\GreaterOrEqual: |
812
|
|
|
$node->setAttribute( |
813
|
|
|
'label', |
814
|
|
|
sprintf('Is greater or equal') |
815
|
|
|
); |
816
|
|
|
|
817
|
|
|
break; |
818
|
|
|
case $astNode instanceof Expr\BinaryOp\BitwiseAnd: |
819
|
|
|
$node->setAttribute( |
820
|
|
|
'label', |
821
|
|
|
sprintf('Bitwise AND') |
822
|
|
|
); |
823
|
|
|
|
824
|
|
|
break; |
825
|
|
|
case $astNode instanceof Expr\BitwiseNot: |
826
|
|
|
$node->setAttribute( |
827
|
|
|
'label', |
828
|
|
|
sprintf('Bitwise not') |
829
|
|
|
); |
830
|
|
|
|
831
|
|
|
break; |
832
|
|
|
case $astNode instanceof Expr\BinaryOp\BitwiseOr: |
833
|
|
|
$node->setAttribute( |
834
|
|
|
'label', |
835
|
|
|
sprintf('Bitwise OR') |
836
|
|
|
); |
837
|
|
|
|
838
|
|
|
break; |
839
|
|
|
case $astNode instanceof Expr\BinaryOp\BitwiseXor: |
840
|
|
|
$node->setAttribute( |
841
|
|
|
'label', |
842
|
|
|
sprintf('Bitwise XOR') |
843
|
|
|
); |
844
|
|
|
|
845
|
|
|
break; |
846
|
|
|
case $astNode instanceof Expr\AssignOp\BitwiseOr: |
847
|
|
|
$node->setAttribute( |
848
|
|
|
'label', |
849
|
|
|
sprintf('Bitwise OR with') |
850
|
|
|
); |
851
|
|
|
|
852
|
|
|
break; |
853
|
|
|
case $astNode instanceof Expr\AssignOp\BitwiseAnd: |
854
|
|
|
$node->setAttribute( |
855
|
|
|
'label', |
856
|
|
|
sprintf('Bitwise AND with') |
857
|
|
|
); |
858
|
|
|
|
859
|
|
|
break; |
860
|
|
|
case $astNode instanceof Expr\BinaryOp\BooleanAnd: |
861
|
|
|
$node->setAttribute( |
862
|
|
|
'label', |
863
|
|
|
sprintf('and') |
864
|
|
|
); |
865
|
|
|
|
866
|
|
|
break; |
867
|
|
|
case $astNode instanceof Stmt\ClassConst: |
868
|
|
|
$node->setAttribute( |
869
|
|
|
'label', |
870
|
|
|
sprintf( |
871
|
|
|
'Constant %s = %s', |
872
|
|
|
$node[0][0]->getAttribute('label'), |
873
|
|
|
$node[0][1]->getAttribute('label') |
874
|
|
|
) |
875
|
|
|
); |
876
|
|
|
|
877
|
|
|
unset($node[0]); |
878
|
|
|
|
879
|
|
|
break; |
880
|
|
|
case $astNode instanceof Const_: |
881
|
|
|
$node->setAttribute( |
882
|
|
|
'label', |
883
|
|
|
sprintf( |
884
|
|
|
'%s', |
885
|
|
|
$node[0]->getAttribute('label') |
886
|
|
|
) |
887
|
|
|
); |
888
|
|
|
|
889
|
|
|
break; |
890
|
|
|
case $astNode instanceof Scalar\MagicConst\Class_: |
891
|
|
|
$node->setAttribute( |
892
|
|
|
'label', |
893
|
|
|
sprintf('%s', '__CLASS__') |
894
|
|
|
); |
895
|
|
|
unset($node[0], $node[1]); |
896
|
|
|
|
897
|
|
|
break; |
898
|
|
|
case $astNode instanceof Stmt\Case_: |
899
|
|
|
$node->setAttribute( |
900
|
|
|
'label', |
901
|
|
|
sprintf( |
902
|
|
|
'Case %s', |
903
|
|
|
$node[0]->getAttribute('label') |
904
|
|
|
) |
905
|
|
|
); |
906
|
|
|
unset($node[0]); |
907
|
|
|
|
908
|
|
|
break; |
909
|
|
|
case $astNode instanceof Stmt\Switch_: |
910
|
|
|
$node->setAttribute( |
911
|
|
|
'label', |
912
|
|
|
sprintf('Switch | Case') |
913
|
|
|
); |
914
|
|
|
|
915
|
|
|
break; |
916
|
|
|
case $astNode instanceof Scalar\MagicConst\Dir: |
917
|
|
|
$node->setAttribute( |
918
|
|
|
'label', |
919
|
|
|
sprintf('__DIR__') |
920
|
|
|
); |
921
|
|
|
|
922
|
|
|
break; |
923
|
|
|
case $astNode instanceof Expr\Include_: |
924
|
|
|
$node->setAttribute( |
925
|
|
|
'label', |
926
|
|
|
sprintf('Include') |
927
|
|
|
); |
928
|
|
|
|
929
|
|
|
break; |
930
|
|
|
case $astNode instanceof Expr\ClosureUse: |
931
|
|
|
$node->setAttribute( |
932
|
|
|
'label', |
933
|
|
|
sprintf( |
934
|
|
|
'Closure %s', |
935
|
|
|
$node[0]->getAttribute('label') |
936
|
|
|
) |
937
|
|
|
); |
938
|
|
|
unset($node[0]); |
939
|
|
|
|
940
|
|
|
break; |
941
|
|
|
case $astNode instanceof Stmt\InlineHTML: |
942
|
|
|
$node->setAttribute( |
943
|
|
|
'label', |
944
|
|
|
sprintf( |
945
|
|
|
'Inline HTML' |
946
|
|
|
) |
947
|
|
|
); |
948
|
|
|
|
949
|
|
|
break; |
950
|
|
|
case $astNode instanceof Stmt\Interface_: |
951
|
|
|
$node->setAttribute( |
952
|
|
|
'label', |
953
|
|
|
sprintf( |
954
|
|
|
'Interface %s', |
955
|
|
|
$node[0]->getAttribute('label') |
956
|
|
|
) |
957
|
|
|
); |
958
|
|
|
|
959
|
|
|
break; |
960
|
|
|
} |
961
|
|
|
} |
962
|
|
|
} |
963
|
|
|
|