Skip to content

Instantly share code, notes, and snippets.

@ghostwriter
Last active May 2, 2022 19:01
Show Gist options
  • Select an option

  • Save ghostwriter/cf06f0875d57bf74fcb2c988dfc6ab7a to your computer and use it in GitHub Desktop.

Select an option

Save ghostwriter/cf06f0875d57bf74fcb2c988dfc6ab7a to your computer and use it in GitHub Desktop.
<?php
$nodeTraverser = new PhpParser\NodeTraverser;
$nodeTraverser->addVisitor(new PhpParser\NodeVisitor\NameResolver);
$visitor = new PhpParser\NodeVisitor\FindingVisitor(function (Node $node) {
return $node instanceof Node\Expr\MethodCall && $node->name instanceof Identifier;
});
$nodeTraverser->addVisitor($visitor);
$recursiveIteratorIterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(__DIR__ . '/src'),
RecursiveIteratorIterator::LEAVES_ONLY
);
$methodCallNodes = [];
foreach ($recursiveIteratorIterator as $file) {
$path = $file->getPathName();
if (!preg_match('/\.php$/', $path)) {
continue;
}
$code = file_get_contents($path);
try {
$stmts = $parser->parse($code);
} catch (PhpParser\Error $e) {
echo sprintf('Parse error:%s [%s]', $e->getMessage(), $path);
continue;
}
$visitor->path = $path;
$visitor->code = $code;
$traverser->traverse($stmts);
$methodCallNodes = [...$methodCallNodes, ...$visitor->getFoundNodes()];
}
// do work. $methodCallNodes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment