Skip to content

Commit be19048

Browse files
committed
Add attribute arguments to output when passed.
1 parent 1e223eb commit be19048

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

ext/reflection/php_reflection.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6403,6 +6403,8 @@ ZEND_METHOD(ReflectionAttribute, __toString)
64036403
reflection_object *intern;
64046404
attribute_reference *attr;
64056405
smart_str str = {0};
6406+
zval tmp;
6407+
int i;
64066408

64076409
if (zend_parse_parameters_none() == FAILURE) {
64086410
RETURN_THROWS();
@@ -6412,7 +6414,37 @@ ZEND_METHOD(ReflectionAttribute, __toString)
64126414

64136415
smart_str_append_printf(&str, "Attribute [ ");
64146416
smart_str_append_printf(&str, "%s", ZSTR_VAL(attr->data->name));
6415-
smart_str_append_printf(&str, " ]\n");
6417+
smart_str_append_printf(&str, " ]");
6418+
6419+
if (attr->data->argc > 0) {
6420+
smart_str_append_printf(&str, " {\n");
6421+
smart_str_append_printf(&str, " - Arguments [%d] {\n", attr->data->argc);
6422+
6423+
for (i = 0; i < attr->data->argc; i++) {
6424+
if (FAILURE == zend_get_attribute_value(&tmp, attr->data, i, attr->scope)) {
6425+
RETURN_THROWS();
6426+
}
6427+
6428+
if (attr->data->args[i].name != NULL) {
6429+
smart_str_append_printf(&str, " Argument #%d [ %s = ", i, ZSTR_VAL(attr->data->args[i].name));
6430+
} else {
6431+
smart_str_append_printf(&str, " Argument #%d [ ", i);
6432+
}
6433+
6434+
if (format_default_value(&str, &tmp, NULL) == FAILURE) {
6435+
return;
6436+
}
6437+
6438+
smart_str_append_printf(&str, " ]\n");
6439+
6440+
zval_ptr_dtor(&tmp);
6441+
}
6442+
smart_str_append_printf(&str, " }\n");
6443+
6444+
smart_str_append_printf(&str, "}\n");
6445+
} else {
6446+
smart_str_append_printf(&str, "\n");
6447+
}
64166448

64176449
RETURN_STR(smart_str_extract(&str));
64186450
}

ext/reflection/tests/ReflectionAttribute_toString.phpt

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,24 @@ ReflectionAttribute::__toString
33
--FILE--
44
<?php
55

6-
#[Foo]
6+
#[Foo, Bar(a: "foo", b: 1234), Baz("foo", 1234)]
77
function foo() {}
88

99
$refl = new ReflectionFunction('foo');
1010
echo $refl->getAttributes()[0];
11+
echo $refl->getAttributes()[1];
12+
echo $refl->getAttributes()[2];
1113
--EXPECTF--
1214
Attribute [ Foo ]
15+
Attribute [ Bar ] {
16+
- Arguments [2] {
17+
Argument #0 [ a = 'foo' ]
18+
Argument #1 [ b = 1234 ]
19+
}
20+
}
21+
Attribute [ Baz ] {
22+
- Arguments [2] {
23+
Argument #0 [ 'foo' ]
24+
Argument #1 [ 1234 ]
25+
}
26+
}

0 commit comments

Comments
 (0)