|
| 1 | +<?php |
| 2 | +declare(strict_types = 1); |
| 3 | + |
| 4 | +namespace Spaze\PHPStan\Rules\Disallowed\Allowed; |
| 5 | + |
| 6 | +use PHPStan\Analyser\Scope; |
| 7 | +use PHPStan\BetterReflection\Reflection\Adapter\FakeReflectionAttribute; |
| 8 | +use PHPStan\BetterReflection\Reflection\Adapter\ReflectionAttribute; |
| 9 | +use PHPStan\BetterReflection\Reflection\ReflectionAttribute as BetterReflectionAttribute; |
| 10 | +use PHPStan\BetterReflection\Reflector\Reflector; |
| 11 | + |
| 12 | +class GetAttributesWhenInSignature |
| 13 | +{ |
| 14 | + |
| 15 | + private Reflector $reflector; |
| 16 | + |
| 17 | + /** @var class-string|null */ |
| 18 | + private ?string $currentClass = null; |
| 19 | + |
| 20 | + private ?string $currentMethod = null; |
| 21 | + |
| 22 | + /** @var string|null */ |
| 23 | + private ?string $currentFunction = null; |
| 24 | + |
| 25 | + |
| 26 | + public function __construct(Reflector $reflector) |
| 27 | + { |
| 28 | + $this->reflector = $reflector; |
| 29 | + } |
| 30 | + |
| 31 | + |
| 32 | + /** |
| 33 | + * Emulates the missing $scope->getMethodOrFunctionSignature(). |
| 34 | + * |
| 35 | + * Because $scope->getFunction() returns null when the node, like for example a namespace node (instance of FullyQualified), |
| 36 | + * is inside the method or the function signature, it's impossible to get to the current method or function reflection using $scope to get its attributes. |
| 37 | + * The hacky solution is to store the current method name in a ClassMethod rule, read it here, and unset it in a InClassMethodNode rule, |
| 38 | + * or the function name in a Function_ and a InFunctionNode rules. |
| 39 | + * |
| 40 | + * @param Scope $scope |
| 41 | + * @return list<FakeReflectionAttribute|ReflectionAttribute|BetterReflectionAttribute>|null |
| 42 | + */ |
| 43 | + public function get(Scope $scope): ?array |
| 44 | + { |
| 45 | + if ( |
| 46 | + $this->currentClass !== null |
| 47 | + && $this->currentMethod !== null |
| 48 | + && $scope->isInClass() |
| 49 | + && $scope->getClassReflection()->getName() === $this->currentClass |
| 50 | + ) { |
| 51 | + return $scope->getClassReflection()->getNativeReflection()->getMethod($this->currentMethod)->getAttributes(); |
| 52 | + } elseif ($this->currentFunction !== null) { |
| 53 | + return $this->reflector->reflectFunction($this->currentFunction)->getAttributes(); |
| 54 | + } |
| 55 | + return null; |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + /** |
| 60 | + * @param class-string $className |
| 61 | + * @param string $methodName |
| 62 | + * @return void |
| 63 | + */ |
| 64 | + public function setCurrentClassMethodName(string $className, string $methodName): void |
| 65 | + { |
| 66 | + $this->currentClass = $className; |
| 67 | + $this->currentMethod = $methodName; |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | + public function unsetCurrentClassMethodName(): void |
| 72 | + { |
| 73 | + $this->currentClass = $this->currentMethod = null; |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + /** |
| 78 | + * @param string $functionName |
| 79 | + * @return void |
| 80 | + */ |
| 81 | + public function setCurrentFunctionName(string $functionName): void |
| 82 | + { |
| 83 | + $this->currentFunction = $functionName; |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + public function unsetCurrentFunctionName(): void |
| 88 | + { |
| 89 | + $this->currentFunction = null; |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments