Skip to content

Commit 1319046

Browse files
committed
Reflection::getReturnType() & getParameterType() supports 'parent'
1 parent bba41ff commit 1319046

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

src/Utils/Reflection.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,9 @@ public static function isBuiltinType(string $type): bool
3636
*/
3737
public static function getReturnType(\ReflectionFunctionAbstract $func)
3838
{
39-
if ($func->hasReturnType()) {
40-
$type = (string) $func->getReturnType();
41-
return strtolower($type) === 'self' ? $func->getDeclaringClass()->getName() : $type;
42-
}
39+
return $func->hasReturnType()
40+
? self::normalizeType((string) $func->getReturnType(), $func)
41+
: NULL;
4342
}
4443

4544

@@ -48,9 +47,21 @@ public static function getReturnType(\ReflectionFunctionAbstract $func)
4847
*/
4948
public static function getParameterType(\ReflectionParameter $param)
5049
{
51-
if ($param->hasType()) {
52-
$type = (string) $param->getType();
53-
return strtolower($type) === 'self' ? $param->getDeclaringClass()->getName() : $type;
50+
return $param->hasType()
51+
? self::normalizeType((string) $param->getType(), $param)
52+
: NULL;
53+
}
54+
55+
56+
private static function normalizeType(string $type, $reflection): string
57+
{
58+
$lower = strtolower($type);
59+
if ($lower === 'self') {
60+
return $reflection->getDeclaringClass()->getName();
61+
} elseif ($lower === 'parent' && $reflection->getDeclaringClass()->getParentClass()) {
62+
return $reflection->getDeclaringClass()->getParentClass()->getName();
63+
} else {
64+
return $type;
5465
}
5566
}
5667

tests/Utils/Reflection.getParameterType.phpt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,28 @@ use Test\B; // for testing purposes
1717

1818
class A
1919
{
20-
function method(Undeclared $undeclared, B $b, array $array, callable $callable, $none)
20+
function method(Undeclared $undeclared, B $b, array $array, callable $callable, self $self, $none)
2121
{}
2222
}
2323

24-
$method = new \ReflectionMethod('A', 'method');
24+
class AExt extends A
25+
{
26+
function methodExt(parent $parent)
27+
{}
28+
}
29+
30+
$method = new ReflectionMethod('A', 'method');
2531
$params = $method->getParameters();
2632

2733
Assert::same('Undeclared', Reflection::getParameterType($params[0]));
2834
Assert::same('Test\B', Reflection::getParameterType($params[1]));
2935
Assert::same('array', Reflection::getParameterType($params[2]));
3036
Assert::same('callable', Reflection::getParameterType($params[3]));
31-
Assert::null(Reflection::getParameterType($params[4]));
37+
Assert::same('A', Reflection::getParameterType($params[4]));
38+
Assert::null(Reflection::getParameterType($params[5]));
39+
40+
41+
$method = new ReflectionMethod('AExt', 'methodExt');
42+
$params = $method->getParameters();
43+
44+
Assert::same('A', Reflection::getParameterType($params[0]));

tests/Utils/Reflection.getReturnType.php7.phpt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ namespace NS
2323

2424
function selfType(): self
2525
{}
26+
27+
function parentType(): parent
28+
{}
29+
}
30+
31+
class AExt extends A
32+
{
33+
function parentTypeExt(): parent
34+
{}
2635
}
2736
}
2837

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

4453
Assert::same('NS\A', Reflection::getReturnType(new \ReflectionMethod(NS\A::class, 'selfType')));
54+
55+
Assert::same('parent', Reflection::getReturnType(new \ReflectionMethod(NS\A::class, 'parentType')));
56+
57+
Assert::same('NS\A', Reflection::getReturnType(new \ReflectionMethod(NS\AExt::class, 'parentTypeExt')));
4558
}

0 commit comments

Comments
 (0)