2017-11-02 18:56:01 +01:00
|
|
|
<?php declare(strict_types=1);
|
2017-01-29 23:20:53 +01:00
|
|
|
|
|
|
|
namespace PhpParser;
|
|
|
|
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
|
2019-01-19 11:18:00 +01:00
|
|
|
class NodeFinderTest extends \PHPUnit\Framework\TestCase
|
2017-04-27 18:14:07 +02:00
|
|
|
{
|
2017-01-29 23:20:53 +01:00
|
|
|
private function getStmtsAndVars() {
|
|
|
|
$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)];
|
|
|
|
$vars = [$assign->var, $assign->expr->left, $assign->expr->right];
|
|
|
|
return [$stmts, $vars];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFind() {
|
|
|
|
$finder = new NodeFinder;
|
|
|
|
list($stmts, $vars) = $this->getStmtsAndVars();
|
|
|
|
$varFilter = function(Node $node) {
|
|
|
|
return $node instanceof Expr\Variable;
|
|
|
|
};
|
|
|
|
$this->assertSame($vars, $finder->find($stmts, $varFilter));
|
|
|
|
$this->assertSame($vars, $finder->find($stmts[0], $varFilter));
|
|
|
|
|
|
|
|
$noneFilter = function () { return false; };
|
|
|
|
$this->assertSame([], $finder->find($stmts, $noneFilter));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindInstanceOf() {
|
|
|
|
$finder = new NodeFinder;
|
|
|
|
list($stmts, $vars) = $this->getStmtsAndVars();
|
|
|
|
$this->assertSame($vars, $finder->findInstanceOf($stmts, Expr\Variable::class));
|
|
|
|
$this->assertSame($vars, $finder->findInstanceOf($stmts[0], Expr\Variable::class));
|
|
|
|
$this->assertSame([], $finder->findInstanceOf($stmts, Expr\BinaryOp\Mul::class));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindFirst() {
|
|
|
|
$finder = new NodeFinder;
|
|
|
|
list($stmts, $vars) = $this->getStmtsAndVars();
|
|
|
|
$varFilter = function(Node $node) {
|
|
|
|
return $node instanceof Expr\Variable;
|
|
|
|
};
|
|
|
|
$this->assertSame($vars[0], $finder->findFirst($stmts, $varFilter));
|
|
|
|
$this->assertSame($vars[0], $finder->findFirst($stmts[0], $varFilter));
|
|
|
|
|
|
|
|
$noneFilter = function () { return false; };
|
2017-12-12 12:02:49 +01:00
|
|
|
$this->assertNull($finder->findFirst($stmts, $noneFilter));
|
2017-01-29 23:20:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindFirstInstanceOf() {
|
|
|
|
$finder = new NodeFinder;
|
|
|
|
list($stmts, $vars) = $this->getStmtsAndVars();
|
|
|
|
$this->assertSame($vars[0], $finder->findFirstInstanceOf($stmts, Expr\Variable::class));
|
|
|
|
$this->assertSame($vars[0], $finder->findFirstInstanceOf($stmts[0], Expr\Variable::class));
|
2017-12-12 12:02:49 +01:00
|
|
|
$this->assertNull($finder->findFirstInstanceOf($stmts, Expr\BinaryOp\Mul::class));
|
2017-01-29 23:20:53 +01:00
|
|
|
}
|
2017-04-27 18:14:07 +02:00
|
|
|
}
|