diff --git a/README.md b/README.md index 27ea0ea..a81f7b6 100755 --- a/README.md +++ b/README.md @@ -29,6 +29,7 @@ use org\example\{Base, Inject, Fixture}; $type= Reflection::type(Fixture::class); $type->name(); // org.example.Fixture +$type->declaredName(); // Fixture $type->literal(); // Fixture::class $type->modifiers(); // Modifiers $type->comment(); // (api doc comment) diff --git a/src/main/php/lang/reflection/Type.class.php b/src/main/php/lang/reflection/Type.class.php index 60f97af..876fd55 100755 --- a/src/main/php/lang/reflection/Type.class.php +++ b/src/main/php/lang/reflection/Type.class.php @@ -20,6 +20,9 @@ public function __construct($reflect) { /** Returns type name (in dotted form) */ public function name(): string { return strtr($this->reflect->name, '\\', '.'); } + /** Returns declared name (without namespace) */ + public function declaredName(): string { return $this->reflect->getShortName(); } + /** Returns type literal (in namespaced form) */ public function literal(): string { return $this->reflect->name; } diff --git a/src/test/php/lang/reflection/unittest/TypeTest.class.php b/src/test/php/lang/reflection/unittest/TypeTest.class.php index ee25e8b..896ba69 100755 --- a/src/test/php/lang/reflection/unittest/TypeTest.class.php +++ b/src/test/php/lang/reflection/unittest/TypeTest.class.php @@ -49,6 +49,12 @@ public function package() { Assert::equals(new Package(__NAMESPACE__), $this->fixture->package()); } + #[Test] + public function declaredName() { + $qualified= nameof($this).'Fixture'; + Assert::equals(substr($qualified, strrpos($qualified, '.') + 1), $this->fixture->declaredName()); + } + #[Test] public function global_namespace() { Assert::null(Reflection::of(\Throwable::class)->package());