From 7e321004b2ef4f4960d3122f9c5ba16939732b3d Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sat, 9 Dec 2023 21:37:52 +0100 Subject: [PATCH 1/2] Implement GH-12908: Show attribute name/class in ReflectionAttribute dump This is consistent with how many other Reflection classes have a name field, and it makes debugging easier. --- ext/reflection/php_reflection.c | 1 + ext/reflection/php_reflection.stub.php | 2 ++ ext/reflection/php_reflection_arginfo.h | 8 +++++++- ext/reflection/tests/gh12908.phpt | 26 +++++++++++++++++++++++++ 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 ext/reflection/tests/gh12908.phpt diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index 53e978604ad91..e272209d45180 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -1122,6 +1122,7 @@ static void reflection_attribute_factory(zval *object, HashTable *attributes, ze reference->target = target; intern->ptr = reference; intern->ref_type = REF_TYPE_ATTRIBUTE; + ZVAL_STR_COPY(reflection_prop_name(object), data->name); } /* }}} */ diff --git a/ext/reflection/php_reflection.stub.php b/ext/reflection/php_reflection.stub.php index 06b58358101b3..41768b6f61b20 100644 --- a/ext/reflection/php_reflection.stub.php +++ b/ext/reflection/php_reflection.stub.php @@ -764,6 +764,8 @@ class ReflectionAttribute implements Reflector /** @cvalue REFLECTION_ATTRIBUTE_IS_INSTANCEOF */ public const int IS_INSTANCEOF = UNKNOWN; + public string $name; + public function getName(): string {} public function getTarget(): int {} public function isRepeated(): bool {} diff --git a/ext/reflection/php_reflection_arginfo.h b/ext/reflection/php_reflection_arginfo.h index 7683128694171..141bc9ec14037 100644 --- a/ext/reflection/php_reflection_arginfo.h +++ b/ext/reflection/php_reflection_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit the .stub.php file instead. - * Stub hash: 11fcaa654fc0442e8338f6849b91aa2970b211b9 */ + * Stub hash: 0adb92be59fadce74bb12017fe64ffbccab2a060 */ ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_Reflection_getModifierNames, 0, 1, IS_ARRAY, 0) ZEND_ARG_TYPE_INFO(0, modifiers, IS_LONG, 0) @@ -1613,6 +1613,12 @@ static zend_class_entry *register_class_ReflectionAttribute(zend_class_entry *cl zend_declare_typed_class_constant(class_entry, const_IS_INSTANCEOF_name, &const_IS_INSTANCEOF_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_LONG)); zend_string_release(const_IS_INSTANCEOF_name); + zval property_name_default_value; + ZVAL_UNDEF(&property_name_default_value); + zend_string *property_name_name = zend_string_init("name", sizeof("name") - 1, 1); + zend_declare_typed_property(class_entry, property_name_name, &property_name_default_value, ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING)); + zend_string_release(property_name_name); + return class_entry; } diff --git a/ext/reflection/tests/gh12908.phpt b/ext/reflection/tests/gh12908.phpt new file mode 100644 index 0000000000000..8a66feb5be995 --- /dev/null +++ b/ext/reflection/tests/gh12908.phpt @@ -0,0 +1,26 @@ +--TEST-- +GH-12908 (Show attribute name/class in ReflectionAttribute dump) +--FILE-- +getAttributes()); + +?> +--EXPECT-- +Array +( + [0] => ReflectionAttribute Object + ( + [name] => AllowDynamicProperties + ) + + [1] => ReflectionAttribute Object + ( + [name] => Foo + ) + +) From edafedf7b4efff321e6cb109d210e691db04cd19 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Sat, 9 Dec 2023 22:24:35 +0100 Subject: [PATCH 2/2] Fix test --- Zend/tests/attributes/031_backtrace.phpt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Zend/tests/attributes/031_backtrace.phpt b/Zend/tests/attributes/031_backtrace.phpt index 504e356e7cc8d..c24d5643724cc 100644 --- a/Zend/tests/attributes/031_backtrace.phpt +++ b/Zend/tests/attributes/031_backtrace.phpt @@ -52,7 +52,9 @@ array(2) { ["class"]=> string(19) "ReflectionAttribute" ["object"]=> - object(ReflectionAttribute)#2 (0) { + object(ReflectionAttribute)#2 (1) { + ["name"]=> + string(11) "MyAttribute" } ["type"]=> string(2) "->"