Skip to content

Commit 3d3c71f

Browse files
Honor existing return types when generating decorators
1 parent 581ab35 commit 3d3c71f

File tree

4 files changed

+19
-5
lines changed

4 files changed

+19
-5
lines changed

src/ProxyManager/Generator/MagicMethodGenerator.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Laminas\Code\Generator\ParameterGenerator;
88
use ReflectionClass;
9+
use ReflectionNamedType;
910

1011
use function strtolower;
1112

@@ -31,6 +32,13 @@ public function __construct(ReflectionClass $originalClass, string $name, array
3132
return;
3233
}
3334

34-
$this->setReturnsReference($originalClass->getMethod($name)->returnsReference());
35+
$originalMethod = $originalClass->getMethod($name);
36+
$returnType = $originalMethod->getReturnType();
37+
38+
if ($returnType instanceof ReflectionNamedType) {
39+
$this->setReturnType($returnType->getName());
40+
}
41+
42+
$this->setReturnsReference($originalMethod->returnsReference());
3543
}
3644
}

src/ProxyManager/ProxyGenerator/NullObject/MethodGenerator/NullObjectMethodInterceptor.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Laminas\Code\Reflection\MethodReflection;
88
use ProxyManager\Generator\MethodGenerator;
99
use ProxyManager\Generator\Util\IdentifierSuffixer;
10+
use ReflectionNamedType;
11+
12+
use function in_array;
1013

1114
/**
1215
* Method decorator for null objects
@@ -20,10 +23,13 @@ public static function generateMethod(MethodReflection $originalMethod): self
2023
{
2124
$method = static::fromReflectionWithoutBodyAndDocBlock($originalMethod);
2225

23-
if ($originalMethod->returnsReference()) {
26+
$returnType = $originalMethod->getReturnType();
27+
$nullCast = $returnType instanceof ReflectionNamedType && in_array($returnType->getName(), ['array', 'float', 'int', 'string'], true) ? '(' . $returnType->getName() . ') ' : '';
28+
29+
if ($originalMethod->returnsReference() || $nullCast !== '') {
2430
$reference = IdentifierSuffixer::getIdentifier('ref');
2531

26-
$method->setBody("\$reference = null;\nreturn \$" . $reference . ';');
32+
$method->setBody('$' . $reference . ' = ' . $nullCast . "null;\nreturn \$" . $reference . ';');
2733
}
2834

2935
return $method;

tests/ProxyManagerTest/ProxyGenerator/NullObjectGeneratorTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public function testGeneratesValidCode(string $className): void
9494
$callback = [$proxy, $method->getName()];
9595

9696
self::assertIsCallable($callback);
97-
self::assertNull($callback());
97+
self::assertEmpty($callback());
9898
}
9999
}
100100

tests/ProxyManagerTestAsset/ClassWithMagicMethods.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function __unset($name)
3232
return (bool) $name;
3333
}
3434

35-
public function __sleep()
35+
public function __sleep(): array
3636
{
3737
return [];
3838
}

0 commit comments

Comments
 (0)