]> granicus.if.org Git - php/commitdiff
Display property default value in reflection dumps
authorNikita Popov <nikita.ppv@gmail.com>
Thu, 2 Apr 2020 09:21:48 +0000 (11:21 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Thu, 2 Apr 2020 09:21:48 +0000 (11:21 +0200)
ext/reflection/php_reflection.c
ext/reflection/tests/024.phpt
ext/reflection/tests/ReflectionClass_export_basic2.phpt
ext/reflection/tests/ReflectionClass_toString_001.phpt
ext/reflection/tests/ReflectionObject___toString_basic1.phpt
ext/reflection/tests/ReflectionObject___toString_basic2.phpt
ext/reflection/tests/ReflectionObject_export_basic1.phpt
ext/reflection/tests/ReflectionObject_export_basic2.phpt
ext/reflection/tests/ReflectionProperty_basic1.phpt
ext/reflection/tests/bug45571.phpt

index 018f97e46201b869d394ceb500fcc72b8cb76da3..77037f9571533bef86a4ad44da2acefcef855230 100644 (file)
@@ -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();
                }
        }
index 0fb1cd6fe23117b0bacca5a85c27b061e3b1c7ba..3582619aa5c6e6eda07002c4c545e1ccbda2e85a 100644 (file)
@@ -29,9 +29,9 @@ Object of class [ <user> class C1 ] {
   }
 
   - Properties [3] {
-    Property [ <default> private $p1 ]
-    Property [ <default> protected $p2 ]
-    Property [ <default> public $p3 ]
+    Property [ <default> private $p1 = 1 ]
+    Property [ <default> protected $p2 = 2 ]
+    Property [ <default> public $p3 = 3 ]
   }
 
   - Dynamic properties [1] {
index 6f3c54c88a28861c2ce5ed771ac622388523c546..f222eb073fd61bf374e35c6bc0dbf2754f8758b1 100644 (file)
@@ -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 [ <user> 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 [ <default> private $a ]
-    Property [ <default> public ?int $c ]
+  - Properties [3] {
+    Property [ <default> private $a = NULL ]
+    Property [ <default> public ?int $c = 42 ]
+    Property [ <default> public Foo $d ]
   }
 
   - Methods [0] {
@@ -37,7 +39,7 @@ Class [ <user> class c ] {
 }
 
 Class [ <user> class d extends c ] {
-  @@ %s 8-8
+  @@ %s 9-9
 
   - Constants [0] {
   }
@@ -48,8 +50,9 @@ Class [ <user> class d extends c ] {
   - Static methods [0] {
   }
 
-  - Properties [1] {
-    Property [ <default> public ?int $c ]
+  - Properties [2] {
+    Property [ <default> public ?int $c = 42 ]
+    Property [ <default> public Foo $d ]
   }
 
   - Methods [0] {
index 4eb82b96f3723290eafce157d39e57c9cec72c0a..a00478e7d855e2ac90c97dda0e5435f07ed6e397 100644 (file)
@@ -24,7 +24,7 @@ Class [ <internal:Reflection> class ReflectionClass implements Reflector, String
   }
 
   - Properties [1] {
-    Property [ <default> public $name ]
+    Property [ <default> public $name = '' ]
   }
 
   - Methods [53] {
index fdd25739bb1194638e1c3ee4efe1e7be302fb0a0..1f647178635bbf15cafca38b429fc234f3f89fc1 100644 (file)
@@ -25,7 +25,7 @@ Object of class [ <user> class Foo ] {
   }
 
   - Properties [1] {
-    Property [ <default> public $bar ]
+    Property [ <default> public $bar = 1 ]
   }
 
   - Dynamic properties [0] {
index db119c92b6451faa6cefdd7a4665950c498bd80c..8de730e9e3598c058c035e529b0dae437dfeb0e6 100644 (file)
@@ -26,7 +26,7 @@ Object of class [ <user> class Foo ] {
   }
 
   - Properties [1] {
-    Property [ <default> public $bar ]
+    Property [ <default> public $bar = 1 ]
   }
 
   - Dynamic properties [2] {
index fdd25739bb1194638e1c3ee4efe1e7be302fb0a0..1f647178635bbf15cafca38b429fc234f3f89fc1 100644 (file)
@@ -25,7 +25,7 @@ Object of class [ <user> class Foo ] {
   }
 
   - Properties [1] {
-    Property [ <default> public $bar ]
+    Property [ <default> public $bar = 1 ]
   }
 
   - Dynamic properties [0] {
index db119c92b6451faa6cefdd7a4665950c498bd80c..8de730e9e3598c058c035e529b0dae437dfeb0e6 100644 (file)
@@ -26,7 +26,7 @@ Object of class [ <user> class Foo ] {
   }
 
   - Properties [1] {
-    Property [ <default> public $bar ]
+    Property [ <default> public $bar = 1 ]
   }
 
   - Dynamic properties [2] {
index 9bdf98b6e05ca8782599686b689e6c479bf5ceee..ed14830bdebd78f554675b8277d3f029a60c9586 100644 (file)
@@ -43,12 +43,12 @@ reflectProperty("TestClass", "prot");
 reflectProperty("TestClass", "priv");
 
 ?>
---EXPECTF--
+--EXPECT--
 **********************************
 Reflecting on property TestClass::pub
 
 __toString():
-string(35) "Property [ <default> public $pub ]
+string(42) "Property [ <default> 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 [ <default> protected $prot ]
+string(43) "Property [ <default> protected $prot = 4 ]
 "
 getName():
 string(4) "prot"
@@ -110,7 +110,7 @@ bool(false)
 Reflecting on property TestClass::priv
 
 __toString():
-string(37) "Property [ <default> private $priv ]
+string(49) "Property [ <default> private $priv = 'keepOut' ]
 "
 getName():
 string(4) "priv"
index aa482193a7f8a62ae8ba4c1a04242d8f1140bd4d..7b48859564a89ee32553a8267466e3728aee73b3 100644 (file)
@@ -25,8 +25,8 @@ Class [ <user> 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] {