From 4eac518132b4aafd40668657b1cbd8e1ef0afdde Mon Sep 17 00:00:00 2001 From: Lctrs Date: Tue, 14 Jan 2020 15:22:42 +0100 Subject: [PATCH] Support getting reflection of an anonymous function From https://www.php.net/manual/en/class.closure.php : > Besides the methods listed here, this class also has an __invoke method. --- .../Php/ClosureInvokeMethodReflection.php | 106 ++++++++++++++++++ src/Type/ClosureType.php | 8 ++ 2 files changed, 114 insertions(+) create mode 100644 src/Reflection/Php/ClosureInvokeMethodReflection.php diff --git a/src/Reflection/Php/ClosureInvokeMethodReflection.php b/src/Reflection/Php/ClosureInvokeMethodReflection.php new file mode 100644 index 00000000000..1cb554b32be --- /dev/null +++ b/src/Reflection/Php/ClosureInvokeMethodReflection.php @@ -0,0 +1,106 @@ +nativeMethodReflection = $nativeMethodReflection; + $this->closureType = $closureType; + } + + public function getDeclaringClass(): ClassReflection + { + return $this->nativeMethodReflection->getDeclaringClass(); + } + + public function isStatic(): bool + { + return $this->nativeMethodReflection->isStatic(); + } + + public function isPrivate(): bool + { + return $this->nativeMethodReflection->isPrivate(); + } + + public function isPublic(): bool + { + return $this->nativeMethodReflection->isPublic(); + } + + public function getDocComment(): ?string + { + return $this->nativeMethodReflection->getDocComment(); + } + + public function getName(): string + { + return $this->nativeMethodReflection->getName(); + } + + public function getPrototype(): ClassMemberReflection + { + return $this->nativeMethodReflection->getPrototype(); + } + + public function getVariants(): array + { + return [ + new FunctionVariant( + $this->closureType->getTemplateTypeMap(), + $this->closureType->getResolvedTemplateTypeMap(), + $this->closureType->getParameters(), + $this->closureType->isVariadic(), + $this->closureType->getReturnType() + ), + ]; + } + + public function isDeprecated(): TrinaryLogic + { + return $this->nativeMethodReflection->isDeprecated(); + } + + public function getDeprecatedDescription(): ?string + { + return $this->nativeMethodReflection->getDeprecatedDescription(); + } + + public function isFinal(): TrinaryLogic + { + return $this->nativeMethodReflection->isFinal(); + } + + public function isInternal(): TrinaryLogic + { + return $this->nativeMethodReflection->isInternal(); + } + + public function getThrowType(): ?Type + { + return $this->nativeMethodReflection->getThrowType(); + } + + public function hasSideEffects(): TrinaryLogic + { + return $this->nativeMethodReflection->hasSideEffects(); + } + +} diff --git a/src/Type/ClosureType.php b/src/Type/ClosureType.php index e7d1235633a..d6fe9e10e5f 100644 --- a/src/Type/ClosureType.php +++ b/src/Type/ClosureType.php @@ -10,6 +10,7 @@ use PHPStan\Reflection\ParameterReflection; use PHPStan\Reflection\ParametersAcceptor; use PHPStan\Reflection\Php\ClosureCallMethodReflection; +use PHPStan\Reflection\Php\ClosureInvokeMethodReflection; use PHPStan\Reflection\PropertyReflection; use PHPStan\TrinaryLogic; use PHPStan\Type\Constant\ConstantArrayType; @@ -167,6 +168,13 @@ public function getMethod(string $methodName, ClassMemberAccessAnswerer $scope): ); } + if ($methodName === '__invoke') { + return new ClosureInvokeMethodReflection( + $this->objectType->getMethod($methodName, $scope), + $this + ); + } + return $this->objectType->getMethod($methodName, $scope); }