-
-
Notifications
You must be signed in to change notification settings - Fork 938
Closed
Labels
Milestone
Description
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;
}Reactions are currently unavailable