-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PhpReflection::getReturnType() can handle PHP 7.1 nullable types
- Loading branch information
Showing
2 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,7 +83,7 @@ public static function getParameterType(\ReflectionParameter $param) | |
public static function getReturnType(\ReflectionFunctionAbstract $func) | ||
{ | ||
if (PHP_VERSION_ID >= 70000 && $func->hasReturnType()) { | ||
$type = (string) $func->getReturnType(); | ||
$type = PHP_VERSION_ID >= 70100 ? $func->getReturnType()->getName() : (string) $func->getReturnType(); | ||
return strtolower($type) === 'self' ? $func->getDeclaringClass()->getName() : $type; | ||
} | ||
$type = preg_replace('#[|\s].*#', '', (string) self::parseAnnotation($func, 'return')); | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Majkl578
Contributor
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
/** | ||
* Test: Nette\DI\PhpReflection::getReturnType | ||
* @phpversion 7.1 | ||
*/ | ||
|
||
namespace NS | ||
{ | ||
use Test\B; | ||
|
||
class A | ||
{ | ||
function noType() | ||
{} | ||
|
||
function classType(): B | ||
{} | ||
|
||
function nativeType(): string | ||
{} | ||
|
||
function selfType(): self | ||
{} | ||
|
||
function nullableClassType(): ?B | ||
{} | ||
|
||
function nullableNativeType(): ?string | ||
{} | ||
|
||
function nullableSelfType(): ?self | ||
{} | ||
|
||
/** @return B */ | ||
function annotationClassType() | ||
{} | ||
|
||
/** @return B|string */ | ||
function annotationUnionType() | ||
{} | ||
|
||
/** @return String */ | ||
function annotationNativeType() | ||
{} | ||
|
||
/** @return self */ | ||
function annotationSelfType() | ||
{} | ||
} | ||
|
||
/** @return B */ | ||
function annotationClassType() | ||
{} | ||
} | ||
|
||
|
||
namespace | ||
{ | ||
use Nette\DI\PhpReflection; | ||
use Tester\Assert; | ||
|
||
require __DIR__ . '/../bootstrap.php'; | ||
|
||
|
||
Assert::null(PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'noType'))); | ||
|
||
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'classType'))); | ||
|
||
Assert::same('string', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'nativeType'))); | ||
|
||
Assert::same('NS\A', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'selfType'))); | ||
|
||
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'nullableClassType'))); | ||
|
||
Assert::same('string', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'nullableNativeType'))); | ||
|
||
Assert::same('NS\A', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'nullableSelfType'))); | ||
|
||
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationClassType'))); | ||
|
||
Assert::same('Test\B', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationUnionType'))); | ||
|
||
Assert::same('string', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationNativeType'))); | ||
|
||
Assert::same('NS\A', PhpReflection::getReturnType(new \ReflectionMethod(NS\A::class, 'annotationSelfType'))); | ||
|
||
// class name expanding is NOT supported for global functions | ||
Assert::same('B', PhpReflection::getReturnType(new \ReflectionFunction('NS\annotationClassType'))); | ||
} |
We could change this to:
to allow
?
in annotations as well.