Skip to content

Commit a98e61e

Browse files
committed
Handle refs, add pure enum test
1 parent f260636 commit a98e61e

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

Zend/tests/gh9775.phpt renamed to Zend/tests/gh9775_1.phpt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
--TEST--
2-
GH-9775: Enum in array_unique()
2+
GH-9775: Backed enum in array_unique()
33
--FILE--
44
<?php
55

@@ -15,6 +15,8 @@ enum Test: string
1515
case COUPONS_ADMIN = 'coupons.admin';
1616
}
1717

18+
$instructorsAdmin = Test::INSTRUCTORS_ADMIN;
19+
1820
$data = [
1921
Test::COURSES_ADMIN,
2022
Test::COURSES_REPORTING_ACCESS,
@@ -23,7 +25,7 @@ $data = [
2325
Test::B2B_DASHBOARD_ACCESS,
2426
Test::B2B_DASHBOARD_ACCESS,
2527
Test::INSTRUCTORS_ADMIN,
26-
Test::INSTRUCTORS_ADMIN,
28+
&$instructorsAdmin,
2729
Test::COUPONS_ADMIN,
2830
Test::AUTHENTICATED,
2931
];

Zend/tests/gh9775_2.phpt

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
--TEST--
2+
GH-9775: Pure enum in array_unique()
3+
--FILE--
4+
<?php
5+
6+
enum Test
7+
{
8+
case AUTHENTICATED;
9+
case COURSES_ADMIN;
10+
case BUNDLES_ADMIN;
11+
case COURSES_REPORTING_ACCESS;
12+
case B2B_DASHBOARD_ACCESS;
13+
case INSTRUCTORS_ADMIN;
14+
case USERS_ADMIN;
15+
case COUPONS_ADMIN;
16+
}
17+
18+
$instructorsAdmin = Test::INSTRUCTORS_ADMIN;
19+
20+
$data = [
21+
Test::COURSES_ADMIN,
22+
Test::COURSES_REPORTING_ACCESS,
23+
Test::BUNDLES_ADMIN,
24+
Test::USERS_ADMIN,
25+
Test::B2B_DASHBOARD_ACCESS,
26+
Test::B2B_DASHBOARD_ACCESS,
27+
Test::INSTRUCTORS_ADMIN,
28+
&$instructorsAdmin,
29+
Test::COUPONS_ADMIN,
30+
Test::AUTHENTICATED,
31+
];
32+
33+
$data = array_unique($data, flags: SORT_REGULAR);
34+
35+
var_dump($data);
36+
37+
?>
38+
--EXPECT--
39+
array(8) {
40+
[0]=>
41+
enum(Test::COURSES_ADMIN)
42+
[1]=>
43+
enum(Test::COURSES_REPORTING_ACCESS)
44+
[2]=>
45+
enum(Test::BUNDLES_ADMIN)
46+
[3]=>
47+
enum(Test::USERS_ADMIN)
48+
[4]=>
49+
enum(Test::B2B_DASHBOARD_ACCESS)
50+
[6]=>
51+
enum(Test::INSTRUCTORS_ADMIN)
52+
[8]=>
53+
enum(Test::COUPONS_ADMIN)
54+
[9]=>
55+
enum(Test::AUTHENTICATED)
56+
}

ext/standard/array.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,15 @@ static zend_always_inline int php_array_key_compare_string_locale_unstable_i(Buc
346346
static zend_always_inline int php_array_data_compare_unstable_i(Bucket *f, Bucket *s) /* {{{ */
347347
{
348348
int result = zend_compare(&f->val, &s->val);
349-
// Special handling for enums
349+
/* Special enums handling for array_unique. We don't want to add this logic to zend_compare as
350+
* that would be observable via comparison operators. */
350351
zval *rhs = &s->val;
352+
ZVAL_DEREF(rhs);
351353
if (UNEXPECTED(Z_TYPE_P(rhs) == IS_OBJECT)
352354
&& result == ZEND_UNCOMPARABLE
353355
&& (Z_OBJCE_P(rhs)->ce_flags & ZEND_ACC_ENUM)) {
354356
zval *lhs = &f->val;
357+
ZVAL_DEREF(lhs);
355358
if (Z_TYPE_P(lhs) == IS_OBJECT && (Z_OBJCE_P(lhs)->ce_flags & ZEND_ACC_ENUM)) {
356359
// Order doesn't matter, we just need to group the same enum values
357360
uintptr_t lhs_uintptr = (uintptr_t)Z_OBJ_P(lhs);

0 commit comments

Comments
 (0)