Skip to content

Commit

Permalink
Performance: use direct find() instead of lookup all nodes then filte…
Browse files Browse the repository at this point in the history
…r on BetterNodeFinder (#3507)
  • Loading branch information
samsonasik committed Mar 23, 2023
1 parent b8a0d66 commit 8609d3b
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -378,12 +378,10 @@ public function findSameNamedExprs(Expr | Variable | Property | PropertyFetch |
return [];
}

$variables = $this->findInstancesOf($scopeNode, [Variable::class]);

return array_filter(
$variables,
fn (Variable $variable): bool => $this->nodeNameResolver->isName($variable, $exprName)
);
/** @var Variable[] $variables */
$variables = $this->find($scopeNode, fn (Node $node): bool =>
$node instanceof Variable && $this->nodeNameResolver->isName($node, $exprName));
return $variables;
}

if ($expr instanceof Property) {
Expand All @@ -399,13 +397,12 @@ public function findSameNamedExprs(Expr | Variable | Property | PropertyFetch |
return [];
}

$propertyFetches = $this->findInstancesOf($scopeNode, [PropertyFetch::class, StaticPropertyFetch::class]);
/** @var PropertyFetch[]|StaticPropertyFetch[] $propertyFetches */
$propertyFetches = $this->find($scopeNode, fn (Node $node): bool =>
($node instanceof PropertyFetch || $node instanceof StaticPropertyFetch)
&& $this->nodeNameResolver->isName($node->name, $exprName));

return array_filter(
$propertyFetches,
fn (PropertyFetch | StaticPropertyFetch $propertyFetch): bool =>
$this->nodeNameResolver->isName($propertyFetch->name, $exprName)
);
return $propertyFetches;
}

/**
Expand Down

0 comments on commit 8609d3b

Please sign in to comment.