Skip to content
Snippets Groups Projects
Commit 8419f015 authored by Nicolas Grekas's avatar Nicolas Grekas
Browse files

Fix dumping complex return types for magic methods

parent 691741d8
No related branches found
No related tags found
No related merge requests found
......@@ -5,9 +5,14 @@ declare(strict_types=1);
namespace ProxyManager\Generator;
use Laminas\Code\Generator\ParameterGenerator;
use LogicException;
use ReflectionClass;
use ReflectionIntersectionType;
use ReflectionNamedType;
use ReflectionUnionType;
use function get_class;
use function implode;
use function strtolower;
/**
......@@ -32,13 +37,22 @@ class MagicMethodGenerator extends MethodGenerator
return;
}
$originalMethod = $originalClass->getMethod($name);
$returnType = $originalMethod->getReturnType();
if ($returnType instanceof ReflectionNamedType) {
$this->setReturnType(($returnType->allowsNull() ? '?' : '') . $returnType->getName());
}
$originalMethod = $originalClass->getMethod($name);
$originalReturnType = $originalMethod->getReturnType();
$this->setReturnsReference($originalMethod->returnsReference());
if ($originalReturnType instanceof ReflectionNamedType) {
$this->setReturnType(($originalReturnType->allowsNull() && $originalReturnType->getName() !== 'mixed' ? '?' : '') . $originalReturnType->getName());
} elseif ($originalReturnType instanceof ReflectionUnionType || $originalReturnType instanceof ReflectionIntersectionType) {
$returnType = [];
foreach ($originalReturnType->getTypes() as $type) {
$returnType[] = $type->getName();
}
$this->setReturnType(implode($originalReturnType instanceof ReflectionIntersectionType ? '&' : '|', $returnType));
} elseif ($originalReturnType) {
throw new LogicException('Unexpected ' . get_class($type));
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment