-
Notifications
You must be signed in to change notification settings - Fork 566
Fix phpstan/phpstan#10862: Invalid array key for constant array append when negative keys are used since PHP 8.3 #5412
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
16
commits into
phpstan:2.1.x
Choose a base branch
from
phpstan-bot:create-pull-request/patch-znh9m95
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.
+215
−1
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e8ee204
Fix array auto-index for negative keys on PHP 8.3+
VincentLanglet c54661b
Add PhpVersion::updatesAutoIncrementKeyForNegativeValues() method
phpstan-bot 47110f1
Split bug-10862 test into PHP 8.2 and PHP 8.3 variants
phpstan-bot 369c2a1
Extract shouldUpdateAutoIndex() private method for readability
phpstan-bot e168a11
Extract auto-index condition into private method and handle PHP 8.0+ …
phpstan-bot 9ac8db3
Add pre-PHP 8 and PHP 8.0+ array literal tests for bug-10862
phpstan-bot 8e30d38
Rename isLiteralArray to isArrayExpression for clarity
phpstan-bot 557536d
Rename isArrayExpression to initializedNonEmpty to express non-empty …
phpstan-bot 694ddc9
Replace setInitializedNonEmpty() with optional param in createEmpty()
phpstan-bot 40956df
Always set initializedNonEmpty=true in createEmpty()
phpstan-bot 2ad0306
Replace initializedNonEmpty with initializedEmpty set in createFromCo…
phpstan-bot 94e9657
Fix initializedEmpty not propagating through createFromConstantArray …
phpstan-bot 15a85d8
Add test for non-empty string-key array to guard initializedEmpty con…
phpstan-bot ee2c7a3
Gate initializedEmpty computation on PHP 8.0-8.2 version range
phpstan-bot 4a7fe14
Rework
VincentLanglet e8fe43a
Rework
VincentLanglet 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| <?php // lint < 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug10862Php74; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| // Pre-PHP 8.0: negative keys never affect auto-index | ||
|
|
||
| // Imperative assignment | ||
| function () { | ||
| $a = []; | ||
| $a[-4] = 1; | ||
| $a[] = 2; | ||
|
|
||
| assertType('array{-4: 1, 0: 2}', $a); | ||
| }; | ||
|
|
||
| // Array literal | ||
| function () { | ||
| $a = [-4 => 1]; | ||
| $a[] = 2; | ||
|
|
||
| assertType('array{-4: 1, 0: 2}', $a); | ||
| }; |
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,32 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug10862Php80; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| // PHP 8.0+: array literal with negative keys updates auto-index | ||
|
|
||
| function () { | ||
| $a = [-4 => 1]; | ||
| $a[] = 2; | ||
|
|
||
| assertType('array{-4: 1, -3: 2}', $a); | ||
| }; | ||
|
|
||
| function () { | ||
| $a = [-10 => 'a', -5 => 'b']; | ||
| $a[] = 'c'; | ||
|
|
||
| assertType("array{-10: 'a', -5: 'b', -4: 'c'}", $a); | ||
| }; | ||
|
|
||
| // Non-empty string-key array: negative key should update auto-index | ||
| function () { | ||
| $a = ['foo' => 'bar']; | ||
| $a[-5] = 'x'; | ||
| $a[] = 'y'; | ||
|
|
||
| assertType("array{foo: 'bar', -5: 'x', -4: 'y'}", $a); | ||
| }; |
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,43 @@ | ||
| <?php // lint <= 8.2 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug10862Php82; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| // PHP <= 8.2: imperative assignment with negative keys does not affect auto-index | ||
|
|
||
| function () { | ||
| $a = []; | ||
| $a[-4] = 1; | ||
| $a[] = 2; | ||
|
|
||
| assertType('array{-4: 1, 0: 2}', $a); | ||
| }; | ||
|
|
||
| function () { | ||
| $a = []; | ||
| $a[-1] = 'x'; | ||
| $a[] = 'y'; | ||
|
|
||
| assertType("array{-1: 'x', 0: 'y'}", $a); | ||
| }; | ||
|
|
||
| function () { | ||
| $a = []; | ||
| $a[-10] = 'a'; | ||
| $a[-5] = 'b'; | ||
| $a[] = 'c'; | ||
|
|
||
| assertType("array{-10: 'a', -5: 'b', 0: 'c'}", $a); | ||
| }; | ||
|
|
||
| function () { | ||
| $a = []; | ||
| $a[-3] = 'a'; | ||
| $a[5] = 'b'; | ||
| $a[] = 'c'; | ||
|
|
||
| assertType("array{-3: 'a', 5: 'b', 6: 'c'}", $a); | ||
| }; | ||
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,59 @@ | ||
| <?php // lint >= 8.3 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug10862Php83; | ||
|
|
||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| // PHP 8.3+: negative keys always affect auto-index (both imperative and literal) | ||
|
|
||
| // Imperative assignment | ||
| function () { | ||
| $a = []; | ||
| $a[-4] = 1; | ||
| $a[] = 2; | ||
|
|
||
| assertType('array{-4: 1, -3: 2}', $a); | ||
| }; | ||
|
|
||
| function () { | ||
| $a = []; | ||
| $a[-1] = 'x'; | ||
| $a[] = 'y'; | ||
|
|
||
| assertType("array{-1: 'x', 0: 'y'}", $a); | ||
| }; | ||
|
|
||
| function () { | ||
| $a = []; | ||
| $a[-10] = 'a'; | ||
| $a[-5] = 'b'; | ||
| $a[] = 'c'; | ||
|
|
||
| assertType("array{-10: 'a', -5: 'b', -4: 'c'}", $a); | ||
| }; | ||
|
|
||
| function () { | ||
| $a = []; | ||
| $a[-3] = 'a'; | ||
| $a[5] = 'b'; | ||
| $a[] = 'c'; | ||
|
|
||
| assertType("array{-3: 'a', 5: 'b', 6: 'c'}", $a); | ||
| }; | ||
|
|
||
| // Array literal | ||
| function () { | ||
| $a = [-4 => 1]; | ||
| $a[] = 2; | ||
|
|
||
| assertType('array{-4: 1, -3: 2}', $a); | ||
| }; | ||
|
|
||
| function () { | ||
| $a = [-10 => 'a', -5 => 'b']; | ||
| $a[] = 'c'; | ||
|
|
||
| assertType("array{-10: 'a', -5: 'b', -4: 'c'}", $a); | ||
| }; |
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.