Getting function attributes from $scope #12510
-
Hi, first, a bit of a context: as part of my disallowed calls extension, I'm writing a custom rule that would report an error when a function or a method is called in a function based on the attribute on the function. In the following code, #[\Attribute]
function one(): void
{
call();
}
function two(): void
{
call();
} And here's the problem I'm facing: using I have tried several things, for example $this->reflectionProvider->getFunction(new Name($scope->getFunction()->getName()), $scope); this returns I have also tried checking Strings would be fine, even though the best would be to get the attributes as a list of $scope->getClassReflection()->getNativeReflection()->getMethod($scope->getFunction()->getName())->getAttributes() Any help is appreciated, thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 1 reply
-
I'm now using (new \PHPStan\BetterReflection\Reflection\Adapter\ReflectionFunction($this->reflector->reflectFunction($functionName)))->getAttributes(); which is taken from Not sure if this is the best way there is, but couldn't figure out anything better. Would still appreciate an expert insight on this :-) |
Beta Was this translation helpful? Give feedback.
-
Wouldn't it be nice 🎵 to have easy access to attributes ON EVERYTHING!? Well, I just spent a few hours today on that 😊 phpstan/phpstan-src@a387fa3 You can now call I'm going to release it in a few days, but you can already require I welcome any feedback about it. |
Beta Was this translation helpful? Give feedback.
-
Indeed, that would be nice
|
Beta Was this translation helpful? Give feedback.
-
Tried supporting 1.x as long as it is easy enough, but this may be where I'll draw the line :-)
Dis/allow a specified method call only in a class which has a given attribute on any method in the class (a feature request here) so all methods is fine I guess :-)
This is cool, thanks! |
Beta Was this translation helpful? Give feedback.
Wouldn't it be nice 🎵 to have easy access to attributes ON EVERYTHING!? Well, I just spent a few hours today on that 😊 phpstan/phpstan-src@a387fa3
You can now call
->getAttributes()
on any thinkable reflection object, including$scope->getFunction()
and you'll get a new PHPStan's AttributeReflection class: https://apiref.phpstan.org/2.1.x/PHPStan.Reflection.AttributeReflection.htmlI'm going to release it in a few days, but you can already require
"phpstan/phpstan": "^2.1.3"
(alongside minimum-stability: dev and prefer-stable: true) and take advantage of it!I welcome any feedback about it.