1<?php declare(strict_types=1); 2 3namespace PhpParser\NodeVisitor; 4 5use PhpParser\Node; 6use PhpParser\Node\Expr; 7use PhpParser\NodeTraverser; 8 9class FindingVisitorTest extends \PHPUnit\Framework\TestCase { 10 public function testFindVariables(): void { 11 $traverser = new NodeTraverser(); 12 $visitor = new FindingVisitor(function (Node $node) { 13 return $node instanceof Node\Expr\Variable; 14 }); 15 $traverser->addVisitor($visitor); 16 17 $assign = new Expr\Assign(new Expr\Variable('a'), new Expr\BinaryOp\Concat( 18 new Expr\Variable('b'), new Expr\Variable('c') 19 )); 20 $stmts = [new Node\Stmt\Expression($assign)]; 21 22 $traverser->traverse($stmts); 23 $this->assertSame([ 24 $assign->var, 25 $assign->expr->left, 26 $assign->expr->right, 27 ], $visitor->getFoundNodes()); 28 } 29 30 public function testFindAll(): void { 31 $traverser = new NodeTraverser(); 32 $visitor = new FindingVisitor(function (Node $node) { 33 return true; // All nodes 34 }); 35 $traverser->addVisitor($visitor); 36 37 $assign = new Expr\Assign(new Expr\Variable('a'), new Expr\BinaryOp\Concat( 38 new Expr\Variable('b'), new Expr\Variable('c') 39 )); 40 $stmts = [new Node\Stmt\Expression($assign)]; 41 42 $traverser->traverse($stmts); 43 $this->assertSame([ 44 $stmts[0], 45 $assign, 46 $assign->var, 47 $assign->expr, 48 $assign->expr->left, 49 $assign->expr->right, 50 ], $visitor->getFoundNodes()); 51 } 52} 53