Skip to content

Commit

Permalink
[PhpParser] Alternative PR for findInstancesOfScoped() to keep existi…
Browse files Browse the repository at this point in the history
…ng performance
  • Loading branch information
samsonasik committed Sep 22, 2024
1 parent c5940d2 commit c56c720
Showing 1 changed file with 26 additions and 9 deletions.
35 changes: 26 additions & 9 deletions src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,23 +221,28 @@ function (Node $subNode) use (&$returns): ?int {
}

/**
* @api to be used
*
* @template T of Node
* @param array<class-string<T>>|class-string<T> $types
* @return array<T>
* @param Node[] $nodes
* @param class-string<T>|array<class-string<T>> $type
* @return T[]
*/
public function findInstancesOfInFunctionLikeScoped(
ClassMethod | Function_ | Closure $functionLike,
string|array $types
): array {
if (is_string($types)) {
$types = [$types];
public function findInstancesOfScoped(array $nodes, string|array $type): array
{
// here verify only pass single nodes as FunctionLike
if (count($nodes) === 1
&& ($nodes[0] instanceof ClassMethod || $nodes[0] instanceof Function_ || $nodes[0] instanceof Closure)) {
$nodes = (array) $nodes[0]->stmts;
}

$types = is_string($type) ? [$type] : $type;

/** @var T[] $foundNodes */
$foundNodes = [];

$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
(array) $functionLike->stmts,
$nodes,
static function (Node $subNode) use ($types, &$foundNodes): ?int {
if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
Expand All @@ -257,6 +262,18 @@ static function (Node $subNode) use ($types, &$foundNodes): ?int {
return $foundNodes;
}

/**
* @template T of Node
* @param array<class-string<T>>|class-string<T> $types
* @return array<T>
*/
public function findInstancesOfInFunctionLikeScoped(
ClassMethod | Function_ | Closure $functionLike,
string|array $types
): array {
return $this->findInstancesOfScoped([$functionLike], $types);
}

/**
* @param callable(Node $node): bool $filter
*/
Expand Down

0 comments on commit c56c720

Please sign in to comment.