Skip to content

Commit 8419f01

Browse files
Fix dumping complex return types for magic methods
1 parent 691741d commit 8419f01

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/ProxyManager/Generator/MagicMethodGenerator.php

+20-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,14 @@
55
namespace ProxyManager\Generator;
66

77
use Laminas\Code\Generator\ParameterGenerator;
8+
use LogicException;
89
use ReflectionClass;
10+
use ReflectionIntersectionType;
911
use ReflectionNamedType;
12+
use ReflectionUnionType;
1013

14+
use function get_class;
15+
use function implode;
1116
use function strtolower;
1217

1318
/**
@@ -32,13 +37,22 @@ public function __construct(ReflectionClass $originalClass, string $name, array
3237
return;
3338
}
3439

35-
$originalMethod = $originalClass->getMethod($name);
36-
$returnType = $originalMethod->getReturnType();
37-
38-
if ($returnType instanceof ReflectionNamedType) {
39-
$this->setReturnType(($returnType->allowsNull() ? '?' : '') . $returnType->getName());
40-
}
40+
$originalMethod = $originalClass->getMethod($name);
41+
$originalReturnType = $originalMethod->getReturnType();
4142

4243
$this->setReturnsReference($originalMethod->returnsReference());
44+
45+
if ($originalReturnType instanceof ReflectionNamedType) {
46+
$this->setReturnType(($originalReturnType->allowsNull() && $originalReturnType->getName() !== 'mixed' ? '?' : '') . $originalReturnType->getName());
47+
} elseif ($originalReturnType instanceof ReflectionUnionType || $originalReturnType instanceof ReflectionIntersectionType) {
48+
$returnType = [];
49+
foreach ($originalReturnType->getTypes() as $type) {
50+
$returnType[] = $type->getName();
51+
}
52+
53+
$this->setReturnType(implode($originalReturnType instanceof ReflectionIntersectionType ? '&' : '|', $returnType));
54+
} elseif ($originalReturnType) {
55+
throw new LogicException('Unexpected ' . get_class($type));
56+
}
4357
}
4458
}

0 commit comments

Comments
 (0)