Skip to content

Commit

Permalink
Reflection::expandClassName() recognizes 'parent'
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 11, 2021
1 parent 7c5b48d commit 26d6fe2
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Utils/Reflection.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ public static function expandClassName(string $name, \ReflectionClass $context):
} elseif ($lower === 'self' || $lower === 'static') {
return $context->name;

} elseif ($lower === 'parent') {
return $context->getParentClass()
? $context->getParentClass()->name
: 'parent';

} elseif ($name[0] === '\\') { // fully qualified name
return ltrim($name, '\\');
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Utils/Reflection.expandClassName.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ Assert::same('BTest', Reflection::expandClassName('BTest', $rcBTest));
Assert::same('Test\Space\Foo', Reflection::expandClassName('self', $rcFoo));
Assert::same('Test\Space\Foo', Reflection::expandClassName('Self', $rcFoo));

Assert::same('parent', Reflection::expandClassName('parent', $rcFoo));
Assert::same('Test\Space\Foo', Reflection::expandClassName('parent', new ReflectionClass(new class extends Test\Space\Foo {
})));

foreach (['String', 'string', 'int', 'float', 'bool', 'array', 'callable', 'iterable', 'void', 'null'] as $type) {
Assert::same(strtolower($type), Reflection::expandClassName($type, $rcFoo));
}
Expand Down

0 comments on commit 26d6fe2

Please sign in to comment.