Skip to content

nullable lost outside guarding if-statement #9105

@puggan

Description

@puggan

Bug report

After using ?-> (nullsafe property) in an if-statement, phpstan changes the type to be not-null.
I can see why it's not null inside the if-block, but under the if-block it can still be null

Code snippet that reproduces the problem

https://phpstan.org/r/c2dc0b31-e6c0-45bb-860f-a28cd5ea1844

https://3v4l.org/d3VdN

class H
{
	public int|null $a = null;
	public self|null $b = null;

	public function h(): void
	{
		\PHPStan\dumpType($this->b);
		if ($this->b?->a < 5) {
			echo '<5', PHP_EOL;
		}
		\PHPStan\dumpType($this->b);
		if ($this->b?->a > 0) {
			echo '>0', PHP_EOL;
		}
		\PHPStan\dumpType($this->b);
	}
}

Expected output

10 | Dumped type: H|null
14 | Dumped type: H|null
18 | Dumped type: H|null

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

Every day

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions