Fix #11569: PHPStan does not handle ksort#5071
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Fix #11569: PHPStan does not handle ksort#5071phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- ksort/krsort now reorder ConstantArrayType entries by key value - After ksort, array_values() returns values in the correct sorted order - Correctly computes isList for sorted arrays (ksort on a list stays a list) - Updated existing bug-10627 test expectations to match correct behavior - New regression test in tests/PHPStan/Analyser/nsrt/bug-11569.php Closes phpstan/phpstan#11569
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
PHPStan did not reorder constant array keys after
ksort()/krsort()calls. This meant that subsequent operations likearray_values()returned values in the wrong order, causing false positives when the values were used witharray_combine().Changes
ksortandkrsortout of the general "sort without preserving list" handler insrc/Analyser/NodeScopeResolver.phpgetArrayKsortFunctionType()method that sortsConstantArrayTypeentries by their key values using PHP's natural comparison (<=>)isListfor sorted arrays: ksort on a list preserves list status (keys already sequential), krsort on a list correctly marks it as non-listtests/PHPStan/Analyser/nsrt/bug-10627.phpto match correct behaviorRoot cause
The
getArraySortDoNotPreserveListFunctionType()method handled all non-list-preserving sort functions identically, simply recreating the ConstantArrayType with the same key/value order. Forksort/krsort, the key order is deterministic and should be reflected in the type. Without reordering,array_values()afterksort()would produce values in the original insertion order rather than the key-sorted order, leading to incorrect types downstream.Test
Added
tests/PHPStan/Analyser/nsrt/bug-11569.phptesting:ksortonarray{name: string, age: int}producesarray{age: int, name: string}(alphabetical key order)krsortreverses key orderarray_values()afterksortreturns values in the correct sorted orderarray_combine()with sorted values produces the correct result typeFixes phpstan/phpstan#11569