Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add findInstancesOfScoped() to BetterNodeFinder, to ease re-use #6323

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 23 additions & 6 deletions src/PhpParser/Node/BetterNodeFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,26 +166,43 @@ public function hasInstancesOfInFunctionLikeScoped(
$types = [$types];
}

$isFoundNode = false;
return $this->findInstancesOfScoped((array) $functionLike->stmts, $types) !== [];
}

/**
* @api to be used
*
* @template T of Node
* @param Node[] $nodes
* @param class-string<T>|array<class-string<T>> $type
* @return T[]
*/
public function findInstancesOfScoped(array $nodes, string|array $type): array
{
/** @var T[] $foundNodes */
$foundNodes = [];

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

$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
(array) $functionLike->stmts,
static function (Node $subNode) use ($types, &$isFoundNode): ?int {
$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;
}

foreach ($types as $type) {
if ($subNode instanceof $type) {
$isFoundNode = true;
return NodeTraverser::STOP_TRAVERSAL;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stop traversal is essential here to not looking more if found, probably make another method with $stopOnFound flag so we have same performance

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, there is already findInstancesOfInFunctionLikeScoped, if there is a $nodes needed, probably better to allow array there, with make another private method for re-use as both method:

public function findInstancesOfInFunctionLikeScoped(

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion is, make it allow FunctionLike in array of nodes[0]:

  1. Keep hasInstancesOfInFunctionLikeScoped as is for performance reason
  2. Modify the method to allow single $nodes, as FunctionLike reuse
    /**
     * @api to be used
     *
     * @template T of Node
     * @param Node[] $nodes
     * @param class-string<T>|array<class-string<T>> $type
     * @return T[]
     */
    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;
        }

        if (is_string($types)) {
            $types = [$types];
        }

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

        $this->simpleCallableNodeTraverser->traverseNodesWithCallable(
            $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;
                }

                foreach ($types as $type) {
                    if ($subNode instanceof $type) {
                        $foundNodes[] = $subNode;
                        return null;
                    }
                }

                return null;
            }
        );

        return $foundNodes;
  1. Then, in findInstancesOfInFunctionLikeScoped, change to just call it:
    public function findInstancesOfInFunctionLikeScoped(
        ClassMethod | Function_ | Closure $functionLike,
        string|array $types
    ): array {
        return $this->findInstancesOfScoped([$functionLike], $types);
    }

I will create an alternative PR for it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good 👍

One of methods should return "all", the other only "first" for performance reasons.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$foundNodes[] = $subNode;
return null;
}
}

return null;
}
);

return $isFoundNode;
return $foundNodes;
}

/**
Expand Down