Skip to content

Commit

Permalink
Reflection::getReturnType() & getParameterType() supports 'parent'
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 14, 2017
1 parent bba41ff commit 1319046
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
25 changes: 18 additions & 7 deletions src/Utils/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ public static function isBuiltinType(string $type): bool
*/
public static function getReturnType(\ReflectionFunctionAbstract $func)
{
if ($func->hasReturnType()) {
$type = (string) $func->getReturnType();
return strtolower($type) === 'self' ? $func->getDeclaringClass()->getName() : $type;
}
return $func->hasReturnType()
? self::normalizeType((string) $func->getReturnType(), $func)
: NULL;
}


Expand All @@ -48,9 +47,21 @@ public static function getReturnType(\ReflectionFunctionAbstract $func)
*/
public static function getParameterType(\ReflectionParameter $param)
{
if ($param->hasType()) {
$type = (string) $param->getType();
return strtolower($type) === 'self' ? $param->getDeclaringClass()->getName() : $type;
return $param->hasType()
? self::normalizeType((string) $param->getType(), $param)
: NULL;
}


private static function normalizeType(string $type, $reflection): string
{
$lower = strtolower($type);
if ($lower === 'self') {
return $reflection->getDeclaringClass()->getName();
} elseif ($lower === 'parent' && $reflection->getDeclaringClass()->getParentClass()) {
return $reflection->getDeclaringClass()->getParentClass()->getName();
} else {
return $type;
}
}

Expand Down
19 changes: 16 additions & 3 deletions tests/Utils/Reflection.getParameterType.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,28 @@ use Test\B; // for testing purposes

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

$method = new \ReflectionMethod('A', 'method');
class AExt extends A
{
function methodExt(parent $parent)
{}
}

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

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


$method = new ReflectionMethod('AExt', 'methodExt');
$params = $method->getParameters();

Assert::same('A', Reflection::getParameterType($params[0]));
13 changes: 13 additions & 0 deletions tests/Utils/Reflection.getReturnType.php7.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ namespace NS

function selfType(): self
{}

function parentType(): parent
{}
}

class AExt extends A
{
function parentTypeExt(): parent
{}
}
}

Expand All @@ -42,4 +51,8 @@ namespace
Assert::same('string', Reflection::getReturnType(new \ReflectionMethod(NS\A::class, 'nativeType')));

Assert::same('NS\A', Reflection::getReturnType(new \ReflectionMethod(NS\A::class, 'selfType')));

Assert::same('parent', Reflection::getReturnType(new \ReflectionMethod(NS\A::class, 'parentType')));

Assert::same('NS\A', Reflection::getReturnType(new \ReflectionMethod(NS\AExt::class, 'parentTypeExt')));
}

0 comments on commit 1319046

Please sign in to comment.