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

False positive: Offset ... on array... on left side of ?? always exists and is not nullable. #5933

Closed
schlndh opened this issue Nov 4, 2021 · 8 comments
Labels
Milestone

Comments

@schlndh
Copy link

schlndh commented Nov 4, 2021

Bug report

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).

Code snippet that reproduces the problem

https://phpstan.org/r/1420808b-7d48-41c2-936e-8e17b0bdae94

Expected output

It shouldn't complain about ?? null.

@chaos0815
Copy link

Not sure if same usecase, but same error:
see https://phpstan.org/r/be6a2d6e-bf05-41be-a803-cc92d9e34c05

@ondrejmirtes
Copy link
Member

@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
Copy link
Author

schlndh commented 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'] ?? 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. 😅

@ondrejmirtes ondrejmirtes reopened this Feb 1, 2022
@ondrejmirtes
Copy link
Member

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 :)

@ondrejmirtes ondrejmirtes added this to the Easy fixes milestone Feb 1, 2022
@phpstan-bot
Copy link
Contributor

@schlndh After the latest commit in dev-master, PHPStan now reports different result with your code snippet:

@@ @@
-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.

@phpstan-bot
Copy link
Contributor

@schlndh After the latest commit in 1.6.x, PHPStan now reports different result with your code snippet:

@@ @@
-18: Offset 'a' on array{a: int<min, 99>, b: int<min, 99>} on left side of ?? always exists and is not nullable.
+No errors

@phpstan-bot
Copy link
Contributor

@ondrejmirtes After the latest commit in 1.6.x, PHPStan now reports different result with your code snippet:

@@ @@
-17: Dumped type: non-empty-array<int<0, 99>, array{a: int<0, 99>, b: int<0, 99>}>
-20: Offset 'a' on array{a: int<0, 99>, b: int<0, 99>} on left side of ?? always exists and is not nullable.
+17: Dumped type: non-empty-array<int<0, 99>, array{a?: int<0, 99>, b?: int<0, 99>}&non-empty-array>
Full report
Line Error
17 Dumped type: non-empty-array<int<0, 99>, array{a?: int<0, 99>, b?: int<0, 99>}&non-empty-array>

@github-actions
Copy link

github-actions bot commented 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.

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

No branches or pull requests

4 participants