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 1884774 commit 5adb374
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 @@ -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.

Copy link
@MartinMajor

MartinMajor Sep 21, 2016

Contributor

We could change this to:

        $type = ltrim(preg_replace('#[|\s].*#', '', (string) self::parseAnnotation($func, 'return')), '?');

to allow ? in annotations as well.

This comment has been minimized.

Copy link
@dg

dg Sep 21, 2016

Author Member

IMHO annotations are dead

This comment has been minimized.

Copy link
@MartinMajor

MartinMajor Sep 21, 2016

Contributor

Ok, you are right that with PHP 7.1 there is no need for them.

This comment has been minimized.

Copy link
@dg

dg Sep 21, 2016

Author Member

But they are still and will be used.

This comment has been minimized.

Copy link
@Majkl578

Majkl578 Sep 21, 2016

Contributor

IMHO annotations are dead

Not quite yet until generics, now you can use them to hint IDE what the return collection type is.

@var Foo[]
@var Collection|Foo[]

This comment has been minimized.

Copy link
@dg

dg Sep 21, 2016

Author Member

From the viewpoint of nette/di are dead.

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 5adb374

Please sign in to comment.