Replacement for recent BetterReflection API deprecation? #10426
Replies: 3 comments 1 reply
-
|
Hi, your suspicion is right, there isn't a better way and newInstance on ReflectionAttribute should probably be un-deprecated. It's just that mixing runtime values with mostly static reflection feels very dirty. In the ideal world, yeah, you'd use getArgumentsExpressions and turn them into Type with InitializerExprTypeResolver, which would give you some understanding what the Attribute consists of, but there you're relying on knowing how the attribute's constructor looks like. As for the documentation: There's https://phpstan.org/developing-extensions/extension-types and https://apiref.phpstan.org/1.11.x/namespace-PHPStan.html. |
Beta Was this translation helpful? Give feedback.
-
|
I agree that it's kinda dirty, but I don't think there's a way to access the "raw" data of the I did indeed find both those links, but none of the docs from the first link seem to mention attributes at all. The second link is pretty much just the code and accompanying doc blocks presented in a different format, which doesn't always (clearly) explain what certain functions are used for or how to use them. Example: I found another |
Beta Was this translation helpful? Give feedback.
-
|
As you likely already noticed I've filed the feature request. I've also addressed some potential issues there, including the last one you mentioned. Thanks so far. :> |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
This doesn't seem to be a bug so I figured a discussion is more appropriate than creating an issue.
It seems commit ondrejmirtes/BetterReflection@30fd205 causes the following problem when trying to read a class's attributes from a custom rule (phpstan 1.10.55 with PHP 8.3.0):
The attribute is used like so:
Using this code for the rule:
What's the replacement for
newInstance()here, or how else would I accessmy_class_nameand getFoo::classas the value? I find the documentation about phpstan's own APIs to be very sparse.$class_attributes[0]->getArgumentsExpressions()seems to indirectly return the attribute's arguments, but their names (my_class_name) are not there. Of course I could simply access it like[0], but things have names for a reason. :> And as far I can tell I would still have to do some "magic" to reconstruct the actual arguments, which just doesn't seem right.Beta Was this translation helpful? Give feedback.
All reactions