You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
False positive: Offset ... on array... on left side of ?? always exists and is not nullable. #5933
Phpstan gets confused when an associative array is created in a loop and subsequently the array is foreach-ed over. It thinks that all the offsets that may have been assigned are assigned and it complains about ?? null. It's one of the new errors that I saw after upgrading to 1.0, so it's probably a regression (unless the message changed and wasn't caught by the baseline anymore).
@ondrejmirtes Thanks, but I don't understand your explanation. Clearly, it can happen that $arr[$i]['a'] is not set even if $arr[$i] is set (which I now realize is the difference between the first and second ?? null). Therefore, it is valid to write $val['a'] ?? null to acknowledge that fact (and mute the undefined index warning from PHP). So PHPStan shouldn't complain about it.
Of course, I understand that an array structure can be arbitrarily complex and PHPStan can never understand everything. In this case, I'd say that non-empty-array<int<0, 99>, array{a?: int<0, 99>, b?: int<0, 99>}> or non-empty-array<int<0, 99>, array{a: int<0, 99>}|array{b: int<0, 99>}> would be better (I assume that either of them would prevent PHPStan from complaining about the ?? null, but I haven't checked). But I'm not sure whether it can be inferred from the code in a reasonable way.
Ultimately, it's probably best not to write code like this in the first place and use proper types instead. 😅
@@ @@-18: Offset 'a' on array{a: int<min, 99>, b: int<min, 99>} on left side of ?? always exists and is not nullable.+18: Offset 'a' on array{a: int<0, 99>, b: int<0, 99>} on left side of ?? always exists and is not nullable.
Full report
Line
Error
18
Offset 'a' on array{a: int<0, 99>, b: int<0, 99>} on left side of ?? always exists and is not nullable.
Activity
chaos0815 commentedon Nov 8, 2021
Not sure if same usecase, but same error:
see https://phpstan.org/r/be6a2d6e-bf05-41be-a803-cc92d9e34c05
ondrejmirtes commentedon Jan 31, 2022
@schlndh PHPStan is smart and I think that it knows what's going on in your example: https://phpstan.org/r/1b680ac5-e330-47e7-bc0e-132f83f31588 - the
'a'index always exists.@chaos0815 unrelated, please open a separate issue.
schlndh commentedon Feb 1, 2022
@ondrejmirtes Thanks, but I don't understand your explanation. Clearly, it can happen that
$arr[$i]['a']is not set even if$arr[$i]is set (which I now realize is the difference between the first and second?? null). Therefore, it is valid to write$val['a'] ?? nullto acknowledge that fact (and mute the undefined index warning from PHP). So PHPStan shouldn't complain about it.Of course, I understand that an array structure can be arbitrarily complex and PHPStan can never understand everything. In this case, I'd say that
non-empty-array<int<0, 99>, array{a?: int<0, 99>, b?: int<0, 99>}>ornon-empty-array<int<0, 99>, array{a: int<0, 99>}|array{b: int<0, 99>}>would be better (I assume that either of them would prevent PHPStan from complaining about the?? null, but I haven't checked). But I'm not sure whether it can be inferred from the code in a reasonable way.Ultimately, it's probably best not to write code like this in the first place and use proper types instead. 😅
ondrejmirtes commentedon Feb 1, 2022
I'm sorry, I misread the example, I thought that in both if/else
'a'is set and not'b'. So yeah, this is a bug :)phpstan-bot commentedon Feb 1, 2022
@schlndh After the latest commit in dev-master, PHPStan now reports different result with your code snippet:
Full report
Offset 'a' on array{a: int<0, 99>, b: int<0, 99>} on left side of ?? always exists and is not nullable.phpstan-bot commentedon Apr 2, 2022
@schlndh After the latest commit in 1.6.x, PHPStan now reports different result with your code snippet:
phpstan-bot commentedon Apr 2, 2022
@ondrejmirtes After the latest commit in 1.6.x, PHPStan now reports different result with your code snippet:
Full report
Dumped type: non-empty-array<int<0, 99>, array{a?: int<0, 99>, b?: int<0, 99>}&non-empty-array>github-actions commentedon May 4, 2022
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.