Skip to content

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
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions Zend/tests/gh9775.phpt
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)
}
19 changes: 18 additions & 1 deletion ext/standard/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member Author

Choose a reason for hiding this comment

The 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;
}
/* }}} */

Expand Down