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

Variables, declared in the right part of ??= expression are considered always existing and not null #10151

Closed
ethernidee opened this issue Nov 17, 2023 · 2 comments

Comments

@ethernidee
Copy link

Bug report

It seems, that ??= is treated as "=" if new variables are initialized in the right part. Example:

$result = $this->cache[$keyName] ??= ($newIndex = count($this->cache) + 1);
// WRONG ERROR: Variable $newIndex in isset() always exists and is not nullable.
if (isset($newIndex)) {
	$this->recordNewCacheItem($keyName);
}

This code should be treated as

if (isset($this->cache[$keyName]) {
	$result = $this->cache[$keyName];
} else {
	$newIndex = count($this->cache) + 1;
	$this->cache[$keyName] = $newIndex;
	$result = $newIndex;
}

Code snippet that reproduces the problem

https://phpstan.org/r/44f585d5-f09f-42f7-abf8-bd612169930f

Expected output

Should be no error.

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

No response

@ondrejmirtes
Copy link
Member

Fixed: phpstan/phpstan-src@846f44e

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 Dec 20, 2023
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