Skip to content

Commit 083b0c3

Browse files
committed
Display property default value in reflection dumps
1 parent b5991d3 commit 083b0c3

10 files changed

+90
-79
lines changed

ext/reflection/php_reflection.c

Lines changed: 64 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,39 @@ static zend_op* _get_recv_op(zend_op_array *op_array, uint32_t offset)
584584
}
585585
/* }}} */
586586

587+
static int format_default_value(smart_str *str, zval *value, zend_class_entry *scope) {
588+
zval zv;
589+
ZVAL_COPY(&zv, value);
590+
if (UNEXPECTED(zval_update_constant_ex(&zv, scope) == FAILURE)) {
591+
zval_ptr_dtor(&zv);
592+
return FAILURE;
593+
}
594+
595+
if (Z_TYPE(zv) == IS_TRUE) {
596+
smart_str_appends(str, "true");
597+
} else if (Z_TYPE(zv) == IS_FALSE) {
598+
smart_str_appends(str, "false");
599+
} else if (Z_TYPE(zv) == IS_NULL) {
600+
smart_str_appends(str, "NULL");
601+
} else if (Z_TYPE(zv) == IS_STRING) {
602+
smart_str_appendc(str, '\'');
603+
smart_str_appendl(str, Z_STRVAL(zv), MIN(Z_STRLEN(zv), 15));
604+
if (Z_STRLEN(zv) > 15) {
605+
smart_str_appends(str, "...");
606+
}
607+
smart_str_appendc(str, '\'');
608+
} else if (Z_TYPE(zv) == IS_ARRAY) {
609+
smart_str_appends(str, "Array");
610+
} else {
611+
zend_string *tmp_zv_str;
612+
zend_string *zv_str = zval_get_tmp_string(&zv, &tmp_zv_str);
613+
smart_str_append(str, zv_str);
614+
zend_tmp_string_release(tmp_zv_str);
615+
}
616+
zval_ptr_dtor(&zv);
617+
return SUCCESS;
618+
}
619+
587620
/* {{{ _parameter_string */
588621
static void _parameter_string(smart_str *str, zend_function *fptr, struct _zend_arg_info *arg_info, uint32_t offset, zend_bool required, char* indent)
589622
{
@@ -617,36 +650,10 @@ static void _parameter_string(smart_str *str, zend_function *fptr, struct _zend_
617650
if (fptr->type == ZEND_USER_FUNCTION && !required) {
618651
zend_op *precv = _get_recv_op((zend_op_array*)fptr, offset);
619652
if (precv && precv->opcode == ZEND_RECV_INIT && precv->op2_type != IS_UNUSED) {
620-
zval zv;
621-
622653
smart_str_appends(str, " = ");
623-
ZVAL_COPY(&zv, RT_CONSTANT(precv, precv->op2));
624-
if (UNEXPECTED(zval_update_constant_ex(&zv, fptr->common.scope) == FAILURE)) {
625-
zval_ptr_dtor(&zv);
654+
if (format_default_value(str, RT_CONSTANT(precv, precv->op2), fptr->common.scope) == FAILURE) {
626655
return;
627656
}
628-
if (Z_TYPE(zv) == IS_TRUE) {
629-
smart_str_appends(str, "true");
630-
} else if (Z_TYPE(zv) == IS_FALSE) {
631-
smart_str_appends(str, "false");
632-
} else if (Z_TYPE(zv) == IS_NULL) {
633-
smart_str_appends(str, "NULL");
634-
} else if (Z_TYPE(zv) == IS_STRING) {
635-
smart_str_appendc(str, '\'');
636-
smart_str_appendl(str, Z_STRVAL(zv), MIN(Z_STRLEN(zv), 15));
637-
if (Z_STRLEN(zv) > 15) {
638-
smart_str_appends(str, "...");
639-
}
640-
smart_str_appendc(str, '\'');
641-
} else if (Z_TYPE(zv) == IS_ARRAY) {
642-
smart_str_appends(str, "Array");
643-
} else {
644-
zend_string *tmp_zv_str;
645-
zend_string *zv_str = zval_get_tmp_string(&zv, &tmp_zv_str);
646-
smart_str_append(str, zv_str);
647-
zend_tmp_string_release(tmp_zv_str);
648-
}
649-
zval_ptr_dtor(&zv);
650657
}
651658
}
652659
smart_str_appends(str, " ]");
@@ -818,6 +825,17 @@ static void _function_string(smart_str *str, zend_function *fptr, zend_class_ent
818825
}
819826
/* }}} */
820827

828+
static zval *property_get_default(zend_property_info *prop_info) {
829+
zend_class_entry *ce = prop_info->ce;
830+
if (prop_info->flags & ZEND_ACC_STATIC) {
831+
zval *prop = &ce->default_static_members_table[prop_info->offset];
832+
ZVAL_DEINDIRECT(prop);
833+
return prop;
834+
} else {
835+
return &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
836+
}
837+
}
838+
821839
/* {{{ _property_string */
822840
static void _property_string(smart_str *str, zend_property_info *prop, const char *prop_name, char* indent)
823841
{
@@ -855,6 +873,14 @@ static void _property_string(smart_str *str, zend_property_info *prop, const cha
855873
zend_unmangle_property_name(prop->name, &class_name, &prop_name);
856874
}
857875
smart_str_append_printf(str, "$%s", prop_name);
876+
877+
zval *default_value = property_get_default(prop);
878+
if (!Z_ISUNDEF_P(default_value)) {
879+
smart_str_appends(str, " = ");
880+
if (format_default_value(str, default_value, prop->ce) == FAILURE) {
881+
return;
882+
}
883+
}
858884
}
859885

860886
smart_str_appends(str, " ]\n");
@@ -3673,7 +3699,7 @@ ZEND_METHOD(reflection_class, __construct)
36733699
/* }}} */
36743700

36753701
/* {{{ add_class_vars */
3676-
static void add_class_vars(zend_class_entry *ce, int statics, zval *return_value)
3702+
static void add_class_vars(zend_class_entry *ce, zend_bool statics, zval *return_value)
36773703
{
36783704
zend_property_info *prop_info;
36793705
zval *prop, prop_copy;
@@ -3686,14 +3712,14 @@ static void add_class_vars(zend_class_entry *ce, int statics, zval *return_value
36863712
prop_info->ce != ce)) {
36873713
continue;
36883714
}
3689-
prop = NULL;
3690-
if (statics && (prop_info->flags & ZEND_ACC_STATIC) != 0) {
3691-
prop = &ce->default_static_members_table[prop_info->offset];
3692-
ZVAL_DEINDIRECT(prop);
3693-
} else if (!statics && (prop_info->flags & ZEND_ACC_STATIC) == 0) {
3694-
prop = &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
3715+
3716+
zend_bool is_static = (prop_info->flags & ZEND_ACC_STATIC) != 0;
3717+
if (statics != is_static) {
3718+
continue;
36953719
}
3696-
if (!prop || (ZEND_TYPE_IS_SET(prop_info->type) && Z_ISUNDEF_P(prop))) {
3720+
3721+
prop = property_get_default(prop_info);
3722+
if (Z_ISUNDEF_P(prop)) {
36973723
continue;
36983724
}
36993725

@@ -5544,7 +5570,6 @@ ZEND_METHOD(reflection_property, hasDefaultValue)
55445570
property_reference *ref;
55455571
zend_property_info *prop_info;
55465572
zval *prop;
5547-
zend_class_entry *ce;
55485573

55495574
if (zend_parse_parameters_none() == FAILURE) {
55505575
RETURN_THROWS();
@@ -5558,15 +5583,7 @@ ZEND_METHOD(reflection_property, hasDefaultValue)
55585583
RETURN_FALSE;
55595584
}
55605585

5561-
ce = prop_info->ce;
5562-
5563-
if ((prop_info->flags & ZEND_ACC_STATIC) != 0) {
5564-
prop = &ce->default_static_members_table[prop_info->offset];
5565-
ZVAL_DEINDIRECT(prop);
5566-
} else {
5567-
prop = &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
5568-
}
5569-
5586+
prop = property_get_default(prop_info);
55705587
RETURN_BOOL(!Z_ISUNDEF_P(prop));
55715588
}
55725589
/* }}} */
@@ -5579,7 +5596,6 @@ ZEND_METHOD(reflection_property, getDefaultValue)
55795596
property_reference *ref;
55805597
zend_property_info *prop_info;
55815598
zval *prop;
5582-
zend_class_entry *ce;
55835599

55845600
if (zend_parse_parameters_none() == FAILURE) {
55855601
RETURN_THROWS();
@@ -5593,15 +5609,7 @@ ZEND_METHOD(reflection_property, getDefaultValue)
55935609
return; // throw exception?
55945610
}
55955611

5596-
ce = prop_info->ce;
5597-
5598-
if ((prop_info->flags & ZEND_ACC_STATIC) != 0) {
5599-
prop = &ce->default_static_members_table[prop_info->offset];
5600-
ZVAL_DEINDIRECT(prop);
5601-
} else {
5602-
prop = &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
5603-
}
5604-
5612+
prop = property_get_default(prop_info);
56055613
if (Z_ISUNDEF_P(prop)) {
56065614
return;
56075615
}
@@ -5613,7 +5621,7 @@ ZEND_METHOD(reflection_property, getDefaultValue)
56135621
/* this is necessary to make it able to work with default array
56145622
* properties, returned to user */
56155623
if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) {
5616-
if (UNEXPECTED(zval_update_constant_ex(return_value, ce) != SUCCESS)) {
5624+
if (UNEXPECTED(zval_update_constant_ex(return_value, prop_info->ce) != SUCCESS)) {
56175625
RETURN_THROWS();
56185626
}
56195627
}

ext/reflection/tests/024.phpt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ Object of class [ <user> class C1 ] {
2929
}
3030

3131
- Properties [3] {
32-
Property [ <default> private $p1 ]
33-
Property [ <default> protected $p2 ]
34-
Property [ <default> public $p3 ]
32+
Property [ <default> private $p1 = 1 ]
33+
Property [ <default> protected $p2 = 2 ]
34+
Property [ <default> public $p3 = 3 ]
3535
}
3636

3737
- Dynamic properties [1] {

ext/reflection/tests/ReflectionClass_export_basic2.phpt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ Class c {
66
private $a;
77
static private $b;
88
public ?int $c = 42;
9+
public Foo $d;
910
}
1011

1112
class d extends c {}
@@ -15,29 +16,30 @@ echo new ReflectionClass("d"), "\n";
1516
?>
1617
--EXPECTF--
1718
Class [ <user> class c ] {
18-
@@ %s 2-6
19+
@@ %s 2-7
1920

2021
- Constants [0] {
2122
}
2223

2324
- Static properties [1] {
24-
Property [ private static $b ]
25+
Property [ private static $b = NULL ]
2526
}
2627

2728
- Static methods [0] {
2829
}
2930

30-
- Properties [2] {
31-
Property [ <default> private $a ]
32-
Property [ <default> public ?int $c ]
31+
- Properties [3] {
32+
Property [ <default> private $a = NULL ]
33+
Property [ <default> public ?int $c = 42 ]
34+
Property [ <default> public Foo $d ]
3335
}
3436

3537
- Methods [0] {
3638
}
3739
}
3840

3941
Class [ <user> class d extends c ] {
40-
@@ %s 8-8
42+
@@ %s 9-9
4143

4244
- Constants [0] {
4345
}
@@ -48,8 +50,9 @@ Class [ <user> class d extends c ] {
4850
- Static methods [0] {
4951
}
5052

51-
- Properties [1] {
52-
Property [ <default> public ?int $c ]
53+
- Properties [2] {
54+
Property [ <default> public ?int $c = 42 ]
55+
Property [ <default> public Foo $d ]
5356
}
5457

5558
- Methods [0] {

ext/reflection/tests/ReflectionClass_toString_001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Class [ <internal:Reflection> class ReflectionClass implements Reflector, String
2424
}
2525

2626
- Properties [1] {
27-
Property [ <default> public $name ]
27+
Property [ <default> public $name = '' ]
2828
}
2929

3030
- Methods [53] {

ext/reflection/tests/ReflectionObject___toString_basic1.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Object of class [ <user> class Foo ] {
2525
}
2626

2727
- Properties [1] {
28-
Property [ <default> public $bar ]
28+
Property [ <default> public $bar = 1 ]
2929
}
3030

3131
- Dynamic properties [0] {

ext/reflection/tests/ReflectionObject___toString_basic2.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Object of class [ <user> class Foo ] {
2626
}
2727

2828
- Properties [1] {
29-
Property [ <default> public $bar ]
29+
Property [ <default> public $bar = 1 ]
3030
}
3131

3232
- Dynamic properties [2] {

ext/reflection/tests/ReflectionObject_export_basic1.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ Object of class [ <user> class Foo ] {
2525
}
2626

2727
- Properties [1] {
28-
Property [ <default> public $bar ]
28+
Property [ <default> public $bar = 1 ]
2929
}
3030

3131
- Dynamic properties [0] {

ext/reflection/tests/ReflectionObject_export_basic2.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ Object of class [ <user> class Foo ] {
2626
}
2727

2828
- Properties [1] {
29-
Property [ <default> public $bar ]
29+
Property [ <default> public $bar = 1 ]
3030
}
3131

3232
- Dynamic properties [2] {

ext/reflection/tests/ReflectionProperty_basic1.phpt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ reflectProperty("TestClass", "prot");
4343
reflectProperty("TestClass", "priv");
4444

4545
?>
46-
--EXPECTF--
46+
--EXPECT--
4747
**********************************
4848
Reflecting on property TestClass::pub
4949

5050
__toString():
51-
string(35) "Property [ <default> public $pub ]
51+
string(42) "Property [ <default> public $pub = NULL ]
5252
"
5353
getName():
5454
string(3) "pub"
@@ -70,7 +70,7 @@ string(8) "NewValue"
7070
Reflecting on property TestClass::stat
7171

7272
__toString():
73-
string(33) "Property [ public static $stat ]
73+
string(53) "Property [ public static $stat = 'static property' ]
7474
"
7575
getName():
7676
string(4) "stat"
@@ -92,7 +92,7 @@ string(8) "NewValue"
9292
Reflecting on property TestClass::prot
9393

9494
__toString():
95-
string(39) "Property [ <default> protected $prot ]
95+
string(43) "Property [ <default> protected $prot = 4 ]
9696
"
9797
getName():
9898
string(4) "prot"
@@ -110,7 +110,7 @@ bool(false)
110110
Reflecting on property TestClass::priv
111111

112112
__toString():
113-
string(37) "Property [ <default> private $priv ]
113+
string(49) "Property [ <default> private $priv = 'keepOut' ]
114114
"
115115
getName():
116116
string(4) "priv"

ext/reflection/tests/bug45571.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ Class [ <user> class C extends A ] {
2525
}
2626

2727
- Static properties [2] {
28-
Property [ protected static $b ]
29-
Property [ public static $c ]
28+
Property [ protected static $b = 1 ]
29+
Property [ public static $c = 2 ]
3030
}
3131

3232
- Static methods [0] {

0 commit comments

Comments
 (0)