-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Support enums in array_unique #11015
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
--TEST-- | ||
GH-9775: Enum in array_unique() | ||
--FILE-- | ||
<?php | ||
|
||
enum Test: string | ||
{ | ||
case AUTHENTICATED = 'authenticated'; | ||
case COURSES_ADMIN = 'courses.admin'; | ||
case BUNDLES_ADMIN = 'bundles.admin'; | ||
case COURSES_REPORTING_ACCESS = 'courses-reporting.access'; | ||
case B2B_DASHBOARD_ACCESS = 'b2b-dashboard.access'; | ||
case INSTRUCTORS_ADMIN = 'instructors.admin'; | ||
case USERS_ADMIN = 'users.admin'; | ||
case COUPONS_ADMIN = 'coupons.admin'; | ||
} | ||
|
||
$data = [ | ||
Test::COURSES_ADMIN, | ||
Test::COURSES_REPORTING_ACCESS, | ||
Test::BUNDLES_ADMIN, | ||
Test::USERS_ADMIN, | ||
Test::B2B_DASHBOARD_ACCESS, | ||
Test::B2B_DASHBOARD_ACCESS, | ||
Test::INSTRUCTORS_ADMIN, | ||
Test::INSTRUCTORS_ADMIN, | ||
Test::COUPONS_ADMIN, | ||
Test::AUTHENTICATED, | ||
]; | ||
|
||
$data = array_unique($data, flags: SORT_REGULAR); | ||
|
||
var_dump($data); | ||
|
||
?> | ||
--EXPECT-- | ||
array(8) { | ||
[0]=> | ||
enum(Test::COURSES_ADMIN) | ||
[1]=> | ||
enum(Test::COURSES_REPORTING_ACCESS) | ||
[2]=> | ||
enum(Test::BUNDLES_ADMIN) | ||
[3]=> | ||
enum(Test::USERS_ADMIN) | ||
[4]=> | ||
enum(Test::B2B_DASHBOARD_ACCESS) | ||
[6]=> | ||
enum(Test::INSTRUCTORS_ADMIN) | ||
[8]=> | ||
enum(Test::COUPONS_ADMIN) | ||
[9]=> | ||
enum(Test::AUTHENTICATED) | ||
} |
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 |
---|---|---|
|
@@ -345,7 +345,24 @@ static zend_always_inline int php_array_key_compare_string_locale_unstable_i(Buc | |
|
||
static zend_always_inline int php_array_data_compare_unstable_i(Bucket *f, Bucket *s) /* {{{ */ | ||
{ | ||
return zend_compare(&f->val, &s->val); | ||
int result = zend_compare(&f->val, &s->val); | ||
// Special handling for enums | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Before merging, add a comment that this is added for array_unique. Otherwise this may seem cryptic. |
||
zval *rhs = &s->val; | ||
if (UNEXPECTED(Z_TYPE_P(rhs) == IS_OBJECT) | ||
&& result == ZEND_UNCOMPARABLE | ||
&& (Z_OBJCE_P(rhs)->ce_flags & ZEND_ACC_ENUM)) { | ||
zval *lhs = &f->val; | ||
if (Z_TYPE_P(lhs) == IS_OBJECT && (Z_OBJCE_P(lhs)->ce_flags & ZEND_ACC_ENUM)) { | ||
// Order doesn't matter, we just need to group the same enum values | ||
uintptr_t lhs_uintptr = (uintptr_t)Z_OBJ_P(lhs); | ||
uintptr_t rhs_uintptr = (uintptr_t)Z_OBJ_P(rhs); | ||
return lhs_uintptr == rhs_uintptr ? 0 : (lhs_uintptr < rhs_uintptr ? -1 : 1); | ||
} else { | ||
// Shift enums to the end of the array | ||
return -1; | ||
} | ||
} | ||
return result; | ||
} | ||
/* }}} */ | ||
|
||
|
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.