From 88dda72c48633326c81a01b861c8b6a923948f56 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sun, 1 May 2022 01:12:17 -0700 Subject: [PATCH] Set return type as nullable when parent is --- src/ProxyManager/Generator/MagicMethodGenerator.php | 2 +- .../NullObject/MethodGenerator/NullObjectMethodInterceptor.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ProxyManager/Generator/MagicMethodGenerator.php b/src/ProxyManager/Generator/MagicMethodGenerator.php index ba2b94ce..8685b557 100644 --- a/src/ProxyManager/Generator/MagicMethodGenerator.php +++ b/src/ProxyManager/Generator/MagicMethodGenerator.php @@ -36,7 +36,7 @@ public function __construct(ReflectionClass $originalClass, string $name, array $returnType = $originalMethod->getReturnType(); if ($returnType instanceof ReflectionNamedType) { - $this->setReturnType($returnType->getName()); + $this->setReturnType(($returnType->allowsNull() ? '?' : '') . $returnType->getName()); } $this->setReturnsReference($originalMethod->returnsReference()); diff --git a/src/ProxyManager/ProxyGenerator/NullObject/MethodGenerator/NullObjectMethodInterceptor.php b/src/ProxyManager/ProxyGenerator/NullObject/MethodGenerator/NullObjectMethodInterceptor.php index f3417ec2..7c15264f 100644 --- a/src/ProxyManager/ProxyGenerator/NullObject/MethodGenerator/NullObjectMethodInterceptor.php +++ b/src/ProxyManager/ProxyGenerator/NullObject/MethodGenerator/NullObjectMethodInterceptor.php @@ -24,7 +24,7 @@ public static function generateMethod(MethodReflection $originalMethod): self $method = static::fromReflectionWithoutBodyAndDocBlock($originalMethod); $returnType = $originalMethod->getReturnType(); - $nullCast = $returnType instanceof ReflectionNamedType && in_array($returnType->getName(), ['array', 'float', 'int', 'string'], true) ? '(' . $returnType->getName() . ') ' : ''; + $nullCast = $returnType instanceof ReflectionNamedType && ! $returnType->allowsNull() && in_array($returnType->getName(), ['array', 'float', 'int', 'string'], true) ? '(' . $returnType->getName() . ') ' : ''; if ($originalMethod->returnsReference() || $nullCast !== '') { $reference = IdentifierSuffixer::getIdentifier('ref');