阅读文档我可以转储收集的信息,但是没有迭代集合以按特定节点类型过滤的示例。
use PhpParser\ParserFactory;
use PhpParser\Error;
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
try {
$statements = $parser->parse(file_get_contents("SomeClass.php"));
$methods = array_filter($statements, function($statement) {
// some form of filtering for methods
});
} catch (Error $exception) {
echo "parse error ~> ", $exception->getMessage();
}
答案 0 :(得分:1)
use PhpParser\{Node, NodeFinder, ParserFactory};
/** Parse file. */
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$stmts = $parser->parse(file_get_contents("SomeClass.php"));
/** Traverse. */
$nodeFinder = new NodeFinder();
$methods = $nodeFinder->findInstanceOf($stmts, Node\Stmt\ClassMethod::class);
/** Dump. */
var_dump($methods);
use PhpParser\{Node, NodeTraverser, NodeVisitorAbstract, ParserFactory};
/** Filter definition. */
$methods = [];
$traverser = new NodeTraverser();
$traverser->addVisitor(new class extends NodeVisitorAbstract {
function enterNode(Node $node) {
if ($node instanceof Node\Stmt\ClassMethod) {
global $methods;
$methods[] = $node;
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
}
}
});
/** Parse file. */
$parser = (new ParserFactory)->create(ParserFactory::PREFER_PHP7);
$stmts = $parser->parse(file_get_contents("SomeClass.php"));
/** Traverse using our filter. */
$traverser->traverse($stmts);
/** Dump. */
var_dump( $methods );