Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reporting call on X|null using php8 null safe operator #4527

Closed
michalhosna opened this issue Feb 11, 2021 · 2 comments
Closed

Reporting call on X|null using php8 null safe operator #4527

michalhosna opened this issue Feb 11, 2021 · 2 comments

Comments

@michalhosna
Copy link

Bug report

In php8, the new null safe operator ?-> was added. When using it in combination with ?? for calling method/accesing properties on values of array like this:

($array['key'] ?? null)?->bar

phpstan incorrectly reports

Cannot access property $bar on T|null.

even though we are using the null safe operator, and the code behaves as expected (return nulls, when the key in the array does not exist).

Code snippet that reproduces the problem

https://phpstan.org/r/d831442b-e70b-48eb-b8ac-d68332590815

<?php declare(strict_types = 1);

class Foo
{
	public function foo(): void
	{
		/** @var Bar[] */
		$array = [];
		
		$value = ($array['randomKey'] ?? null)?->bar;
	}
}

class Bar {
	public mixed $bar;
}

Expected output

To not report any errors

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@990ba51

@github-actions
Copy link

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 30, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants