Skip to content

Commit b3acd5f

Browse files
authored
fix(reflection): return null for method return type if not defined (#1645)
1 parent 061b576 commit b3acd5f

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

packages/reflection/src/MethodReflector.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,12 @@ public function invokeArgs(?object $object, array $args = []): mixed
5353
return $this->reflectionMethod->invokeArgs($object, $args);
5454
}
5555

56-
public function getReturnType(): TypeReflector
56+
public function getReturnType(): ?TypeReflector
5757
{
58+
if ($this->reflectionMethod->getReturnType() === null) {
59+
return null;
60+
}
61+
5862
return new TypeReflector($this->reflectionMethod->getReturnType());
5963
}
6064

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tempest\Reflection\Tests\Fixtures;
6+
7+
final class NoReturnType
8+
{
9+
// @mago-expect lint:return-type
10+
public function noReturnType()
11+
{
12+
return 2137;
13+
}
14+
}

packages/reflection/tests/MethodReflectorTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PHPUnit\Framework\TestCase;
66
use Tempest\Reflection\ClassReflector;
77
use Tempest\Reflection\ParameterReflector;
8+
use Tempest\Reflection\Tests\Fixtures\NoReturnType;
89
use Tempest\Reflection\Tests\Fixtures\TestClassA;
910

1011
final class MethodReflectorTest extends TestCase
@@ -24,4 +25,13 @@ public function test_get_parameter_by_name(): void
2425
->getParameter('unknown'),
2526
);
2627
}
28+
29+
public function test_no_return_type_returns_null(): void
30+
{
31+
$this->assertNull(
32+
new ClassReflector(NoReturnType::class)
33+
->getMethod('noReturnType')
34+
->getReturnType(),
35+
);
36+
}
2737
}

0 commit comments

Comments
 (0)