Skip to content

Commit

Permalink
Improve performance of find() and findFirst() when passed $nodes is e…
Browse files Browse the repository at this point in the history
…mpty array
  • Loading branch information
samsonasik authored and nikic committed Oct 7, 2023
1 parent 4e27a17 commit 481fec4
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/PhpParser/NodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ class NodeFinder {
* @return Node[] Found nodes satisfying the filter callback
*/
public function find($nodes, callable $filter): array {
if ($nodes === []) {
return [];
}

if (!is_array($nodes)) {
$nodes = [$nodes];
}
Expand Down Expand Up @@ -52,6 +56,10 @@ public function findInstanceOf($nodes, string $class): array {
* @return null|Node Found node (or null if none found)
*/
public function findFirst($nodes, callable $filter): ?Node {
if ($nodes === []) {
return null;
}

if (!is_array($nodes)) {
$nodes = [$nodes];
}
Expand Down

0 comments on commit 481fec4

Please sign in to comment.