File tree Expand file tree Collapse file tree 4 files changed +41
-0
lines changed
test/php/lang/reflection/unittest Expand file tree Collapse file tree 4 files changed +41
-0
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,10 @@ XP Reflection ChangeLog
33
44## ?.?.? / ????-??-??
55
6+ ## 3.5.0 / ????-??-??
7+
8+ * Merged PR #46 : Add ` Package::child() ` to return child packages - @thekid
9+
610## 3.4.1 / 2025-08-15
711
812* Fixed PHP 8.5 compatibility, see xp-framework/test #24 - @thekid
Original file line number Diff line number Diff line change @@ -187,6 +187,7 @@ $package->literal(); // 'org\example'
187187$package->type('Fixture'); // Type instance
188188$package->types(); // iterable with Type instances
189189$package->parent() // Package or NULL
190+ $package->child('impl') // Child package "org.example.impl" or NULL
190191$package->children(); // iterable with Package instances
191192$package->classLoaders(); // iterable with lang.ClassLoader instances
192193```
Original file line number Diff line number Diff line change @@ -54,6 +54,21 @@ public function parent() {
5454 return false === $ p ? new Package () : new Package (substr ($ this ->name , 0 , $ p ));
5555 }
5656
57+ /**
58+ * Returns a child package with a given name. Returns NULL if the child package
59+ * does not exist.
60+ *
61+ * @return ?self
62+ */
63+ public function child (string $ name ) {
64+ $ child = ($ this ->name ? $ this ->name .'. ' : '' ).strtr ($ name , '\\' , '. ' );
65+ if (ClassLoader::getDefault ()->providesPackage ($ child )) {
66+ return new self ($ child );
67+ } else {
68+ return null ;
69+ }
70+ }
71+
5772 /**
5873 * Returns this package's child packages
5974 *
Original file line number Diff line number Diff line change @@ -51,6 +51,27 @@ public function global_package_has_no_parent() {
5151 Assert::null ((new Package ())->parent ());
5252 }
5353
54+ #[Test]
55+ public function non_existant_child () {
56+ Assert::null ((new Package ())->child ('lang.non_existant_package ' ));
57+ }
58+
59+ #[Test, Values(['reflection.unittest ' , 'reflection \\unittest ' ])]
60+ public function resolve_child ($ reference ) {
61+ Assert::equals (
62+ new Package ('lang.reflection.unittest ' ),
63+ (new Package ('lang ' ))->child ($ reference )
64+ );
65+ }
66+
67+ #[Test, Values(['lang.reflection.unittest ' , 'lang \\reflection \\unittest ' ])]
68+ public function toplevel_child ($ reference ) {
69+ Assert::equals (
70+ new Package ('lang.reflection.unittest ' ),
71+ (new Package ())->child ($ reference )
72+ );
73+ }
74+
5475 #[Test]
5576 public function children () {
5677 Assert::equals (
You can’t perform that action at this time.
0 commit comments