-
Notifications
You must be signed in to change notification settings - Fork 8k
Fix SORT_REGULAR transitivity for mixed types #20315
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
jmarble
wants to merge
7
commits into
php:master
Choose a base branch
from
jmarble:fix-sort-regular-transitivity-tls
base: master
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.
+397
−2
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
438b127
Fix SORT_REGULAR transitivity for mixed types
jmarble 3347505
Merge branch 'php:master' into fix-sort-regular-transitivity-tls
jmarble 3a300dc
Fix empty string ordering in transitive comparison
jmarble 7f8261d
Initialize transitive_compare_mode to fix Windows ZTS
jmarble 768ec31
Refactor for readability
jmarble f1f8d6b
Add tests for GH-20262
jmarble e3b9ab9
Fix test for x32 platform compatibility
jmarble 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
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,88 @@ | ||
| --TEST-- | ||
| SORT_REGULAR uses transitive comparison for consistent sorting | ||
| --FILE-- | ||
| <?php | ||
| echo "Test 1: Scalar mixed numeric/non-numeric strings\n"; | ||
| $arr1 = ["5", "10", "3A"]; | ||
| sort($arr1, SORT_REGULAR); | ||
| var_dump($arr1); | ||
|
|
||
| echo "\nTest 2: Same values, different order (should match)\n"; | ||
| $arr2 = ["3A", "10", "5"]; | ||
| sort($arr2, SORT_REGULAR); | ||
| var_dump($arr2); | ||
|
|
||
| echo "\nTest 3: Nested arrays with mixed strings\n"; | ||
| $arr3 = [ | ||
| ['streetNumber' => '5'], | ||
| ['streetNumber' => '10'], | ||
| ['streetNumber' => '3A'], | ||
| ]; | ||
| sort($arr3, SORT_REGULAR); | ||
| foreach ($arr3 as $item) { | ||
| echo $item['streetNumber']; | ||
| if ($item !== end($arr3)) echo " "; | ||
| } | ||
| echo "\n"; | ||
|
|
||
| echo "\nTest 4: Same nested arrays, reversed (should match)\n"; | ||
| $arr4 = [ | ||
| ['streetNumber' => '3A'], | ||
| ['streetNumber' => '10'], | ||
| ['streetNumber' => '5'], | ||
| ]; | ||
| sort($arr4, SORT_REGULAR); | ||
| foreach ($arr4 as $item) { | ||
| echo $item['streetNumber']; | ||
| if ($item !== end($arr4)) echo " "; | ||
| } | ||
| echo "\n"; | ||
|
|
||
| echo "\nTest 5: array_unique with nested arrays\n"; | ||
| $arr5 = [ | ||
| ['number' => '5'], | ||
| ['number' => '10'], | ||
| ['number' => '5'], | ||
| ['number' => '3A'], | ||
| ['number' => '5'], | ||
jmarble marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ]; | ||
| $unique = array_unique($arr5, SORT_REGULAR); | ||
| echo "Unique count: " . count($unique) . "\n"; | ||
|
|
||
| echo "\nTest 6: Comparison operators remain unchanged\n"; | ||
| echo '"5" <=> "3A": ' . ("5" <=> "3A") . "\n"; | ||
| echo '"10" <=> "3A": ' . ("10" <=> "3A") . "\n"; | ||
| ?> | ||
| --EXPECT-- | ||
| Test 1: Scalar mixed numeric/non-numeric strings | ||
| array(3) { | ||
| [0]=> | ||
| string(1) "5" | ||
| [1]=> | ||
| string(2) "10" | ||
| [2]=> | ||
| string(2) "3A" | ||
| } | ||
|
|
||
| Test 2: Same values, different order (should match) | ||
| array(3) { | ||
| [0]=> | ||
| string(1) "5" | ||
| [1]=> | ||
| string(2) "10" | ||
| [2]=> | ||
| string(2) "3A" | ||
| } | ||
|
|
||
| Test 3: Nested arrays with mixed strings | ||
| 5 10 3A | ||
|
|
||
| Test 4: Same nested arrays, reversed (should match) | ||
| 5 10 3A | ||
|
|
||
| Test 5: array_unique with nested arrays | ||
| Unique count: 3 | ||
|
|
||
| Test 6: Comparison operators remain unchanged | ||
| "5" <=> "3A": 1 | ||
| "10" <=> "3A": -1 | ||
81 changes: 81 additions & 0 deletions
81
ext/standard/tests/array/sort_regular_transitive_objects.phpt
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,81 @@ | ||
| --TEST-- | ||
| SORT_REGULAR with objects containing mixed numeric/non-numeric strings is transitive | ||
| --FILE-- | ||
| <?php | ||
| class Address { | ||
| public function __construct( | ||
| public string $streetNumber, | ||
| public string $streetName | ||
| ) {} | ||
| } | ||
|
|
||
| echo "Test 1: Objects with mixed numeric/non-numeric string properties\n"; | ||
| $addresses = [ | ||
| new Address('5', 'Main St'), | ||
| new Address('10', 'Main St'), | ||
| new Address('3A', 'Main St'), | ||
| ]; | ||
| sort($addresses, SORT_REGULAR); | ||
| echo "Result:"; | ||
| foreach ($addresses as $addr) { | ||
| echo " " . $addr->streetNumber; | ||
| } | ||
| echo "\n"; | ||
|
|
||
| echo "\nTest 2: Same objects, different order (should match Test 1)\n"; | ||
| $addresses2 = [ | ||
| new Address('3A', 'Main St'), | ||
| new Address('10', 'Main St'), | ||
| new Address('5', 'Main St'), | ||
| ]; | ||
| sort($addresses2, SORT_REGULAR); | ||
| echo "Result:"; | ||
| foreach ($addresses2 as $addr) { | ||
| echo " " . $addr->streetNumber; | ||
| } | ||
| echo "\n"; | ||
|
|
||
| echo "\nTest 3: Another permutation (should match Test 1)\n"; | ||
| $addresses3 = [ | ||
| new Address('10', 'Main St'), | ||
| new Address('5', 'Main St'), | ||
| new Address('3A', 'Main St'), | ||
| ]; | ||
| sort($addresses3, SORT_REGULAR); | ||
| echo "Result:"; | ||
| foreach ($addresses3 as $addr) { | ||
| echo " " . $addr->streetNumber; | ||
| } | ||
| echo "\n"; | ||
|
|
||
| echo "\nTest 4: array_unique() with objects\n"; | ||
| $addresses_with_dupes = [ | ||
| new Address('5', 'Main St'), | ||
| new Address('10', 'Main St'), | ||
| new Address('10', 'Main St'), | ||
| new Address('3A', 'Main St'), | ||
| new Address('5', 'Main St'), | ||
| ]; | ||
| $unique = array_unique($addresses_with_dupes, SORT_REGULAR); | ||
| echo "Input: 5 objects (with duplicates)\n"; | ||
| echo "Output: " . count($unique) . " unique objects\n"; | ||
| echo "Street numbers:"; | ||
| foreach ($unique as $addr) { | ||
| echo " " . $addr->streetNumber; | ||
| } | ||
| echo "\n"; | ||
| ?> | ||
| --EXPECT-- | ||
| Test 1: Objects with mixed numeric/non-numeric string properties | ||
| Result: 5 10 3A | ||
|
|
||
| Test 2: Same objects, different order (should match Test 1) | ||
| Result: 5 10 3A | ||
|
|
||
| Test 3: Another permutation (should match Test 1) | ||
| Result: 5 10 3A | ||
|
|
||
| Test 4: array_unique() with objects | ||
| Input: 5 objects (with duplicates) | ||
| Output: 3 unique objects | ||
| Street numbers: 5 10 3A |
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.