- 
                Notifications
    You must be signed in to change notification settings 
- Fork 545
fix null coalesce false positive for multi-dimensional array in loop #4475
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php declare(strict_types = 1); | ||
|  | ||
| namespace OptionalArrayKeyInMultiDimArrayLoop; | ||
|  | ||
| /** | ||
| * @param array<int, array{1: int, 2: int, 3: float}> $rows | ||
| */ | ||
| function foo(array $rows): mixed | ||
| { | ||
| $itemMap = []; | ||
|  | ||
| foreach ($rows as $row) { | ||
| $x = $row[1]; | ||
| $month = $row[2]; | ||
|  | ||
| $itemMap[$x][$month]['foo'] ??= 5; | ||
| $itemMap[$x][$month]['bar'] ??= 5; | ||
|  | ||
| \PHPStan\Testing\assertType('array{foo: 5, bar: 5, amount?: 0.0}', $itemMap[$x][$month]); | ||
| $itemMap[$x][$month]['amount'] ??= 0.0; | ||
| } | ||
|  | ||
| return $itemMap; | ||
| } | 
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php declare(strict_types = 1); | ||
|  | ||
| namespace BugNullCoalesceMultiDimLoop; | ||
|  | ||
| /** | ||
| * @param array<int, array{1: int, 2: int, 3: float}> $rows | ||
| */ | ||
| function foo(array $rows): mixed | ||
| { | ||
| $itemMap = []; | ||
|  | ||
| foreach ($rows as $row) { | ||
| $x = $row[1]; | ||
| $month = $row[2]; | ||
|  | ||
| $itemMap[$x][$month]['foo'] ??= 5; | ||
| $itemMap[$x][$month]['bar'] ??= 5; | ||
|  | ||
| $itemMap[$x][$month]['amount'] ??= 0.0; | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The same test in tests/PHPStan/Analyser/nsrt/ with some  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added and it does fail in 2.1.x: https://phpstan.org/r/b97ea05c-33dc-4287-8223-cd5a6369730d | ||
| } | ||
|  | ||
| return $itemMap; | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm ignoring this change. This test was added as a performance regression test (I'm not sure if/how that works, or why it makes assertions about the errors in the first place) and it's extremely hard to reason about the code. So I'm assuming that the reported errors don't matter.
Fundamentally, the array handling in PHPStan has a set of bugs and as a result of this PR it will have a slightly different (hopefully smaller) set of bugs. But there can definitely be cases where the new code causes a regression.
If you want to see the difference detected in the test for yourself here are the errors:
Previous:
New: