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

Possible false positive with generics in a tree structure #4707

Closed
MidnightDesign opened this issue Mar 16, 2021 · 3 comments
Closed

Possible false positive with generics in a tree structure #4707

MidnightDesign opened this issue Mar 16, 2021 · 3 comments

Comments

@MidnightDesign
Copy link
Contributor

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.

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@d225a68

@MidnightDesign
Copy link
Contributor Author

Awesome, thank you so much!

@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 Apr 29, 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