Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/Type/Php/JsonDecodeDynamicReturnTypeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down