Skip to content

Commit a39b29f

Browse files
committed
Add Reflection::package() utility method
1 parent 2dd7110 commit a39b29f

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/main/php/lang/Reflection.class.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use lang\meta\{MetaInformation, FromSyntaxTree, FromAttributes};
44
use lang\reflection\{Type, Package};
5-
use lang\{ClassLoader, ClassNotFoundException};
5+
use lang\{ClassLoader, ClassNotFoundException, IllegalArgumentException};
66

77
/**
88
* Factory for reflection instances.
@@ -56,6 +56,22 @@ public static function type($arg) {
5656
}
5757
}
5858

59+
/**
60+
* Returns a reflection package for a given argument.
61+
*
62+
* @param string
63+
* @return lang.reflection.Type
64+
* @throws lang.IllegalArgumentException
65+
*/
66+
public static function package($arg) {
67+
$cl= ClassLoader::getDefault();
68+
$name= strtr($arg, '\\', '.');
69+
if ($cl->providesPackage($name)) {
70+
return new Package($name);
71+
}
72+
throw new IllegalArgumentException('No package named '.$name);
73+
}
74+
5975
/**
6076
* Creates a new reflection instance, which may either refer to a type
6177
* or to a package.

src/test/php/lang/reflection/unittest/ReflectionTest.class.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public function of_package_name() {
3434
Assert::instance(Package::class, Reflection::of('lang.reflection.unittest'));
3535
}
3636

37+
#[Test]
38+
public function package() {
39+
Assert::instance(Package::class, Reflection::package('lang.reflection.unittest'));
40+
}
41+
3742
#[Test, Values([70000, 70100, 70200, 70300, 70400])]
3843
public function parser_for_php7($versionId) {
3944
Assert::instance(FromSyntaxTree::class, Reflection::annotations($versionId));

0 commit comments

Comments
 (0)