Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lang.reflection.Type::declaredName() #41

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading