WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
File tree Expand file tree Collapse file tree 6 files changed +97
-3
lines changed
fixtures/classes/Loadable Expand file tree Collapse file tree 6 files changed +97
-3
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * BaseClass
4+ *
5+ * @package TenUpPlugin\Loadable
6+ */
7+
8+ declare (strict_types = 1 );
9+
10+ namespace TenupFrameworkTestClasses \Loadable ;
11+
12+ /**
13+ * A base class.
14+ */
15+ class BaseClass {
16+
17+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * ChildClass
4+ *
5+ * @package TenUpPlugin\Loadable
6+ */
7+
8+ declare (strict_types = 1 );
9+
10+ namespace TenupFrameworkTestClasses \Loadable ;
11+
12+ use TenupFrameworkTestClasses \Loadable \BaseClass ;
13+
14+ /**
15+ * Represents a class that extends the functionality of BaseClass.
16+ */
17+ class ChildClass extends BaseClass {
18+
19+ }
Original file line number Diff line number Diff line change 1+ <?php
2+ /**
3+ * InvalidChildClass
4+ *
5+ * @package TenUpPlugin\Loadable
6+ */
7+
8+ declare (strict_types = 1 );
9+
10+ namespace TenupFrameworkTestClasses \Loadable ;
11+
12+ /**
13+ * Represents a class that attempts to extend a non-existent or invalid superclass.
14+ *
15+ * This class is invalid due to the parent class not being defined or available.
16+ * Ensure that the parent class exists and is properly imported or declared
17+ * in order to resolve this issue.
18+ */
19+ class InvalidChildClass extends NonExistentClass {
20+
21+ }
Original file line number Diff line number Diff line change @@ -6,6 +6,8 @@ parameters:
66 paths :
77 - src/
88 - fixtures/classes/
9+ excludePaths :
10+ - fixtures/classes/Loadable/InvalidChildClass.php
911 reportUnmatchedIgnoredErrors : false
1012 level : 10
1113 ignoreErrors :
Original file line number Diff line number Diff line change @@ -125,9 +125,11 @@ public function init_classes( $dir = '' ) {
125125 continue ;
126126 }
127127
128- // Create a new reflection of the class.
129- // @phpstan-ignore argument.type
130- $ reflection_class = new ReflectionClass ( $ class );
128+ $ reflection_class = $ this ->get_fully_loadable_class ( $ class );
129+
130+ if ( ! $ reflection_class ) {
131+ continue ;
132+ }
131133
132134 // Using reflection, check if the class can be initialized.
133135 // If not, skip.
@@ -174,6 +176,26 @@ public function init_classes( $dir = '' ) {
174176 }
175177 }
176178
179+ /**
180+ * Retrieves a fully loadable class using reflection.
181+ *
182+ * @param string $class_name The name of the class to load.
183+ *
184+ * @return false|ReflectionClass Returns a ReflectionClass instance if the class is loadable, or false if it is not.
185+ *
186+ * @phpstan-ignore missingType.generics
187+ */
188+ public function get_fully_loadable_class ( string $ class_name ): false |ReflectionClass {
189+ try {
190+ // Create a new reflection of the class.
191+ // @phpstan-ignore argument.type
192+ return new ReflectionClass ( $ class_name );
193+ } catch ( \Throwable $ e ) {
194+ // This includes ReflectionException, Error due to missing parent, etc.
195+ return false ;
196+ }
197+ }
198+
177199 /**
178200 * Slugify a class name.
179201 *
Original file line number Diff line number Diff line change @@ -134,4 +134,17 @@ public function test_only_classes_implementing_module_interface_are_initialized(
134134 $ this ->assertTrue ( did_action ( 'tenup_framework_module_init__tenupframeworktestclasses-posttypes-demo ' ) > 0 , 'Demo was not initialized. ' );
135135 $ this ->assertFalse ( did_action ( 'tenup_framework_module_init__tenupframeworktestclasses-standalone-standalone ' ) > 0 , 'Standalone class was initialized. ' );
136136 }
137+
138+ /**
139+ * Validate if the classes are fully loadable.
140+ *
141+ * @return void
142+ */
143+ public function testIsClassFullyLoadable () {
144+ $ module_init = \TenupFramework \ModuleInitialization::instance ();
145+
146+ $ this ->assertInstanceOf ( 'ReflectionClass ' , $ module_init ->get_fully_loadable_class ( '\TenupFrameworkTestClasses\Loadable\BaseClass ' ) );
147+ $ this ->assertInstanceOf ( 'ReflectionClass ' , $ module_init ->get_fully_loadable_class ( '\TenupFrameworkTestClasses\Loadable\ChildClass ' ) );
148+ $ this ->assertFalse ( $ module_init ->get_fully_loadable_class ( '\TenupFrameworkTestClasses\Loadable\InvalidChildClass ' ) );
149+ }
137150}
You can’t perform that action at this time.
0 commit comments