Skip to content

Not respecting previous null check #4357

@allejo

Description

@allejo

Bug report

It looks like null checks that happen previous and cause an exit from a function are ignored. i.e. After the null check, the variable is still considered "nullable."

Code snippet that reproduces the problem

<?php declare(strict_types = 1);

class Sample {
	/** @var null|array<string, string> */
	private $arr = null;
	
	public function test(): void {
		if ($this->arr === null) {
			return;
		}
		
		unset($this->arr['hello']);
		
		if (count($this->arr) === 0) {
			$this->arr = null;
		}
	}
}

https://phpstan.org/r/a79d1e2f-932f-4c40-8e92-530baa0a3fb8

Expected output

This should work the same and not require an explicit null check like so,

if ($this->arr !== null && count($this->arr) === 0) {
    $this->arr = null;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions