Skip to content

Commit

Permalink
PhpReflection::getParameterType() can handle PHP 7.1 nullable types (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMajor authored and dg committed Sep 21, 2016
1 parent 7960c8d commit 5f73db6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/DI/PhpReflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ public static function getDeclaringClass(\ReflectionProperty $prop)
*/
public static function getParameterType(\ReflectionParameter $param)
{
if (PHP_VERSION_ID >= 70000) {
if (PHP_VERSION_ID >= 70100) {
return $param->hasType() ? $param->getType()->getName() : NULL;
} elseif (PHP_VERSION_ID >= 70000) {
return $param->hasType() ? (string) $param->getType() : NULL;
} elseif ($param->isArray() || $param->isCallable()) {
return $param->isArray() ? 'array' : 'callable';
Expand Down
31 changes: 31 additions & 0 deletions tests/DI/PhpReflection.getParameterType.php71.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* Test: Nette\DI\PhpReflection::getParameterType
* @phpversion 7.1
*/

use Nette\DI\PhpReflection;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


use Test\B; // for testing purposes

class A
{
function method(Undeclared $undeclared, B $b, array $array, callable $callable, $none, ?B $nullable)
{}
}

$method = new \ReflectionMethod('A', 'method');
$params = $method->getParameters();

Assert::same('Undeclared', PhpReflection::getParameterType($params[0]));
Assert::same('Test\B', PhpReflection::getParameterType($params[1]));
Assert::same('array', PhpReflection::getParameterType($params[2]));
Assert::same('callable', PhpReflection::getParameterType($params[3]));
Assert::null(PhpReflection::getParameterType($params[4]));
Assert::same('Test\B', PhpReflection::getParameterType($params[5]));

0 comments on commit 5f73db6

Please sign in to comment.