Skip to content

Commit 2c35e8e

Browse files
committed
wip
1 parent 3ec2af7 commit 2c35e8e

File tree

2 files changed

+51
-8
lines changed

2 files changed

+51
-8
lines changed

src/Searcher.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,15 +138,53 @@ protected function findAllCalls(array $ast): array
138138

139139
protected function findCalls(array $ast, string $class, ?string $nodeNameProp, array $names): array
140140
{
141+
// $result = [];
142+
//
143+
// $nodeFinder = new NodeFinder();
144+
//
145+
// $nodes = $nodeFinder->findInstanceOf($ast, $class);
146+
//
147+
// foreach ($nodes as $node) {
148+
// $result[] = $node->jsonSerialize();
149+
// }
150+
//
151+
// file_put_contents(getcwd() . '/test3.json', json_encode($result, JSON_PRETTY_PRINT));
152+
// die();
153+
154+
// return [];
155+
141156
$nodeFinder = new NodeFinder();
142157

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

160+
145161
if (! $nodeNameProp) {
146162
return $nodes;
147163
}
148164

149165
return array_filter($nodes, function (Node $node) use ($names, $nodeNameProp) {
166+
$name = '';
167+
168+
if ($node instanceof FuncCall) {
169+
$name = $node->name->parts[0];
170+
}
171+
172+
if ($node instanceof Node\Expr\StaticCall) {
173+
$name = $node->name->name;
174+
}
175+
176+
if ($node instanceof Node\Expr\New_) {
177+
$name = $node->class->name->name;
178+
}
179+
180+
if ($node instanceof Node\Expr\Assign) {
181+
$name = $node->var->name;
182+
}
183+
184+
if (!empty($name)) {
185+
return in_array($name, $names, true);
186+
}
187+
150188
if (isset($node->{$nodeNameProp}->name)) {
151189
return in_array($node->{$nodeNameProp}->name, $names, true);
152190
}
@@ -183,7 +221,7 @@ protected function traverseNodes(FileSearchResults $results, array $nodes): void
183221
{
184222
$traverser = new NodeTraverser();
185223

186-
$traverser->addVisitor(new FunctionCallVisitor($results));
224+
$traverser->addVisitor(new FunctionCallVisitor($results, $this->functions));
187225

188226
$traverser->traverse($nodes);
189227
}

src/Visitors/FunctionCallVisitor.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,26 @@ class FunctionCallVisitor extends NodeVisitorAbstract
1313
/** @var FileSearchResults */
1414
protected $results;
1515

16-
public function __construct(FileSearchResults $results)
16+
protected $functionNames = [];
17+
18+
public function __construct(FileSearchResults $results, array $functionNames)
1719
{
1820
$this->results = $results;
21+
$this->functionNames = $functionNames;
1922
}
2023

2124
public function enterNode(Node $node)
2225
{
2326
if ($node instanceof FuncCall) {
24-
$location = FunctionCallLocation::create(
25-
$node->name->parts[0],
26-
$node->getStartLine(),
27-
$node->getEndLine()
28-
);
27+
if (in_array($node->name->parts[0], $this->functionNames, true)) {
28+
$location = FunctionCallLocation::create(
29+
$node->name->parts[0],
30+
$node->getStartLine(),
31+
$node->getEndLine()
32+
);
2933

30-
$this->results->addLocation($location);
34+
$this->results->addLocation($location);
35+
}
3136
}
3237

3338
if ($node instanceof Node\Expr\StaticCall) {

0 commit comments

Comments
 (0)