diff --git a/src/Type/Php/JsonDecodeDynamicReturnTypeExtension.php b/src/Type/Php/JsonDecodeDynamicReturnTypeExtension.php index 8ffbe62..33e18e6 100644 --- a/src/Type/Php/JsonDecodeDynamicReturnTypeExtension.php +++ b/src/Type/Php/JsonDecodeDynamicReturnTypeExtension.php @@ -18,13 +18,12 @@ */ final class JsonDecodeDynamicReturnTypeExtension implements DynamicFunctionReturnTypeExtension { - private FunctionReflection $nativeJsonDecodeReflection; + private ?FunctionReflection $nativeJsonDecodeReflection = null; public function __construct( private readonly JsonThrowOnErrorDynamicReturnTypeExtension $phpstanCheck, - ReflectionProvider $reflectionProvider, + private readonly ReflectionProvider $reflectionProvider, ) { - $this->nativeJsonDecodeReflection = $reflectionProvider->getFunction(new Name('json_decode'), null); } public function isFunctionSupported(FunctionReflection $functionReflection): bool @@ -34,6 +33,10 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type { + // trigger reflection processing lazily. + if ($this->nativeJsonDecodeReflection === null) { + $this->nativeJsonDecodeReflection = $this->reflectionProvider->getFunction(new Name('json_decode'), null); + } $result = $this->phpstanCheck->getTypeFromFunctionCall($this->nativeJsonDecodeReflection, $functionCall, $scope); // if PHPStan reports null and there is a json error, then an invalid constant string was passed