From: Nikita Popov Date: Thu, 2 Apr 2020 09:21:48 +0000 (+0200) Subject: Display property default value in reflection dumps X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=083b0c38a0aad768403d9fc973e70e02712bd031;p=php Display property default value in reflection dumps --- diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 018f97e462..77037f9571 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -584,6 +584,39 @@ static zend_op* _get_recv_op(zend_op_array *op_array, uint32_t offset) } /* }}} */ +static int format_default_value(smart_str *str, zval *value, zend_class_entry *scope) { + zval zv; + ZVAL_COPY(&zv, value); + if (UNEXPECTED(zval_update_constant_ex(&zv, scope) == FAILURE)) { + zval_ptr_dtor(&zv); + return FAILURE; + } + + if (Z_TYPE(zv) == IS_TRUE) { + smart_str_appends(str, "true"); + } else if (Z_TYPE(zv) == IS_FALSE) { + smart_str_appends(str, "false"); + } else if (Z_TYPE(zv) == IS_NULL) { + smart_str_appends(str, "NULL"); + } else if (Z_TYPE(zv) == IS_STRING) { + smart_str_appendc(str, '\''); + smart_str_appendl(str, Z_STRVAL(zv), MIN(Z_STRLEN(zv), 15)); + if (Z_STRLEN(zv) > 15) { + smart_str_appends(str, "..."); + } + smart_str_appendc(str, '\''); + } else if (Z_TYPE(zv) == IS_ARRAY) { + smart_str_appends(str, "Array"); + } else { + zend_string *tmp_zv_str; + zend_string *zv_str = zval_get_tmp_string(&zv, &tmp_zv_str); + smart_str_append(str, zv_str); + zend_tmp_string_release(tmp_zv_str); + } + zval_ptr_dtor(&zv); + return SUCCESS; +} + /* {{{ _parameter_string */ static void _parameter_string(smart_str *str, zend_function *fptr, struct _zend_arg_info *arg_info, uint32_t offset, zend_bool required, char* indent) { @@ -617,36 +650,10 @@ static void _parameter_string(smart_str *str, zend_function *fptr, struct _zend_ if (fptr->type == ZEND_USER_FUNCTION && !required) { zend_op *precv = _get_recv_op((zend_op_array*)fptr, offset); if (precv && precv->opcode == ZEND_RECV_INIT && precv->op2_type != IS_UNUSED) { - zval zv; - smart_str_appends(str, " = "); - ZVAL_COPY(&zv, RT_CONSTANT(precv, precv->op2)); - if (UNEXPECTED(zval_update_constant_ex(&zv, fptr->common.scope) == FAILURE)) { - zval_ptr_dtor(&zv); + if (format_default_value(str, RT_CONSTANT(precv, precv->op2), fptr->common.scope) == FAILURE) { return; } - if (Z_TYPE(zv) == IS_TRUE) { - smart_str_appends(str, "true"); - } else if (Z_TYPE(zv) == IS_FALSE) { - smart_str_appends(str, "false"); - } else if (Z_TYPE(zv) == IS_NULL) { - smart_str_appends(str, "NULL"); - } else if (Z_TYPE(zv) == IS_STRING) { - smart_str_appendc(str, '\''); - smart_str_appendl(str, Z_STRVAL(zv), MIN(Z_STRLEN(zv), 15)); - if (Z_STRLEN(zv) > 15) { - smart_str_appends(str, "..."); - } - smart_str_appendc(str, '\''); - } else if (Z_TYPE(zv) == IS_ARRAY) { - smart_str_appends(str, "Array"); - } else { - zend_string *tmp_zv_str; - zend_string *zv_str = zval_get_tmp_string(&zv, &tmp_zv_str); - smart_str_append(str, zv_str); - zend_tmp_string_release(tmp_zv_str); - } - zval_ptr_dtor(&zv); } } smart_str_appends(str, " ]"); @@ -818,6 +825,17 @@ static void _function_string(smart_str *str, zend_function *fptr, zend_class_ent } /* }}} */ +static zval *property_get_default(zend_property_info *prop_info) { + zend_class_entry *ce = prop_info->ce; + if (prop_info->flags & ZEND_ACC_STATIC) { + zval *prop = &ce->default_static_members_table[prop_info->offset]; + ZVAL_DEINDIRECT(prop); + return prop; + } else { + return &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)]; + } +} + /* {{{ _property_string */ static void _property_string(smart_str *str, zend_property_info *prop, const char *prop_name, char* indent) { @@ -855,6 +873,14 @@ static void _property_string(smart_str *str, zend_property_info *prop, const cha zend_unmangle_property_name(prop->name, &class_name, &prop_name); } smart_str_append_printf(str, "$%s", prop_name); + + zval *default_value = property_get_default(prop); + if (!Z_ISUNDEF_P(default_value)) { + smart_str_appends(str, " = "); + if (format_default_value(str, default_value, prop->ce) == FAILURE) { + return; + } + } } smart_str_appends(str, " ]\n"); @@ -3673,7 +3699,7 @@ ZEND_METHOD(reflection_class, __construct) /* }}} */ /* {{{ add_class_vars */ -static void add_class_vars(zend_class_entry *ce, int statics, zval *return_value) +static void add_class_vars(zend_class_entry *ce, zend_bool statics, zval *return_value) { zend_property_info *prop_info; zval *prop, prop_copy; @@ -3686,14 +3712,14 @@ static void add_class_vars(zend_class_entry *ce, int statics, zval *return_value prop_info->ce != ce)) { continue; } - prop = NULL; - if (statics && (prop_info->flags & ZEND_ACC_STATIC) != 0) { - prop = &ce->default_static_members_table[prop_info->offset]; - ZVAL_DEINDIRECT(prop); - } else if (!statics && (prop_info->flags & ZEND_ACC_STATIC) == 0) { - prop = &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)]; + + zend_bool is_static = (prop_info->flags & ZEND_ACC_STATIC) != 0; + if (statics != is_static) { + continue; } - if (!prop || (ZEND_TYPE_IS_SET(prop_info->type) && Z_ISUNDEF_P(prop))) { + + prop = property_get_default(prop_info); + if (Z_ISUNDEF_P(prop)) { continue; } @@ -5544,7 +5570,6 @@ ZEND_METHOD(reflection_property, hasDefaultValue) property_reference *ref; zend_property_info *prop_info; zval *prop; - zend_class_entry *ce; if (zend_parse_parameters_none() == FAILURE) { RETURN_THROWS(); @@ -5558,15 +5583,7 @@ ZEND_METHOD(reflection_property, hasDefaultValue) RETURN_FALSE; } - ce = prop_info->ce; - - if ((prop_info->flags & ZEND_ACC_STATIC) != 0) { - prop = &ce->default_static_members_table[prop_info->offset]; - ZVAL_DEINDIRECT(prop); - } else { - prop = &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)]; - } - + prop = property_get_default(prop_info); RETURN_BOOL(!Z_ISUNDEF_P(prop)); } /* }}} */ @@ -5579,7 +5596,6 @@ ZEND_METHOD(reflection_property, getDefaultValue) property_reference *ref; zend_property_info *prop_info; zval *prop; - zend_class_entry *ce; if (zend_parse_parameters_none() == FAILURE) { RETURN_THROWS(); @@ -5593,15 +5609,7 @@ ZEND_METHOD(reflection_property, getDefaultValue) return; // throw exception? } - ce = prop_info->ce; - - if ((prop_info->flags & ZEND_ACC_STATIC) != 0) { - prop = &ce->default_static_members_table[prop_info->offset]; - ZVAL_DEINDIRECT(prop); - } else { - prop = &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)]; - } - + prop = property_get_default(prop_info); if (Z_ISUNDEF_P(prop)) { return; } @@ -5613,7 +5621,7 @@ ZEND_METHOD(reflection_property, getDefaultValue) /* this is necessary to make it able to work with default array * properties, returned to user */ if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) { - if (UNEXPECTED(zval_update_constant_ex(return_value, ce) != SUCCESS)) { + if (UNEXPECTED(zval_update_constant_ex(return_value, prop_info->ce) != SUCCESS)) { RETURN_THROWS(); } } diff --git a/ext/reflection/tests/024.phpt b/ext/reflection/tests/024.phpt index 0fb1cd6fe2..3582619aa5 100644 --- a/ext/reflection/tests/024.phpt +++ b/ext/reflection/tests/024.phpt @@ -29,9 +29,9 @@ Object of class [ class C1 ] { } - Properties [3] { - Property [ private $p1 ] - Property [ protected $p2 ] - Property [ public $p3 ] + Property [ private $p1 = 1 ] + Property [ protected $p2 = 2 ] + Property [ public $p3 = 3 ] } - Dynamic properties [1] { diff --git a/ext/reflection/tests/ReflectionClass_export_basic2.phpt b/ext/reflection/tests/ReflectionClass_export_basic2.phpt index 6f3c54c88a..f222eb073f 100644 --- a/ext/reflection/tests/ReflectionClass_export_basic2.phpt +++ b/ext/reflection/tests/ReflectionClass_export_basic2.phpt @@ -6,6 +6,7 @@ Class c { private $a; static private $b; public ?int $c = 42; + public Foo $d; } class d extends c {} @@ -15,21 +16,22 @@ echo new ReflectionClass("d"), "\n"; ?> --EXPECTF-- Class [ class c ] { - @@ %s 2-6 + @@ %s 2-7 - Constants [0] { } - Static properties [1] { - Property [ private static $b ] + Property [ private static $b = NULL ] } - Static methods [0] { } - - Properties [2] { - Property [ private $a ] - Property [ public ?int $c ] + - Properties [3] { + Property [ private $a = NULL ] + Property [ public ?int $c = 42 ] + Property [ public Foo $d ] } - Methods [0] { @@ -37,7 +39,7 @@ Class [ class c ] { } Class [ class d extends c ] { - @@ %s 8-8 + @@ %s 9-9 - Constants [0] { } @@ -48,8 +50,9 @@ Class [ class d extends c ] { - Static methods [0] { } - - Properties [1] { - Property [ public ?int $c ] + - Properties [2] { + Property [ public ?int $c = 42 ] + Property [ public Foo $d ] } - Methods [0] { diff --git a/ext/reflection/tests/ReflectionClass_toString_001.phpt b/ext/reflection/tests/ReflectionClass_toString_001.phpt index 4eb82b96f3..a00478e7d8 100644 --- a/ext/reflection/tests/ReflectionClass_toString_001.phpt +++ b/ext/reflection/tests/ReflectionClass_toString_001.phpt @@ -24,7 +24,7 @@ Class [ class ReflectionClass implements Reflector, String } - Properties [1] { - Property [ public $name ] + Property [ public $name = '' ] } - Methods [53] { diff --git a/ext/reflection/tests/ReflectionObject___toString_basic1.phpt b/ext/reflection/tests/ReflectionObject___toString_basic1.phpt index fdd25739bb..1f64717863 100644 --- a/ext/reflection/tests/ReflectionObject___toString_basic1.phpt +++ b/ext/reflection/tests/ReflectionObject___toString_basic1.phpt @@ -25,7 +25,7 @@ Object of class [ class Foo ] { } - Properties [1] { - Property [ public $bar ] + Property [ public $bar = 1 ] } - Dynamic properties [0] { diff --git a/ext/reflection/tests/ReflectionObject___toString_basic2.phpt b/ext/reflection/tests/ReflectionObject___toString_basic2.phpt index db119c92b6..8de730e9e3 100644 --- a/ext/reflection/tests/ReflectionObject___toString_basic2.phpt +++ b/ext/reflection/tests/ReflectionObject___toString_basic2.phpt @@ -26,7 +26,7 @@ Object of class [ class Foo ] { } - Properties [1] { - Property [ public $bar ] + Property [ public $bar = 1 ] } - Dynamic properties [2] { diff --git a/ext/reflection/tests/ReflectionObject_export_basic1.phpt b/ext/reflection/tests/ReflectionObject_export_basic1.phpt index fdd25739bb..1f64717863 100644 --- a/ext/reflection/tests/ReflectionObject_export_basic1.phpt +++ b/ext/reflection/tests/ReflectionObject_export_basic1.phpt @@ -25,7 +25,7 @@ Object of class [ class Foo ] { } - Properties [1] { - Property [ public $bar ] + Property [ public $bar = 1 ] } - Dynamic properties [0] { diff --git a/ext/reflection/tests/ReflectionObject_export_basic2.phpt b/ext/reflection/tests/ReflectionObject_export_basic2.phpt index db119c92b6..8de730e9e3 100644 --- a/ext/reflection/tests/ReflectionObject_export_basic2.phpt +++ b/ext/reflection/tests/ReflectionObject_export_basic2.phpt @@ -26,7 +26,7 @@ Object of class [ class Foo ] { } - Properties [1] { - Property [ public $bar ] + Property [ public $bar = 1 ] } - Dynamic properties [2] { diff --git a/ext/reflection/tests/ReflectionProperty_basic1.phpt b/ext/reflection/tests/ReflectionProperty_basic1.phpt index 9bdf98b6e0..ed14830bde 100644 --- a/ext/reflection/tests/ReflectionProperty_basic1.phpt +++ b/ext/reflection/tests/ReflectionProperty_basic1.phpt @@ -43,12 +43,12 @@ reflectProperty("TestClass", "prot"); reflectProperty("TestClass", "priv"); ?> ---EXPECTF-- +--EXPECT-- ********************************** Reflecting on property TestClass::pub __toString(): -string(35) "Property [ public $pub ] +string(42) "Property [ public $pub = NULL ] " getName(): string(3) "pub" @@ -70,7 +70,7 @@ string(8) "NewValue" Reflecting on property TestClass::stat __toString(): -string(33) "Property [ public static $stat ] +string(53) "Property [ public static $stat = 'static property' ] " getName(): string(4) "stat" @@ -92,7 +92,7 @@ string(8) "NewValue" Reflecting on property TestClass::prot __toString(): -string(39) "Property [ protected $prot ] +string(43) "Property [ protected $prot = 4 ] " getName(): string(4) "prot" @@ -110,7 +110,7 @@ bool(false) Reflecting on property TestClass::priv __toString(): -string(37) "Property [ private $priv ] +string(49) "Property [ private $priv = 'keepOut' ] " getName(): string(4) "priv" diff --git a/ext/reflection/tests/bug45571.phpt b/ext/reflection/tests/bug45571.phpt index aa482193a7..7b48859564 100644 --- a/ext/reflection/tests/bug45571.phpt +++ b/ext/reflection/tests/bug45571.phpt @@ -25,8 +25,8 @@ Class [ class C extends A ] { } - Static properties [2] { - Property [ protected static $b ] - Property [ public static $c ] + Property [ protected static $b = 1 ] + Property [ public static $c = 2 ] } - Static methods [0] {