From 4b0f9586db3d74e5c3228311f4fece7f77ccb898 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Mon, 2 May 2016 11:42:06 +0200 Subject: [PATCH] Add missing update_constants in ReflectionClassConstant Also fix indentation of __toString(). --- ext/reflection/php_reflection.c | 17 +++++-- .../tests/ReflectionClassConstant_basic1.phpt | 24 +++++----- .../ReflectionClassConstant_getValue.phpt | 47 +++++++++++++++++++ 3 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 ext/reflection/tests/ReflectionClassConstant_getValue.phpt diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 5b8569e9f20..2a7ff61957c 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -456,8 +456,8 @@ static void _class_string(string *str, zend_class_entry *ce, zval *obj, char *in zend_class_constant *c; ZEND_HASH_FOREACH_STR_KEY_PTR(&ce->constants_table, key, c) { - zval_update_constant_ex(&c->value, NULL); - _class_const_string(str, ZSTR_VAL(key), c, indent); + zval_update_constant_ex(&c->value, c->ce); + _class_const_string(str, ZSTR_VAL(key), c, ZSTR_VAL(sub_indent.buf)); } ZEND_HASH_FOREACH_END(); } string_printf(str, "%s }\n", indent); @@ -633,11 +633,15 @@ static void _const_string(string *str, char *name, zval *value, char *indent) /* {{{ _class_const_string */ static void _class_const_string(string *str, char *name, zend_class_constant *c, char *indent) { - char *type = zend_zval_type_name(&c->value); char *visibility = zend_visibility_string(Z_ACCESS_FLAGS(c->value)); - zend_string *value_str = zval_get_string(&c->value); + zend_string *value_str; + char *type; - string_printf(str, "%s Constant [ %s %s %s ] { %s }\n", + zval_update_constant_ex(&c->value, c->ce); + value_str = zval_get_string(&c->value); + type = zend_zval_type_name(&c->value); + + string_printf(str, "%sConstant [ %s %s %s ] { %s }\n", indent, visibility, type, name, ZSTR_VAL(value_str)); zend_string_release(value_str); @@ -3821,6 +3825,9 @@ ZEND_METHOD(reflection_class_constant, getValue) GET_REFLECTION_OBJECT_PTR(ref); ZVAL_DUP(return_value, &ref->value); + if (Z_CONSTANT_P(return_value)) { + zval_update_constant_ex(return_value, ref->ce); + } } /* }}} */ diff --git a/ext/reflection/tests/ReflectionClassConstant_basic1.phpt b/ext/reflection/tests/ReflectionClassConstant_basic1.phpt index 3d4f49f144a..fd8118650fc 100644 --- a/ext/reflection/tests/ReflectionClassConstant_basic1.phpt +++ b/ext/reflection/tests/ReflectionClassConstant_basic1.phpt @@ -53,13 +53,13 @@ reflectClassConstant($instance, "BAD_CONST"); Reflecting on class constant TestClass::PUB __toString(): -string(42) " Constant [ public boolean PUB ] { 1 } +string(38) "Constant [ public boolean PUB ] { 1 } " export(): -string(42) " Constant [ public boolean PUB ] { 1 } +string(38) "Constant [ public boolean PUB ] { 1 } " export(): - Constant [ public boolean PUB ] { 1 } +Constant [ public boolean PUB ] { 1 } NULL getName(): @@ -87,13 +87,13 @@ string(21) "/** My Doc comment */" Reflecting on class constant TestClass::PROT __toString(): -string(46) " Constant [ protected integer PROT ] { 4 } +string(42) "Constant [ protected integer PROT ] { 4 } " export(): -string(46) " Constant [ protected integer PROT ] { 4 } +string(42) "Constant [ protected integer PROT ] { 4 } " export(): - Constant [ protected integer PROT ] { 4 } +Constant [ protected integer PROT ] { 4 } NULL getName(): @@ -121,13 +121,13 @@ string(26) "/** Another doc comment */" Reflecting on class constant TestClass::PRIV __toString(): -string(49) " Constant [ private string PRIV ] { keepOut } +string(45) "Constant [ private string PRIV ] { keepOut } " export(): -string(49) " Constant [ private string PRIV ] { keepOut } +string(45) "Constant [ private string PRIV ] { keepOut } " export(): - Constant [ private string PRIV ] { keepOut } +Constant [ private string PRIV ] { keepOut } NULL getName(): @@ -155,13 +155,13 @@ bool(false) Reflecting on class constant TestClass::PRIV __toString(): -string(49) " Constant [ private string PRIV ] { keepOut } +string(45) "Constant [ private string PRIV ] { keepOut } " export(): -string(49) " Constant [ private string PRIV ] { keepOut } +string(45) "Constant [ private string PRIV ] { keepOut } " export(): - Constant [ private string PRIV ] { keepOut } +Constant [ private string PRIV ] { keepOut } NULL getName(): diff --git a/ext/reflection/tests/ReflectionClassConstant_getValue.phpt b/ext/reflection/tests/ReflectionClassConstant_getValue.phpt new file mode 100644 index 00000000000..e447d15357b --- /dev/null +++ b/ext/reflection/tests/ReflectionClassConstant_getValue.phpt @@ -0,0 +1,47 @@ +--TEST-- +Test variations of getting constant values +--FILE-- +getValue()); +echo new ReflectionClassConstant('B', 'X'); +echo new ReflectionClass('C'); + +?> +--EXPECTF-- +int(2) +Constant [ public integer X ] { 2 } +Class [ class C ] { + @@ %s 12-15 + + - Constants [2] { + Constant [ public integer X ] { 2 } + Constant [ public integer Y ] { 1 } + } + + - Static properties [0] { + } + + - Static methods [0] { + } + + - Properties [0] { + } + + - Methods [0] { + } +}