Skip to content

Commit

Permalink
Add lang.reflection.Type::declaredName()
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Mar 28, 2024
1 parent 802fbfd commit 37b662a
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<public>
$type->comment(); // (api doc comment)
Expand Down
3 changes: 3 additions & 0 deletions src/main/php/lang/reflection/Type.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
6 changes: 6 additions & 0 deletions src/test/php/lang/reflection/unittest/TypeTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 37b662a

Please sign in to comment.