From aaba625cb82802d3aed0362cfabab28c5da8e1f0 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 21 Nov 2025 10:34:19 +0100 Subject: [PATCH 1/2] trigger reflection processing lazily --- src/Type/Php/JsonDecodeDynamicReturnTypeExtension.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Type/Php/JsonDecodeDynamicReturnTypeExtension.php b/src/Type/Php/JsonDecodeDynamicReturnTypeExtension.php index 8ffbe62..43575c0 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 From 5534f8c6c002568c56cbd29f7ec9aef46981e8a0 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Fri, 21 Nov 2025 10:41:27 +0100 Subject: [PATCH 2/2] Update JsonDecodeDynamicReturnTypeExtension.php Co-Authored-By: Kay W. <29700073+kayw-geek@users.noreply.github.com> --- src/Type/Php/JsonDecodeDynamicReturnTypeExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Type/Php/JsonDecodeDynamicReturnTypeExtension.php b/src/Type/Php/JsonDecodeDynamicReturnTypeExtension.php index 43575c0..33e18e6 100644 --- a/src/Type/Php/JsonDecodeDynamicReturnTypeExtension.php +++ b/src/Type/Php/JsonDecodeDynamicReturnTypeExtension.php @@ -33,7 +33,7 @@ public function isFunctionSupported(FunctionReflection $functionReflection): boo public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type { - // trigger reflection processing lazily + // trigger reflection processing lazily. if ($this->nativeJsonDecodeReflection === null) { $this->nativeJsonDecodeReflection = $this->reflectionProvider->getFunction(new Name('json_decode'), null); }