Skip to content

Possible false positive with generics in a tree structure #4707

@MidnightDesign

Description

@MidnightDesign

Bug report

Code snippet that reproduces the problem

https://phpstan.org/r/f7496069-aa93-438b-80b1-b7e957c72d5f

<?php declare(strict_types = 1);

/**
 * @template TParent of ParentNodeInterface
 */
interface ChildNodeInterface
{
    /** @return TParent */
    public function getParent(): ParentNodeInterface;
}

interface ParentNodeInterface
{
    /** @return list<ChildNodeInterface<static>> */
    public function getChildren(): array;
}

final class Block implements ParentNodeInterface
{
    /** @var list<Row> */
    private $rows = [];
	
    /** @return list<Row> */
    public function getChildren(): array
    {
        return $this->rows;
    }	
}

/** @implements ChildNodeInterface<Block> */
final class Row implements ChildNodeInterface
{
    /** @var Block $parent */
    private $parent;
	
    public function getParent(): Block {
        return $this->parent;
    }
}

Expected output

No errors

Actual output

Return type (array<int, Row>) of method Block::getChildren() should be compatible with return type (array<int, ChildNodeInterface<static(ParentNodeInterface)>>) of method ParentNodeInterface::getChildren()

I could somewhat understand the error if Block wasn't final, but in this case I can't see any problems with the types.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions