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

"no value type specified in iterable type" for interface that extends IteratorAggregate with key and value types set explicitly via @extends #2740

Closed
ezzatron opened this issue Dec 15, 2019 · 3 comments
Labels
Milestone

Comments

@ezzatron
Copy link
Contributor

Bug report

When implementing a specific configuration of "nestable" iterable types, PHPStan will produce errors that claim "no value type specified in iterable type" for types that have been explicitly "un-genericised" using PHPDoc annotations.

This is a pretty strange use case that has been distilled from a more complex real-world case. The actual versions of the types represented here are:

Code snippet that reproduces the problem

From https://phpstan.org/r/2594d7d7-d75e-4916-b924-f2120ab028c2:

<?php

/**
 * A collection that can contain members.
 *
 * @extends IteratorAggregate<int,Member>
 */
interface Collection extends IteratorAggregate
{
}

/**
 * A member of a collection. Also a collection containing only itself.
 *
 * In the real world, this would contain additional methods.
 */
interface Member extends Collection
{
}

class MemberImpl implements Member
{
    /**
     * @return Iterator<int,Member>
     */
    public function getIterator(): Iterator
    {
        return new ArrayIterator([$this]);
    }
}

class CollectionImpl implements Collection
{
    public function __construct(Member ...$members)
    {
        $this->members = $members;
    }

    /**
     * @return Member
     */
    public function getMember(): Member
    {
        return new MemberImpl();
    }

    /**
     * @return Iterator<int,Member>
     */
    public function getIterator(): Iterator
    {
        return new ArrayIterator($this->members);
    }

    /**
     * @var array<int,Member>
     */
    private $members;
}

Expected output

PHPStan should not produce an error. Instead it produces an error like:

Method CollectionImpl::getMember() return type has no value type specified in iterable type Member.

@ondrejmirtes ondrejmirtes added this to the Generics milestone Dec 30, 2019
@ondrejmirtes
Copy link
Member

Fixed, thank you! phpstan/phpstan-src@65e578c

@ezzatron
Copy link
Contributor Author

ezzatron commented Jan 5, 2020

Thank you for the fix!

@lock
Copy link

lock bot commented Feb 5, 2020

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.

@lock lock bot locked as resolved and limited conversation to collaborators Feb 5, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants