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

Infinity loop while handle some method annotations #5231

Closed
don-tre opened this issue Jun 29, 2021 · 3 comments
Closed

Infinity loop while handle some method annotations #5231

don-tre opened this issue Jun 29, 2021 · 3 comments

Comments

@don-tre
Copy link

don-tre commented Jun 29, 2021

Bug report

Code snippet that reproduces the problem

// stub
class SomeClass
{

}

// class with bug

/**
 *     annotation must be exists:
 * @method SomeClass[]|SomeParentCollection getSorted()
 */
class SomeParentCollection implements Iterator
{
    /** @var SomeClass[] */
    protected $collection;

    public function findByName(string $name): ?SomeClass
    {
        foreach ($this->collection as $item) {
            if ((string)$item === $name) {
                return $item;
            }
        }

        return null;
    }

    // must be exists!
    public function existsByKey(string $name): bool
    {
        return $this->findByName($name) !== null;
    }

    public function getSorted(callable $comparator): self
    {
        $sortedCollection = $this->collection;
        usort($sortedCollection, $comparator);

        $filtered = array_values($sortedCollection);

        return new static(...$filtered);
    }

    public function rewind(): void
    {
        reset($this->collection);
    }

    public function current()
    {
        return current($this->collection);
    }

    /**
     * @return bool|float|int|string|null
     */
    public function key()
    {
        return key($this->collection);
    }

    /**
     * @return mixed|void
     */
    public function next()
    {
        return next($this->collection);
    }

    public function valid(): bool
    {
        return isset($this->collection[key($this->collection)]);
    }
}

I debug this code.
Loop begins in \PHPStan\Reflection\Annotations\AnnotationsMethodsClassReflectionExtension::findClassReflectionWithMethod

The problem begins from existsByKey, phpstan goes to findByName, then try get info about current() method, checks phpdoc and get infinity loop while triyng to parse "@method SomeClass[]|SomeParentCollection getSorted()".
Phpstan successfully parse SomeClass[], but fails on next step SomeParentCollection.

Requirements for bug:

  • annotation with 2 return type "@method SomeClass[]|SomeParentCollection getSorted()"
  • method that calls another method with foreach.

I tried to remove one of this requirement, all works fine.

I tried it on phpstan.org and I got "An error occured. Please try again later."

Expected output

Result exists.

Did PHPStan help you today? Did it make you happy in any way?

Sure. I make own rules and it helps to provide code standards in our team. Great product!

@mergeable
Copy link

mergeable bot commented Jun 29, 2021

This bug report is missing a link to reproduction on phpstan.org.

It will most likely be closed after manual review.

@ondrejmirtes
Copy link
Member

Hi, thanks for the report, this should fix the problem: phpstan/phpstan-src@8fd9667

Please test it by installing dev-master: composer require --dev phpstan/phpstan:dev-master. If you have Composer 2.1.0 or later, it will be much faster! Otherwise you need to add --prefer-dist to that too.

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 13, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants