-
-
Notifications
You must be signed in to change notification settings - Fork 947
Problem with chained assignments? #7142
Copy link
Copy link
Closed
phpstan/phpstan-src
#1473Labels
Milestone
Description
Bug report
Using docker version ghcr.io/phpstan/phpstan:latest, but verified with sandbox:
- with or without strict
- with or without bleeding edge
- Level 9
The middle item of a chained assignment appears to be uncertain, while the left item is known.
Not sure if there's some syntax here I should be using instead..
Code snippet that reproduces the problem
https://phpstan.org/r/e21ee227-0d57-484d-8aac-d028662bc06b
<?php declare(strict_types = 1);
/**
* @return array{id: int}|null
*/
function maybeNull(){
if ((rand(3)%2) != 0) {
return ['id' => 1];
}
return null;
}
/**
* @return void
*/
function foo(){
if (!is_null($a = $b = maybeNull())){
echo $a['id'];
echo $b['id']; // 20 "Offset 'id' does not exist on array{id: int}|null."
}
}Adding extra parens doesn't seem to affect it: https://phpstan.org/r/3bd843c3-6b01-49f1-a714-6829e82b4cab
if (!is_null($a = ($b = maybeNull()))){Adding a third chained assignment appears to indicate only the leftmost one is known: https://phpstan.org/r/00c89cf0-196a-483a-8fb7-42d6658c520e
if (!is_null($a = $b = $c = maybeNull())){
echo $a['id'];
echo $b['id']; // 20 "Offset 'id' does not exist on array{id: int}|null."
echo $c['id']; // 21 "Offset 'id' does not exist on array{id: int}|null."
}
Expected output
No errors
Actual output
20 | Offset 'id' does not exist on array{id: int}\|null.
Reactions are currently unavailable