Skip to content

Commit

Permalink
PhpReflection::getReturnType() can handle PHP 7.1 nullable types
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 21, 2016
1 parent 9fcfe47 commit 7d1385a
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/DI/PhpReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,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'));
Expand Down
90 changes: 90 additions & 0 deletions tests/DI/PhpReflection.getReturnType.php71.phpt
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')));
}

0 comments on commit 7d1385a

Please sign in to comment.