File: NodeTraverserTest.php

package info (click to toggle)
php-parser 5.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 4,532 kB
  • sloc: php: 23,585; yacc: 1,272; makefile: 39; sh: 8
file content (476 lines) | stat: -rw-r--r-- 17,863 bytes parent folder | download | duplicates (3)
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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<?php declare(strict_types=1);

namespace PhpParser;

use PhpParser\Node\Expr;
use PhpParser\Node\Scalar\Int_;
use PhpParser\Node\Scalar\String_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\If_;

class NodeTraverserTest extends \PHPUnit\Framework\TestCase {
    public function testNonModifying(): void {
        $str1Node = new String_('Foo');
        $str2Node = new String_('Bar');
        $echoNode = new Node\Stmt\Echo_([$str1Node, $str2Node]);
        $stmts    = [$echoNode];

        $visitor = new NodeVisitorForTesting();
        $traverser = new NodeTraverser();
        $traverser->addVisitor($visitor);

        $this->assertEquals($stmts, $traverser->traverse($stmts));
        $this->assertEquals([
            ['beforeTraverse', $stmts],
            ['enterNode', $echoNode],
            ['enterNode', $str1Node],
            ['leaveNode', $str1Node],
            ['enterNode', $str2Node],
            ['leaveNode', $str2Node],
            ['leaveNode', $echoNode],
            ['afterTraverse', $stmts],
        ], $visitor->trace);
    }

    public function testModifying(): void {
        $str1Node  = new String_('Foo');
        $str2Node  = new String_('Bar');
        $printNode = new Expr\Print_($str1Node);

        // Visitor 2 performs changes, visitors 1 and 3 observe the changes.
        $visitor1 = new NodeVisitorForTesting();
        $visitor2 = new NodeVisitorForTesting([
            ['beforeTraverse', [], [$str1Node]],
            ['enterNode', $str1Node, $printNode],
            ['enterNode', $str1Node, $str2Node],
            ['leaveNode', $str2Node, $str1Node],
            ['leaveNode', $printNode, $str1Node],
            ['afterTraverse', [$str1Node], []],
        ]);
        $visitor3 = new NodeVisitorForTesting();

        $traverser = new NodeTraverser($visitor1, $visitor2, $visitor3);

        // as all operations are reversed we end where we start
        $this->assertEquals([], $traverser->traverse([]));
        $this->assertEquals([
            // Sees nodes before changes on entry.
            ['beforeTraverse', []],
            ['enterNode', $str1Node],
            ['enterNode', $str1Node],
            // Sees nodes after changes on leave.
            ['leaveNode', $str1Node],
            ['leaveNode', $str1Node],
            ['afterTraverse', []],
        ], $visitor1->trace);
        $this->assertEquals([
            // Sees nodes after changes on entry.
            ['beforeTraverse', [$str1Node]],
            ['enterNode', $printNode],
            ['enterNode', $str2Node],
            // Sees nodes before changes on leave.
            ['leaveNode', $str2Node],
            ['leaveNode', $printNode],
            ['afterTraverse', [$str1Node]],
        ], $visitor3->trace);
    }

    public function testRemoveFromLeave(): void {
        $str1Node = new String_('Foo');
        $str2Node = new String_('Bar');

        $visitor = new NodeVisitorForTesting([
            ['leaveNode', $str1Node, NodeVisitor::REMOVE_NODE],
        ]);
        $visitor2 = new NodeVisitorForTesting();

        $traverser = new NodeTraverser();
        $traverser->addVisitor($visitor2);
        $traverser->addVisitor($visitor);

        $stmts = [$str1Node, $str2Node];
        $this->assertEquals([$str2Node], $traverser->traverse($stmts));
        $this->assertEquals([
            ['beforeTraverse', $stmts],
            ['enterNode', $str1Node],
            ['enterNode', $str2Node],
            ['leaveNode', $str2Node],
            ['afterTraverse', [$str2Node]],
        ], $visitor2->trace);
    }

    public function testRemoveFromEnter(): void {
        $str1Node = new String_('Foo');
        $str2Node = new String_('Bar');

        $visitor = new NodeVisitorForTesting([
            ['enterNode', $str1Node, NodeVisitor::REMOVE_NODE],
        ]);
        $visitor2 = new NodeVisitorForTesting();

        $traverser = new NodeTraverser();
        $traverser->addVisitor($visitor);
        $traverser->addVisitor($visitor2);

        $stmts = [$str1Node, $str2Node];
        $this->assertEquals([$str2Node], $traverser->traverse($stmts));
        $this->assertEquals([
            ['beforeTraverse', $stmts],
            ['enterNode', $str2Node],
            ['leaveNode', $str2Node],
            ['afterTraverse', [$str2Node]],
        ], $visitor2->trace);
    }

    public function testReturnArrayFromEnter(): void {
        $str1Node = new String_('Str1');
        $str2Node = new String_('Str2');
        $str3Node = new String_('Str3');
        $str4Node = new String_('Str4');

        $visitor = new NodeVisitorForTesting([
            ['enterNode', $str1Node, [$str3Node, $str4Node]],
        ]);
        $visitor2 = new NodeVisitorForTesting();

        $traverser = new NodeTraverser();
        $traverser->addVisitor($visitor);
        $traverser->addVisitor($visitor2);

        $stmts = [$str1Node, $str2Node];
        $this->assertEquals([$str3Node, $str4Node, $str2Node], $traverser->traverse($stmts));
        $this->assertEquals([
            ['beforeTraverse', $stmts],
            ['enterNode', $str2Node],
            ['leaveNode', $str2Node],
            ['afterTraverse', [$str3Node, $str4Node, $str2Node]],
        ], $visitor2->trace);
    }

    public function testMerge(): void {
        $strStart  = new String_('Start');
        $strMiddle = new String_('End');
        $strEnd    = new String_('Middle');
        $strR1     = new String_('Replacement 1');
        $strR2     = new String_('Replacement 2');

        $visitor = new NodeVisitorForTesting([
            ['leaveNode', $strMiddle, [$strR1, $strR2]],
        ]);

        $traverser = new NodeTraverser();
        $traverser->addVisitor($visitor);

        $this->assertEquals(
            [$strStart, $strR1, $strR2, $strEnd],
            $traverser->traverse([$strStart, $strMiddle, $strEnd])
        );
    }

    public function testInvalidDeepArray(): void {
        $this->expectException(\LogicException::class);
        $this->expectExceptionMessage('Invalid node structure: Contains nested arrays');
        $strNode = new String_('Foo');
        $stmts = [[[$strNode]]];

        $traverser = new NodeTraverser();
        $this->assertEquals($stmts, $traverser->traverse($stmts));
    }

    public function testDontTraverseChildren(): void {
        $strNode = new String_('str');
        $printNode = new Expr\Print_($strNode);
        $varNode = new Expr\Variable('foo');
        $mulNode = new Expr\BinaryOp\Mul($varNode, $varNode);
        $negNode = new Expr\UnaryMinus($mulNode);
        $stmts = [$printNode, $negNode];

        $visitor1 = new NodeVisitorForTesting([
            ['enterNode', $printNode, NodeVisitor::DONT_TRAVERSE_CHILDREN],
        ]);
        $visitor2 = new NodeVisitorForTesting([
            ['enterNode', $mulNode, NodeVisitor::DONT_TRAVERSE_CHILDREN],
        ]);

        $expectedTrace = [
            ['beforeTraverse', $stmts],
            ['enterNode', $printNode],
            ['leaveNode', $printNode],
            ['enterNode', $negNode],
            ['enterNode', $mulNode],
            ['leaveNode', $mulNode],
            ['leaveNode', $negNode],
            ['afterTraverse', $stmts],
        ];

        $traverser = new NodeTraverser();
        $traverser->addVisitor($visitor1);
        $traverser->addVisitor($visitor2);

        $this->assertEquals($stmts, $traverser->traverse($stmts));
        $this->assertEquals($expectedTrace, $visitor1->trace);
        $this->assertEquals($expectedTrace, $visitor2->trace);
    }

    public function testDontTraverseCurrentAndChildren(): void {
        // print 'str'; -($foo * $foo);
        $strNode = new String_('str');
        $printNode = new Expr\Print_($strNode);
        $varNode = new Expr\Variable('foo');
        $mulNode = new Expr\BinaryOp\Mul($varNode, $varNode);
        $divNode = new Expr\BinaryOp\Div($varNode, $varNode);
        $negNode = new Expr\UnaryMinus($mulNode);
        $stmts = [$printNode, $negNode];

        $visitor1 = new NodeVisitorForTesting([
            ['enterNode', $printNode, NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN],
            ['enterNode', $mulNode, NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN],
            ['leaveNode', $mulNode, $divNode],
        ]);
        $visitor2 = new NodeVisitorForTesting();

        $traverser = new NodeTraverser();
        $traverser->addVisitor($visitor1);
        $traverser->addVisitor($visitor2);

        $resultStmts = $traverser->traverse($stmts);
        $this->assertInstanceOf(Expr\BinaryOp\Div::class, $resultStmts[1]->expr);

        $this->assertEquals([
            ['beforeTraverse', $stmts],
            ['enterNode', $printNode],
            ['leaveNode', $printNode],
            ['enterNode', $negNode],
            ['enterNode', $mulNode],
            ['leaveNode', $mulNode],
            ['leaveNode', $negNode],
            ['afterTraverse', $resultStmts],
        ], $visitor1->trace);
        $this->assertEquals([
            ['beforeTraverse', $stmts],
            ['enterNode', $negNode],
            ['leaveNode', $negNode],
            ['afterTraverse', $resultStmts],
        ], $visitor2->trace);
    }

    public function testStopTraversal(): void {
        $varNode1 = new Expr\Variable('a');
        $varNode2 = new Expr\Variable('b');
        $varNode3 = new Expr\Variable('c');
        $mulNode = new Expr\BinaryOp\Mul($varNode1, $varNode2);
        $printNode = new Expr\Print_($varNode3);
        $stmts = [$mulNode, $printNode];

        // From enterNode() with array parent
        $visitor = new NodeVisitorForTesting([
            ['enterNode', $mulNode, NodeVisitor::STOP_TRAVERSAL],
        ]);
        $traverser = new NodeTraverser();
        $traverser->addVisitor($visitor);
        $this->assertEquals($stmts, $traverser->traverse($stmts));
        $this->assertEquals([
            ['beforeTraverse', $stmts],
            ['enterNode', $mulNode],
            ['afterTraverse', $stmts],
        ], $visitor->trace);

        // From enterNode with Node parent
        $visitor = new NodeVisitorForTesting([
            ['enterNode', $varNode1, NodeVisitor::STOP_TRAVERSAL],
        ]);
        $traverser = new NodeTraverser();
        $traverser->addVisitor($visitor);
        $this->assertEquals($stmts, $traverser->traverse($stmts));
        $this->assertEquals([
            ['beforeTraverse', $stmts],
            ['enterNode', $mulNode],
            ['enterNode', $varNode1],
            ['afterTraverse', $stmts],
        ], $visitor->trace);

        // From leaveNode with Node parent
        $visitor = new NodeVisitorForTesting([
            ['leaveNode', $varNode1, NodeVisitor::STOP_TRAVERSAL],
        ]);
        $traverser = new NodeTraverser();
        $traverser->addVisitor($visitor);
        $this->assertEquals($stmts, $traverser->traverse($stmts));
        $this->assertEquals([
            ['beforeTraverse', $stmts],
            ['enterNode', $mulNode],
            ['enterNode', $varNode1],
            ['leaveNode', $varNode1],
            ['afterTraverse', $stmts],
        ], $visitor->trace);

        // From leaveNode with array parent
        $visitor = new NodeVisitorForTesting([
            ['leaveNode', $mulNode, NodeVisitor::STOP_TRAVERSAL],
        ]);
        $traverser = new NodeTraverser();
        $traverser->addVisitor($visitor);
        $this->assertEquals($stmts, $traverser->traverse($stmts));
        $this->assertEquals([
            ['beforeTraverse', $stmts],
            ['enterNode', $mulNode],
            ['enterNode', $varNode1],
            ['leaveNode', $varNode1],
            ['enterNode', $varNode2],
            ['leaveNode', $varNode2],
            ['leaveNode', $mulNode],
            ['afterTraverse', $stmts],
        ], $visitor->trace);

        // Check that pending array modifications are still carried out
        $visitor = new NodeVisitorForTesting([
            ['leaveNode', $mulNode, NodeVisitor::REMOVE_NODE],
            ['enterNode', $printNode, NodeVisitor::STOP_TRAVERSAL],
        ]);
        $traverser = new NodeTraverser();
        $traverser->addVisitor($visitor);
        $this->assertEquals([$printNode], $traverser->traverse($stmts));
        $this->assertEquals([
            ['beforeTraverse', $stmts],
            ['enterNode', $mulNode],
            ['enterNode', $varNode1],
            ['leaveNode', $varNode1],
            ['enterNode', $varNode2],
            ['leaveNode', $varNode2],
            ['leaveNode', $mulNode],
            ['enterNode', $printNode],
            ['afterTraverse', [$printNode]],
        ], $visitor->trace);
    }

    public function testReplaceWithNull(): void {
        $one = new Int_(1);
        $else1 = new Else_();
        $else2 = new Else_();
        $if1 = new If_($one, ['else' => $else1]);
        $if2 = new If_($one, ['else' => $else2]);
        $stmts = [$if1, $if2];
        $visitor1 = new NodeVisitorForTesting([
            ['enterNode', $else1, NodeVisitor::REPLACE_WITH_NULL],
            ['leaveNode', $else2, NodeVisitor::REPLACE_WITH_NULL],
        ]);
        $visitor2 = new NodeVisitorForTesting();
        $traverser = new NodeTraverser();
        $traverser->addVisitor($visitor1);
        $traverser->addVisitor($visitor2);
        $newStmts = $traverser->traverse($stmts);
        $this->assertEquals([
            new If_($one),
            new If_($one),
        ], $newStmts);
        $this->assertEquals([
            ['beforeTraverse', $stmts],
            ['enterNode', $if1],
            ['enterNode', $one],
            // We never see the if1 Else node.
            ['leaveNode', $one],
            ['leaveNode', $if1],
            ['enterNode', $if2],
            ['enterNode', $one],
            ['leaveNode', $one],
            // We do see the if2 Else node, as it will only be replaced afterwards.
            ['enterNode', $else2],
            ['leaveNode', $else2],
            ['leaveNode', $if2],
            ['afterTraverse', $stmts],
        ], $visitor2->trace);
    }

    public function testRemovingVisitor(): void {
        $visitor1 = new class () extends NodeVisitorAbstract {};
        $visitor2 = new class () extends NodeVisitorAbstract {};
        $visitor3 = new class () extends NodeVisitorAbstract {};

        $traverser = new NodeTraverser();
        $traverser->addVisitor($visitor1);
        $traverser->addVisitor($visitor2);
        $traverser->addVisitor($visitor3);

        $getVisitors = (function () {
            return $this->visitors;
        })->bindTo($traverser, NodeTraverser::class);

        $preExpected = [$visitor1, $visitor2, $visitor3];
        $this->assertSame($preExpected, $getVisitors());

        $traverser->removeVisitor($visitor2);

        $postExpected = [$visitor1, $visitor3];
        $this->assertSame($postExpected, $getVisitors());
    }

    public function testNoCloneNodes(): void {
        $stmts = [new Node\Stmt\Echo_([new String_('Foo'), new String_('Bar')])];

        $traverser = new NodeTraverser();

        $this->assertSame($stmts, $traverser->traverse($stmts));
    }

    /**
     * @dataProvider provideTestInvalidReturn
     */
    public function testInvalidReturn($stmts, $visitor, $message): void {
        $this->expectException(\LogicException::class);
        $this->expectExceptionMessage($message);

        $traverser = new NodeTraverser();
        $traverser->addVisitor($visitor);
        $traverser->traverse($stmts);
    }

    public static function provideTestInvalidReturn() {
        $num = new Node\Scalar\Int_(42);
        $expr = new Node\Stmt\Expression($num);
        $stmts = [$expr];

        $visitor1 = new NodeVisitorForTesting([
            ['enterNode', $expr, 'foobar'],
        ]);
        $visitor2 = new NodeVisitorForTesting([
            ['enterNode', $num, 'foobar'],
        ]);
        $visitor3 = new NodeVisitorForTesting([
            ['leaveNode', $num, 'foobar'],
        ]);
        $visitor4 = new NodeVisitorForTesting([
            ['leaveNode', $expr, 'foobar'],
        ]);
        $visitor5 = new NodeVisitorForTesting([
            ['leaveNode', $num, [new Node\Scalar\Float_(42.0)]],
        ]);
        $visitor6 = new NodeVisitorForTesting([
            ['leaveNode', $expr, false],
        ]);
        $visitor7 = new NodeVisitorForTesting([
            ['enterNode', $expr, new Node\Scalar\Int_(42)],
        ]);
        $visitor8 = new NodeVisitorForTesting([
            ['enterNode', $num, new Node\Stmt\Return_()],
        ]);
        $visitor9 = new NodeVisitorForTesting([
            ['enterNode', $expr, NodeVisitor::REPLACE_WITH_NULL],
        ]);
        $visitor10 = new NodeVisitorForTesting([
            ['leaveNode', $expr, NodeVisitor::REPLACE_WITH_NULL],
        ]);

        return [
            [$stmts, $visitor1, 'enterNode() returned invalid value of type string'],
            [$stmts, $visitor2, 'enterNode() returned invalid value of type string'],
            [$stmts, $visitor3, 'leaveNode() returned invalid value of type string'],
            [$stmts, $visitor4, 'leaveNode() returned invalid value of type string'],
            [$stmts, $visitor5, 'leaveNode() may only return an array if the parent structure is an array'],
            [$stmts, $visitor6, 'leaveNode() returned invalid value of type bool'],
            [$stmts, $visitor7, 'Trying to replace statement (Stmt_Expression) with expression (Scalar_Int). Are you missing a Stmt_Expression wrapper?'],
            [$stmts, $visitor8, 'Trying to replace expression (Scalar_Int) with statement (Stmt_Return)'],
            [$stmts, $visitor9, 'REPLACE_WITH_NULL can not be used if the parent structure is an array'],
            [$stmts, $visitor10, 'REPLACE_WITH_NULL can not be used if the parent structure is an array'],
        ];
    }
}