Skip to content

Commit 7d1385a

Browse files
committed
PhpReflection::getReturnType() can handle PHP 7.1 nullable types
1 parent 9fcfe47 commit 7d1385a

File tree

2 files changed

+91
-1
lines changed

2 files changed

+91
-1
lines changed

src/DI/PhpReflection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static function getParameterType(\ReflectionParameter $param)
8484
public static function getReturnType(\ReflectionFunctionAbstract $func)
8585
{
8686
if (PHP_VERSION_ID >= 70000 && $func->hasReturnType()) {
87-
$type = (string) $func->getReturnType();
87+
$type = PHP_VERSION_ID >= 70100 ? $func->getReturnType()->getName() : (string) $func->getReturnType();
8888
return strtolower($type) === 'self' ? $func->getDeclaringClass()->getName() : $type;
8989
}
9090
$type = preg_replace('#[|\s].*#', '', (string) self::parseAnnotation($func, 'return'));
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
/**
4+
* Test: Nette\DI\PhpReflection::getReturnType
5+
* @phpversion 7.1
6+
*/
7+
8+
namespace NS
9+
{
10+
use Test\B;
11+
12+
class A
13+
{
14+
function noType()
15+
{}
16+
17+
function classType(): B
18+
{}
19+
20+
function nativeType(): string
21+
{}
22+
23+
function selfType(): self
24+
{}
25+
26+
function nullableClassType(): ?B
27+
{}
28+
29+
function nullableNativeType(): ?string
30+
{}
31+
32+
function nullableSelfType(): ?self
33+
{}
34+
35+
/** @return B */
36+
function annotationClassType()
37+
{}
38+
39+
/** @return B|string */
40+
function annotationUnionType()
41+
{}
42+
43+
/** @return String */
44+
function annotationNativeType()
45+
{}
46+
47+
/** @return self */
48+
function annotationSelfType()
49+
{}
50+
}
51+
52+
/** @return B */
53+
function annotationClassType()
54+
{}
55+
}
56+
57+
58+
namespace
59+
{
60+
use Nette\DI\PhpReflection;
61+
use Tester\Assert;
62+
63+
require __DIR__ . '/../bootstrap.php';
64+
65+
66+
Assert::null(PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'noType')));
67+
68+
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'classType')));
69+
70+
Assert::same('string', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'nativeType')));
71+
72+
Assert::same('NS\A', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'selfType')));
73+
74+
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'nullableClassType')));
75+
76+
Assert::same('string', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'nullableNativeType')));
77+
78+
Assert::same('NS\A', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'nullableSelfType')));
79+
80+
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationClassType')));
81+
82+
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationUnionType')));
83+
84+
Assert::same('string', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationNativeType')));
85+
86+
Assert::same('NS\A', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationSelfType')));
87+
88+
// class name expanding is NOT supported for global functions
89+
Assert::same('B', PhpReflection::getReturnType(new \ReflectionFunction('NS\annotationClassType')));
90+
}

0 commit comments

Comments
 (0)