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:
6
6
paths :
7
7
- src/
8
8
- fixtures/classes/
9
+ excludePaths :
10
+ - fixtures/classes/Loadable/InvalidChildClass.php
9
11
reportUnmatchedIgnoredErrors : false
10
12
level : 10
11
13
ignoreErrors :
Original file line number Diff line number Diff line change @@ -125,9 +125,11 @@ public function init_classes( $dir = '' ) {
125
125
continue ;
126
126
}
127
127
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
+ }
131
133
132
134
// Using reflection, check if the class can be initialized.
133
135
// If not, skip.
@@ -174,6 +176,26 @@ public function init_classes( $dir = '' ) {
174
176
}
175
177
}
176
178
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
+
177
199
/**
178
200
* Slugify a class name.
179
201
*
Original file line number Diff line number Diff line change @@ -134,4 +134,17 @@ public function test_only_classes_implementing_module_interface_are_initialized(
134
134
$ this ->assertTrue ( did_action ( 'tenup_framework_module_init__tenupframeworktestclasses-posttypes-demo ' ) > 0 , 'Demo was not initialized. ' );
135
135
$ this ->assertFalse ( did_action ( 'tenup_framework_module_init__tenupframeworktestclasses-standalone-standalone ' ) > 0 , 'Standalone class was initialized. ' );
136
136
}
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
+ }
137
150
}
You can’t perform that action at this time.
0 commit comments