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

Conditional return type + Union Types = unexpected result #9860

Closed
LastDragon-ru opened this issue Sep 7, 2023 · 2 comments
Closed

Conditional return type + Union Types = unexpected result #9860

LastDragon-ru opened this issue Sep 7, 2023 · 2 comments
Labels
Milestone

Comments

@LastDragon-ru
Copy link

LastDragon-ru commented Sep 7, 2023

Bug report

If we have something like

/**
 * @return ($b is A ? ANode : ($b is B ? BNode : CNode))
 */
public function b(A|B|C $b): ANode|BNode|CNode {
    return match(true) {
        $b instanceof A => new ANode(),
        $b instanceof B => new BNode(),
        default => new CNode(),
     };
}

PHPStan will resolve the return type of b() as b(A) => ANode, b(B) => BNode which are expected. But for b(A|B) it will report that the return type is ANode|BNode|CNode. This is not true, the type should be ANode|BNode.

Code snippet that reproduces the problem

https://phpstan.org/r/9d568b79-afd0-48b3-a379-50127980fbb5

Expected output

ANode|BNode

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

Yep 😋

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src#2641

@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 Oct 28, 2023
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