Skip to content

Implement GH-12908: Show attribute name/class in ReflectionAttribute … #12917

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
4 changes: 3 additions & 1 deletion Zend/tests/attributes/031_backtrace.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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) "->"
Expand Down
1 change: 1 addition & 0 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

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

why is this needed and not in b0e83aa?

Copy link
Member Author

Choose a reason for hiding this comment

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

Because there is already code that copies into the property table for the case you linked.

Copy link
Member

Choose a reason for hiding this comment

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

My commit got reverted, I missed the fact that the property is inherited from ReflectionFunctionAbstract.

}
/* }}} */

Expand Down
2 changes: 2 additions & 0 deletions ext/reflection/php_reflection.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,8 @@ class ReflectionAttribute implements Reflector
/** @cvalue REFLECTION_ATTRIBUTE_IS_INSTANCEOF */
public const int IS_INSTANCEOF = UNKNOWN;

public string $name;
Copy link
Member

Choose a reason for hiding this comment

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

Should be readonly?

Copy link
Member Author

Choose a reason for hiding this comment

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

Perhaps, although the others aren't. The readonly-ness is handled manually by _reflection_write_property and it throws a ReflectionException. So for consistency I went this route.
The readonly modifier makes it throw an Error, so changing the behaviour of the others would be a small BC break.

Copy link
Member

Choose a reason for hiding this comment

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

I also think this should be possible because changing the property makes zero sense, and makes no difference. Some people may naively think that the name property has an effect on the attribute itself.

TBH we also made a few properties readonly in the past, so I think it's doable now as well, unless some very popular library (e.g. BetterReflection) redefine them.

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, I'll check BetterReflection some time later and if that's okay I'll drop the special handling and let these use readonly

Copy link
Member Author

Choose a reason for hiding this comment

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

Here is the patch to remove the special handling: https://gist.github.com/nielsdos/e69d6b826fecf60a7c3fedce3f83ff7f

However, we can't apply that patch. BetterReflection relies on the behaviour (e.g. Roave/BetterReflection@3eedf48 but there are more cases, also exception classes in catch would be required to change) and that patch therefore causes 562 test failures.


public function getName(): string {}
public function getTarget(): int {}
public function isRepeated(): bool {}
Expand Down
8 changes: 7 additions & 1 deletion ext/reflection/php_reflection_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions ext/reflection/tests/gh12908.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
--TEST--
GH-12908 (Show attribute name/class in ReflectionAttribute dump)
--FILE--
<?php

#[\AllowDynamicProperties, \Foo]
class Test {
}

print_r((new ReflectionClass(Test::class))->getAttributes());

?>
--EXPECT--
Array
(
[0] => ReflectionAttribute Object
(
[name] => AllowDynamicProperties
)

[1] => ReflectionAttribute Object
(
[name] => Foo
)

)