-
Notifications
You must be signed in to change notification settings - Fork 545
Don't loose known offset-types in array_merge() #4554
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
staabm
wants to merge
12
commits into
phpstan:2.1.x
Choose a base branch
from
staabm:merge
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
12 commits
Select commit
Hold shift + click to select a range
71d80ae
Don't loose known offset-values in array_merge()
staabm a0ef976
regression test
staabm 9b16f74
more tests
staabm 89cb925
Update HasOffsetType.php
staabm 93d7744
support HasOffset* types
staabm 00aa84a
Update ArrayMergeFunctionDynamicReturnTypeExtension.php
staabm 0ba56e7
infer HasOffsetValueType for last occuring string-keys
staabm 0e4a696
refactor
staabm 152453c
more tests
staabm 8896025
test unions
staabm 87c3cb2
refactor
staabm 059cc60
cs
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
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
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
123 changes: 123 additions & 0 deletions
123
tests/PHPStan/Analyser/nsrt/array-merge-const-non-const.php
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,123 @@ | ||
| <?php | ||
|
|
||
| namespace ArrayMergeConstNonConst; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| function doFoo(array $post): void { | ||
| assertType( | ||
| "non-empty-array&hasOffset('a')&hasOffset('b')", | ||
| array_merge(['a' => 1, 'b' => false, 10 => 99], $post) | ||
| ); | ||
| } | ||
|
|
||
| function doBar(array $array): void { | ||
| assertType( | ||
| "non-empty-array&hasOffsetValue('a', 1)&hasOffsetValue('b', false)", | ||
| array_merge($array, ['a' => 1, 'b' => false, 10 => 99]) | ||
| ); | ||
| } | ||
|
|
||
| function doFooBar(array $array): void { | ||
| assertType( | ||
| "non-empty-array&hasOffset('x')&hasOffsetValue('a', 1)&hasOffsetValue('b', false)&hasOffsetValue('c', 'e')", | ||
| array_merge(['c' => 'd', 'x' => 'y'], $array, ['a' => 1, 'b' => false, 'c' => 'e']) | ||
| ); | ||
| } | ||
|
|
||
| function doFooInts(array $array): void { | ||
| assertType( | ||
| "non-empty-array&hasOffsetValue('a', 1)&hasOffsetValue('c', 'e')", | ||
| array_merge([1 => 'd'], $array, ['a' => 1, 3 => false, 'c' => 'e']) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @param array<string> $array | ||
| */ | ||
| function floatKey(array $array): void { | ||
| assertType( | ||
| "non-empty-array<string>&hasOffsetValue('a', '1')&hasOffsetValue('c', 'e')", | ||
| array_merge([4.23 => 'd'], $array, ['a' => '1', 3 => 'false', 'c' => 'e']) | ||
| ); | ||
| } | ||
|
|
||
| function doOptKeys(array $array, array $arr2): void { | ||
| if (rand(0, 1)) { | ||
| $array['abc'] = 'def'; | ||
| } | ||
| assertType("array", array_merge($arr2, $array)); | ||
| assertType("array", array_merge($array, $arr2)); | ||
| } | ||
|
|
||
| /** | ||
| * @param array{a?: 1, b: 2} $array | ||
| */ | ||
| function doOptShapeKeys(array $array, array $arr2): void { | ||
| assertType("non-empty-array&hasOffsetValue('b', 2)", array_merge($arr2, $array)); | ||
| assertType("non-empty-array&hasOffset('b')", array_merge($array, $arr2)); | ||
| } | ||
|
|
||
| function hasOffsetKeys(array $array, array $arr2): void { | ||
| if (array_key_exists('b', $array)) { | ||
| assertType("non-empty-array&hasOffsetValue('b', mixed)", array_merge($arr2, $array)); | ||
| assertType("non-empty-array&hasOffset('b')", array_merge($array, $arr2)); | ||
| } | ||
| } | ||
|
|
||
| function hasOffsetValueKeys(array $hasB, array $mixedArray, array $hasC): void { | ||
| $hasB['b'] = 123; | ||
| $hasC['c'] = 'def'; | ||
|
|
||
| assertType("non-empty-array&hasOffsetValue('b', 123)", array_merge($mixedArray, $hasB)); | ||
| assertType("non-empty-array&hasOffset('b')", array_merge($hasB, $mixedArray)); | ||
|
|
||
| assertType( | ||
| "non-empty-array&hasOffset('b')&hasOffsetValue('c', 'def')", | ||
| array_merge($mixedArray, $hasB, $hasC) | ||
| ); | ||
| assertType( | ||
| "non-empty-array&hasOffset('b')&hasOffsetValue('c', 'def')", | ||
| array_merge($hasB, $mixedArray, $hasC) | ||
| ); | ||
|
|
||
| assertType( | ||
| "non-empty-array&hasOffset('c')&hasOffsetValue('b', 123)", | ||
| array_merge($hasC, $mixedArray, $hasB) | ||
| ); | ||
| assertType( | ||
| "non-empty-array&hasOffset('b')&hasOffset('c')", | ||
| array_merge($hasC, $hasB, $mixedArray) | ||
| ); | ||
|
|
||
| if (rand(0, 1)) { | ||
| $hasBorC = ['b' => 1]; | ||
| } else { | ||
| $hasBorC = ['c' => 2]; | ||
| } | ||
| assertType('array{b: 1}|array{c: 2}', $hasBorC); | ||
| assertType("non-empty-array", array_merge($mixedArray, $hasBorC)); | ||
| assertType("non-empty-array", array_merge($hasBorC, $mixedArray)); | ||
|
|
||
| if (rand(0, 1)) { | ||
| $differentCs = ['c' => 10]; | ||
| } else { | ||
| $differentCs = ['c' => 20]; | ||
| } | ||
| assertType('array{c: 10}|array{c: 20}', $differentCs); | ||
| assertType("non-empty-array&hasOffsetValue('c', 10|20)", array_merge($mixedArray, $differentCs)); | ||
| assertType("non-empty-array&hasOffset('c')", array_merge($differentCs, $mixedArray)); | ||
|
|
||
| assertType("non-empty-array&hasOffsetValue('c', 10|20)", array_merge($mixedArray, $hasBorC, $differentCs)); | ||
| assertType("non-empty-array", array_merge($differentCs, $mixedArray, $hasBorC)); // could be non-empty-array&hasOffset('c') | ||
| assertType("non-empty-array&hasOffsetValue('c', 10|20)", array_merge($hasBorC, $mixedArray, $differentCs)); | ||
| assertType("non-empty-array", array_merge($differentCs, $hasBorC, $mixedArray)); // could be non-empty-array&hasOffset('c') | ||
| } | ||
|
|
||
| /** | ||
| * @param array{a?: 1, b?: 2} $allOptional | ||
| */ | ||
| function doAllOptional(array $allOptional, array $arr2): void { | ||
| assertType("array", array_merge($arr2, $allOptional)); | ||
| assertType("array", array_merge($allOptional, $arr2)); | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php | ||
|
|
||
| namespace Bug8438; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| /** | ||
| * @param array<string, string> $array | ||
| * | ||
| * @return array{expr: mixed, ...} | ||
| */ | ||
| protected function foo(array $array): array | ||
| { | ||
| $rnd = mt_rand(); | ||
| if ($rnd === 0) { | ||
| return ['expr' => 'test']; | ||
| } elseif ($rnd === 1) { | ||
| // no error with checkBenevolentUnionTypes: false (default even with l9 + strict rules) | ||
| return ['expr' => 'test', 1 => 'ok']; | ||
| } else { | ||
| // phpstan must understand 'expr' key is always present in the result, | ||
| // then there will be no error here neither | ||
| return array_merge($array, ['expr' => 'test', 1 => 'ok']); | ||
| } | ||
| } | ||
| } |
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.
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.
analog HasOffsetValueType->getOffsetType()