Skip to content

Empty if clause changes type of variable from T|null to T|array|null #4117

@AJenbo

Description

@AJenbo

Bug report

    /** @var array<int, T> */
    protected $items = [];

    /**
     * @return ?T
     */
	public function broken(int $key)
    {
        $item = $this->items[$key] ?? null;
        if ($item) {
        }

        return $item;
    }

Method GenericList::broken() should return T|null but returns array|T|null.

Code snippet that reproduces the problem

https://phpstan.org/r/54561da3-8e55-438b-91b1-cf4c73eed57f

Expected output

No errors

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions