2017-11-02 18:56:01 +01:00
|
|
|
<?php declare(strict_types=1);
|
2017-01-21 15:41:24 +01:00
|
|
|
|
|
|
|
namespace PhpParser\NodeVisitor;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\NodeTraverser;
|
2017-04-27 18:14:07 +02:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-01-21 15:41:24 +01:00
|
|
|
|
2017-04-27 18:14:07 +02:00
|
|
|
class FindingVisitorTest extends TestCase
|
|
|
|
{
|
2017-01-21 15:41:24 +01:00
|
|
|
public function testFindVariables() {
|
|
|
|
$traverser = new NodeTraverser();
|
2017-01-29 23:20:53 +01:00
|
|
|
$visitor = new FindingVisitor(function(Node $node) {
|
2017-01-21 15:41:24 +01:00
|
|
|
return $node instanceof Node\Expr\Variable;
|
|
|
|
});
|
|
|
|
$traverser->addVisitor($visitor);
|
|
|
|
|
|
|
|
$assign = new Expr\Assign(new Expr\Variable('a'), new Expr\BinaryOp\Concat(
|
|
|
|
new Expr\Variable('b'), new Expr\Variable('c')
|
|
|
|
));
|
|
|
|
$stmts = [new Node\Stmt\Expression($assign)];
|
|
|
|
|
|
|
|
$traverser->traverse($stmts);
|
|
|
|
$this->assertSame([
|
|
|
|
$assign->var,
|
|
|
|
$assign->expr->left,
|
|
|
|
$assign->expr->right,
|
|
|
|
], $visitor->getFoundNodes());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindAll() {
|
|
|
|
$traverser = new NodeTraverser();
|
2017-01-29 23:20:53 +01:00
|
|
|
$visitor = new FindingVisitor(function(Node $node) {
|
2017-01-21 15:41:24 +01:00
|
|
|
return true; // All nodes
|
|
|
|
});
|
|
|
|
$traverser->addVisitor($visitor);
|
|
|
|
|
|
|
|
$assign = new Expr\Assign(new Expr\Variable('a'), new Expr\BinaryOp\Concat(
|
|
|
|
new Expr\Variable('b'), new Expr\Variable('c')
|
|
|
|
));
|
|
|
|
$stmts = [new Node\Stmt\Expression($assign)];
|
|
|
|
|
|
|
|
$traverser->traverse($stmts);
|
|
|
|
$this->assertSame([
|
|
|
|
$stmts[0],
|
|
|
|
$assign,
|
|
|
|
$assign->var,
|
|
|
|
$assign->expr,
|
|
|
|
$assign->expr->left,
|
|
|
|
$assign->expr->right,
|
|
|
|
], $visitor->getFoundNodes());
|
|
|
|
}
|
2017-04-27 18:14:07 +02:00
|
|
|
}
|