Skip to content

Commit 90a845c

Browse files
committed
Merge branch 'PHP-8.1'
* PHP-8.1: Fix ReflectionProperty::__toString() of properties containing enums
2 parents 81d4d5d + 1944c14 commit 90a845c

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

ext/reflection/php_reflection.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,13 @@ static int format_default_value(smart_str *str, zval *value) {
638638
format_default_value(str, zv);
639639
} ZEND_HASH_FOREACH_END();
640640
smart_str_appendc(str, ']');
641+
} else if (Z_TYPE_P(value) == IS_OBJECT) {
642+
zend_object *obj = Z_OBJ_P(value);
643+
zend_class_entry *class = obj->ce;
644+
ZEND_ASSERT(class->ce_flags & ZEND_ACC_ENUM);
645+
smart_str_append(str, class->name);
646+
smart_str_appends(str, "::");
647+
smart_str_append(str, Z_STR_P(zend_enum_fetch_case_name(obj)));
641648
} else {
642649
ZEND_ASSERT(Z_TYPE_P(value) == IS_CONSTANT_AST);
643650
zend_string *ast_str = zend_ast_export("", Z_ASTVAL_P(value), "");

ext/reflection/tests/gh8444.phpt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
GH-8444 (Fix ReflectionProperty::__toString() of properties containing instantiated enums)
3+
--FILE--
4+
<?php
5+
6+
enum Foo
7+
{
8+
case Bar;
9+
}
10+
11+
class Bar
12+
{
13+
public Foo $enum = Foo::Bar;
14+
public $enumInArray = [Foo::Bar];
15+
}
16+
17+
echo new \ReflectionProperty('Bar', 'enum'), "\n";
18+
echo new \ReflectionProperty('Bar', 'enumInArray'), "\n";
19+
20+
echo new \ReflectionProperty(new Bar, 'enum'), "\n";
21+
echo new \ReflectionProperty(new Bar, 'enumInArray'), "\n";
22+
23+
?>
24+
--EXPECT--
25+
Property [ public Foo $enum = Foo::Bar ]
26+
27+
Property [ public $enumInArray = [Foo::Bar] ]
28+
29+
Property [ public Foo $enum = Foo::Bar ]
30+
31+
Property [ public $enumInArray = [Foo::Bar] ]

0 commit comments

Comments
 (0)