Skip to content

Bugfix 80097: Have ReflectionAttribute implement Reflector, __toString #6117

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 54 additions & 1 deletion ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -6397,6 +6397,59 @@ ZEND_METHOD(ReflectionAttribute, __clone)
_DO_THROW("Cannot clone object using __clone()");
}

/* {{{ Returns a string representation */
ZEND_METHOD(ReflectionAttribute, __toString)
{
reflection_object *intern;
attribute_reference *attr;
smart_str str = {0};
zval tmp;
int i;

if (zend_parse_parameters_none() == FAILURE) {
RETURN_THROWS();
}

GET_REFLECTION_OBJECT_PTR(attr);

smart_str_appends(&str, "Attribute [ ");
smart_str_append(&str, "%s", attr->data->name);
smart_str_append_printf(&str, " ]");

if (attr->data->argc > 0) {
smart_str_append_printf(&str, " {\n");
smart_str_append_printf(&str, " - Arguments [%d] {\n", attr->data->argc);

for (i = 0; i < attr->data->argc; i++) {
if (FAILURE == zend_get_attribute_value(&tmp, attr->data, i, attr->scope)) {
RETURN_THROWS();
}

if (attr->data->args[i].name != NULL) {
smart_str_append_printf(&str, " Argument #%d [ %s = ", i, ZSTR_VAL(attr->data->args[i].name));
} else {
smart_str_append_printf(&str, " Argument #%d [ ", i);
}

if (format_default_value(&str, &tmp, NULL) == FAILURE) {
return;
}

smart_str_append_printf(&str, " ]\n");

zval_ptr_dtor(&tmp);
}
smart_str_append_printf(&str, " }\n");

smart_str_append_printf(&str, "}\n");
} else {
smart_str_append_printf(&str, "\n");
}

RETURN_STR(smart_str_extract(&str));
}
/* }}} */

/* {{{ * Returns the name of the attribute */
ZEND_METHOD(ReflectionAttribute, getName)
{
Expand Down Expand Up @@ -7062,7 +7115,7 @@ PHP_MINIT_FUNCTION(reflection) /* {{{ */
reflection_reference_ptr = register_class_ReflectionReference();
reflection_init_class_handlers(reflection_reference_ptr);

reflection_attribute_ptr = register_class_ReflectionAttribute();
reflection_attribute_ptr = register_class_ReflectionAttribute(reflector_ptr);
reflection_init_class_handlers(reflection_attribute_ptr);

reflection_enum_ptr = register_class_ReflectionEnum(reflection_class_ptr);
Expand Down
4 changes: 3 additions & 1 deletion ext/reflection/php_reflection.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,14 +680,16 @@ private function __clone(): void {}
private function __construct() {}
}

final class ReflectionAttribute
final class ReflectionAttribute implements Reflector
{
public function getName(): string {}
public function getTarget(): int {}
public function isRepeated(): bool {}
public function getArguments(): array {}
public function newInstance(): object {}

public function __toString(): string {}

private function __clone(): void {}

private function __construct() {}
Expand Down
9 changes: 7 additions & 2 deletions ext/reflection/php_reflection_arginfo.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: b7cb399903fb1965ba6294c4dcf9364539e93b5b */
* Stub hash: 3a424463071c9252fd0b6551d8b6e794f139f8f7 */

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)
Expand Down Expand Up @@ -539,6 +539,8 @@ ZEND_END_ARG_INFO()
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_ReflectionAttribute_newInstance, 0, 0, IS_OBJECT, 0)
ZEND_END_ARG_INFO()

#define arginfo_class_ReflectionAttribute___toString arginfo_class_ReflectionFunction___toString

#define arginfo_class_ReflectionAttribute___clone arginfo_class_ReflectionFunctionAbstract___clone

#define arginfo_class_ReflectionAttribute___construct arginfo_class_ReflectionReference___construct
Expand Down Expand Up @@ -794,6 +796,7 @@ ZEND_METHOD(ReflectionAttribute, getTarget);
ZEND_METHOD(ReflectionAttribute, isRepeated);
ZEND_METHOD(ReflectionAttribute, getArguments);
ZEND_METHOD(ReflectionAttribute, newInstance);
ZEND_METHOD(ReflectionAttribute, __toString);
ZEND_METHOD(ReflectionAttribute, __clone);
ZEND_METHOD(ReflectionAttribute, __construct);
ZEND_METHOD(ReflectionEnum, __construct);
Expand Down Expand Up @@ -1117,6 +1120,7 @@ static const zend_function_entry class_ReflectionAttribute_methods[] = {
ZEND_ME(ReflectionAttribute, isRepeated, arginfo_class_ReflectionAttribute_isRepeated, ZEND_ACC_PUBLIC)
ZEND_ME(ReflectionAttribute, getArguments, arginfo_class_ReflectionAttribute_getArguments, ZEND_ACC_PUBLIC)
ZEND_ME(ReflectionAttribute, newInstance, arginfo_class_ReflectionAttribute_newInstance, ZEND_ACC_PUBLIC)
ZEND_ME(ReflectionAttribute, __toString, arginfo_class_ReflectionAttribute___toString, ZEND_ACC_PUBLIC)
ZEND_ME(ReflectionAttribute, __clone, arginfo_class_ReflectionAttribute___clone, ZEND_ACC_PRIVATE)
ZEND_ME(ReflectionAttribute, __construct, arginfo_class_ReflectionAttribute___construct, ZEND_ACC_PRIVATE)
ZEND_FE_END
Expand Down Expand Up @@ -1412,13 +1416,14 @@ static zend_class_entry *register_class_ReflectionReference(void)
return class_entry;
}

static zend_class_entry *register_class_ReflectionAttribute(void)
static zend_class_entry *register_class_ReflectionAttribute(zend_class_entry *class_entry_Reflector)
{
zend_class_entry ce, *class_entry;

INIT_CLASS_ENTRY(ce, "ReflectionAttribute", class_ReflectionAttribute_methods);
class_entry = zend_register_internal_class_ex(&ce, NULL);
class_entry->ce_flags |= ZEND_ACC_FINAL;
zend_class_implements(class_entry, 1, class_entry_Reflector);

return class_entry;
}
Expand Down
26 changes: 26 additions & 0 deletions ext/reflection/tests/ReflectionAttribute_toString.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
ReflectionAttribute::__toString
--FILE--
<?php

#[Foo, Bar(a: "foo", b: 1234), Baz("foo", 1234)]
function foo() {}

$refl = new ReflectionFunction('foo');
echo $refl->getAttributes()[0];
echo $refl->getAttributes()[1];
echo $refl->getAttributes()[2];
--EXPECTF--
Attribute [ Foo ]
Attribute [ Bar ] {
- Arguments [2] {
Argument #0 [ a = 'foo' ]
Argument #1 [ b = 1234 ]
}
}
Attribute [ Baz ] {
Copy link
Member

@nikic nikic Jun 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks okay, but I wonder if it would make sense to print it as

Attribute [ Baz('foo', 1234) ]

or even

#[Baz('foo', 1234)]

instead?

Maybe this doesn't compose well when included in other reflection output though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the idea was that this gets its own _attribute_string at some point and nests into the existing reflection structures that look this way.

- Arguments [2] {
Argument #0 [ 'foo' ]
Argument #1 [ 1234 ]
}
}