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

[PhpParser] Alternative PR for findInstancesOfScoped() to keep existing performance #6324

Merged
merged 2 commits into from
Sep 22, 2024

Conversation

samsonasik
Copy link
Member

@samsonasik samsonasik commented Sep 22, 2024

Alternative PR for #6323 , 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>> $types
     * @return T[]
     */
    public function findInstancesOfScoped(array $nodes, string|array $types): 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);
    }

Closes #6323

@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba it is ready to review.

@TomasVotruba TomasVotruba merged commit 5170018 into main Sep 22, 2024
36 checks passed
@TomasVotruba TomasVotruba deleted the alternative-pr-for-find-instances-of-scoped branch September 22, 2024 18:15
@TomasVotruba
Copy link
Member

Looks good, thanks 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants