Skip to content

Else after if (isset) on union of array shapes not narrowed #12401

@RobertMe

Description

@RobertMe

Bug report

Hi,

When I have a variable of type array{a: string}|array{b: string} (i.e. union of two array shapes, one with key a and one with key b) I would expect that after an if (isset($arr['a'])) check the type is array{a: string} and the impossible type in the union is removed. And I would expect the opposite to happen for remaining branches (i.e.: remove all types which already applied), meaning that if it's followed by an else that the only possible type is array{b: string}. But this isn't the case. While if / elseif properly removes invalid types from the union, it seems like handled types aren't removed for the remainder of the elseif / else cases.

Code snippet that reproduces the problem

https://phpstan.org/r/cdc39146-ba14-4841-85b4-889368a2c09d

Expected output

On line 10 I would expect Dumped type: array{c: string} and on line 18 I would expect Dumped type: *NEVER* (because no values in the union remain)

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

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions