Skip to content

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

@ezzatron

Description

@ezzatron

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions