-
Notifications
You must be signed in to change notification settings - Fork 555
Fix phpstan/phpstan#8270: False positive on array item modification #5189
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
Open
phpstan-bot
wants to merge
21
commits into
phpstan:2.1.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-w8d4otm
base: 2.1.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
f015cdb
Fix false positive on array item modification with non-constant key
github-actions[bot] c808bac
Add assertion
VincentLanglet 45ba75c
Add non-regression test for phpstan/phpstan#13857
phpstan-bot ec18c1c
Add non-regression test for phpstan/phpstan#13623
phpstan-bot 4f3bfc2
Add IfConstantConditionRule test for phpstan/phpstan#8270
phpstan-bot de6f6df
Add NullCoalesceRule test for phpstan/phpstan#13623
phpstan-bot b0865e9
Deduplicate bug-8270 and bug-13623 test files
phpstan-bot e281d8c
improve naming
staabm 9e969b8
Update AssignHandler.php
staabm 5460901
Update bug-13857.php
staabm f6520d7
Add test for optional offset in shouldUnionExistingItemType
phpstan-bot 9f6d382
Revert "Add test for optional offset in shouldUnionExistingItemType"
staabm 9874e91
Update bug-8270.php
staabm f6c6d4d
Update bug-8270.php
staabm 4976107
more tests
staabm 91c7a78
Update bug-13857.php
staabm 980a943
Update bug-13857.php
staabm 03b1809
Fix shouldUnionExistingItemType to use bidirectional supertype check
phpstan-bot b7e4706
Update AssignHandler.php
staabm d015a48
Update bug-13857.php
staabm 7647ecf
Update bug-13857.php
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug13623; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function (array $results): void { | ||
| $customers = []; | ||
|
|
||
| foreach ($results as $row) { | ||
| $customers[$row['customer_id']] ??= []; | ||
| $customers[$row['customer_id']]['orders'] ??= []; | ||
| $customers[$row['customer_id']]['orders'][$row['order_id']] ??= []; | ||
|
|
||
| $customers[$row['customer_id']]['orders'][$row['order_id']]['balance_forward'] ??= 0; | ||
| $customers[$row['customer_id']]['orders'][$row['order_id']]['new_invoice'] ??= 0; | ||
| $customers[$row['customer_id']]['orders'][$row['order_id']]['payments'] ??= 0; | ||
| $customers[$row['customer_id']]['orders'][$row['order_id']]['balance'] ??= $row['order_total']; | ||
| } | ||
|
|
||
| assertType("array<array{orders: non-empty-array<array{balance_forward: 0, new_invoice: 0, payments: 0, balance: mixed}>}>", $customers); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug13857; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| /** | ||
| * @param array<int, array{state: string}> $array | ||
| */ | ||
| function test(array $array, int $id): void { | ||
| $array[$id]['state'] = 'foo'; | ||
| // only one element was set to 'foo', not all of them. | ||
| assertType("non-empty-array<int, array{state: string}>", $array); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, array{state?: string}> $array | ||
| */ | ||
| function testMaybe(array $array, int $id): void { | ||
| $array[$id]['state'] = 'foo'; | ||
| // only one element was set to 'foo', not all of them. | ||
| assertType("non-empty-array<int, array{state?: string}>", $array); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, array{state: string|bool}> $array | ||
| */ | ||
| function testUnionValue(array $array, int $id): void { | ||
| $array[$id]['state'] = 'foo'; | ||
| // only one element was set to 'foo', not all of them. | ||
| assertType("non-empty-array<int, array{state: bool|string}>", $array); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, array{state: string}|array{foo: int}> $array | ||
| */ | ||
| function testUnionArray(array $array, int $id): void { | ||
| $array[$id]['state'] = 'foo'; | ||
| // only one element was set to 'foo', not all of them. | ||
| assertType("non-empty-array<int, non-empty-array{foo?: int, state?: string}>", $array); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, array{state: string}|array{foo: int}> $array | ||
| */ | ||
| function testUnionArrayDifferentType(array $array, int $id): void { | ||
| $array[$id]['state'] = true; | ||
| assertType("non-empty-array<int, array{state: string}|non-empty-array{foo?: int, state?: true}>", $array); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, array{state: 'foo'}> $array | ||
| */ | ||
| function testConstantArray(array $array, int $id): void { | ||
| $array[$id]['state'] = 'bar'; | ||
| assertType("non-empty-array<int, array{state: 'bar'}|array{state: 'foo'}>", $array); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<int, array{state: 'foo'}> $array | ||
| */ | ||
| function testConstantArrayNonScalarAssign(array $array, int $id, bool $b): void { | ||
| $array[$id]['state'] = $b; | ||
| assertType("non-empty-array<int, array{state: 'foo'}|array{state: bool}>", $array); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types = 1); | ||
staabm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| namespace Bug8270; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function doFoo() { | ||
| /** @var non-empty-list<array{test: false, value: int}> $list */ | ||
| $list = []; | ||
| $list[0]['test'] = true; | ||
|
|
||
| foreach ($list as $item) { | ||
| assertType('array{test: bool, value: int}', $item); | ||
| if ($item['test']) { | ||
| assertType('true', $item['test']); | ||
| echo $item['value']; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function doBar() { | ||
| $list = []; | ||
|
|
||
| for ($i = 0; $i < 10; $i++) { | ||
| $list[] = [ | ||
| 'test' => false, | ||
| 'value' => rand(), | ||
| ]; | ||
| } | ||
|
|
||
| if ($list === []) { | ||
| return; | ||
| } | ||
|
|
||
| $k = array_key_first($list); | ||
| assertType('int<0, max>', $k); | ||
| $list[$k]['test'] = true; | ||
|
|
||
| foreach ($list as $item) { | ||
| assertType('array{test: bool, value: int<0, max>}', $item); | ||
| if ($item['test']) { | ||
| echo $item['value']; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.