1<?php declare(strict_types=1); 2 3namespace PhpParser\NodeVisitor; 4 5use PhpParser\Node; 6use PhpParser\Node\Expr; 7use PhpParser\NodeTraverser; 8 9class FirstFindingVisitorTest extends \PHPUnit\Framework\TestCase { 10 public function testFindFirstVariable(): void { 11 $traverser = new NodeTraverser(); 12 $visitor = new FirstFindingVisitor(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\Variable('b')); 18 $stmts = [new Node\Stmt\Expression($assign)]; 19 20 $traverser->traverse($stmts); 21 $this->assertSame($assign->var, $visitor->getFoundNode()); 22 } 23 24 public function testFindNone(): void { 25 $traverser = new NodeTraverser(); 26 $visitor = new FirstFindingVisitor(function (Node $node) { 27 return $node instanceof Node\Expr\BinaryOp; 28 }); 29 $traverser->addVisitor($visitor); 30 31 $assign = new Expr\Assign(new Expr\Variable('a'), new Expr\Variable('b')); 32 $stmts = [new Node\Stmt\Expression($assign)]; 33 34 $traverser->traverse($stmts); 35 $this->assertNull($visitor->getFoundNode()); 36 } 37} 38