Skip to content

Commit

Permalink
Fix dumping complex return types for magic methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed May 5, 2022
1 parent 691741d commit 8419f01
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/ProxyManager/Generator/MagicMethodGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,14 @@
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;

/**
Expand All @@ -32,13 +37,22 @@ public function __construct(ReflectionClass $originalClass, string $name, array
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 comments on commit 8419f01

Please sign in to comment.