Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
patinthehat committed Jul 5, 2021
1 parent 3ec2af7 commit 2c35e8e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 8 deletions.
40 changes: 39 additions & 1 deletion src/Searcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,53 @@ protected function findAllCalls(array $ast): array

protected function findCalls(array $ast, string $class, ?string $nodeNameProp, array $names): array
{
// $result = [];
//
// $nodeFinder = new NodeFinder();
//
// $nodes = $nodeFinder->findInstanceOf($ast, $class);
//
// foreach ($nodes as $node) {
// $result[] = $node->jsonSerialize();
// }
//
// file_put_contents(getcwd() . '/test3.json', json_encode($result, JSON_PRETTY_PRINT));
// die();

// return [];

$nodeFinder = new NodeFinder();

$nodes = $nodeFinder->findInstanceOf($ast, $class);


if (! $nodeNameProp) {
return $nodes;
}

return array_filter($nodes, function (Node $node) use ($names, $nodeNameProp) {
$name = '';

if ($node instanceof FuncCall) {
$name = $node->name->parts[0];
}

if ($node instanceof Node\Expr\StaticCall) {
$name = $node->name->name;
}

if ($node instanceof Node\Expr\New_) {
$name = $node->class->name->name;
}

if ($node instanceof Node\Expr\Assign) {
$name = $node->var->name;
}

if (!empty($name)) {
return in_array($name, $names, true);
}

if (isset($node->{$nodeNameProp}->name)) {
return in_array($node->{$nodeNameProp}->name, $names, true);
}
Expand Down Expand Up @@ -183,7 +221,7 @@ protected function traverseNodes(FileSearchResults $results, array $nodes): void
{
$traverser = new NodeTraverser();

$traverser->addVisitor(new FunctionCallVisitor($results));
$traverser->addVisitor(new FunctionCallVisitor($results, $this->functions));

$traverser->traverse($nodes);
}
Expand Down
19 changes: 12 additions & 7 deletions src/Visitors/FunctionCallVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,26 @@ class FunctionCallVisitor extends NodeVisitorAbstract
/** @var FileSearchResults */
protected $results;

public function __construct(FileSearchResults $results)
protected $functionNames = [];

public function __construct(FileSearchResults $results, array $functionNames)
{
$this->results = $results;
$this->functionNames = $functionNames;
}

public function enterNode(Node $node)
{
if ($node instanceof FuncCall) {
$location = FunctionCallLocation::create(
$node->name->parts[0],
$node->getStartLine(),
$node->getEndLine()
);
if (in_array($node->name->parts[0], $this->functionNames, true)) {
$location = FunctionCallLocation::create(
$node->name->parts[0],
$node->getStartLine(),
$node->getEndLine()
);

$this->results->addLocation($location);
$this->results->addLocation($location);
}
}

if ($node instanceof Node\Expr\StaticCall) {
Expand Down

0 comments on commit 2c35e8e

Please sign in to comment.