From 4998ea2b0f4bdda76c45d245334d990f15d1c060 Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Tue, 6 May 2025 11:37:41 +0100 Subject: [PATCH 1/3] Refactor class initialization with error handling Move class reflection logic to a new `get_fully_loadable_class` method to improve clarity and reusability. Added error handling to safely handle non-loadable classes, ensuring smoother execution and fewer runtime errors. --- src/ModuleInitialization.php | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/ModuleInitialization.php b/src/ModuleInitialization.php index 02d2776..092ea68 100644 --- a/src/ModuleInitialization.php +++ b/src/ModuleInitialization.php @@ -125,9 +125,11 @@ public function init_classes( $dir = '' ) { continue; } - // Create a new reflection of the class. - // @phpstan-ignore argument.type - $reflection_class = new ReflectionClass( $class ); + $reflection_class = $this->get_fully_loadable_class( $class ); + + if ( ! $reflection_class ) { + continue; + } // Using reflection, check if the class can be initialized. // If not, skip. @@ -174,6 +176,26 @@ public function init_classes( $dir = '' ) { } } + /** + * Retrieves a fully loadable class using reflection. + * + * @param string $class_name The name of the class to load. + * + * @return false|ReflectionClass Returns a ReflectionClass instance if the class is loadable, or false if it is not. + * + * @phpstan-ignore missingType.generics + */ + public function get_fully_loadable_class( string $class_name ): false|ReflectionClass { + try { + // Create a new reflection of the class. + // @phpstan-ignore argument.type + return new ReflectionClass( $class_name ); + } catch ( \Throwable $e ) { + // This includes ReflectionException, Error due to missing parent, etc. + return false; + } + } + /** * Slugify a class name. * From a772af7bb7ccdc9eb1a97aea04574047c656c5c5 Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Tue, 6 May 2025 11:38:23 +0100 Subject: [PATCH 2/3] Add loadable classes and validation test Introduce `BaseClass`, `ChildClass`, and `InvalidChildClass` to the fixtures. Added a test method to validate if these classes are fully loadable, ensuring the module handles valid and invalid class definitions appropriately. --- fixtures/classes/Loadable/BaseClass.php | 17 +++++++++++++++ fixtures/classes/Loadable/ChildClass.php | 19 +++++++++++++++++ .../classes/Loadable/InvalidChildClass.php | 21 +++++++++++++++++++ tests/ModuleInitializationTest.php | 13 ++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 fixtures/classes/Loadable/BaseClass.php create mode 100644 fixtures/classes/Loadable/ChildClass.php create mode 100644 fixtures/classes/Loadable/InvalidChildClass.php diff --git a/fixtures/classes/Loadable/BaseClass.php b/fixtures/classes/Loadable/BaseClass.php new file mode 100644 index 0000000..e8eb346 --- /dev/null +++ b/fixtures/classes/Loadable/BaseClass.php @@ -0,0 +1,17 @@ +assertTrue( did_action( 'tenup_framework_module_init__tenupframeworktestclasses-posttypes-demo' ) > 0, 'Demo was not initialized.' ); $this->assertFalse( did_action( 'tenup_framework_module_init__tenupframeworktestclasses-standalone-standalone' ) > 0, 'Standalone class was initialized.' ); } + + /** + * Validate if the classes are fully loadable. + * + * @return void + */ + public function testIsClassFullyLoadable() { + $module_init = \TenupFramework\ModuleInitialization::instance(); + + $this->assertInstanceOf( 'ReflectionClass', $module_init->get_fully_loadable_class( '\TenupFrameworkTestClasses\Loadable\BaseClass' ) ); + $this->assertInstanceOf( 'ReflectionClass', $module_init->get_fully_loadable_class( '\TenupFrameworkTestClasses\Loadable\ChildClass' ) ); + $this->assertFalse( $module_init->get_fully_loadable_class( '\TenupFrameworkTestClasses\Loadable\InvalidChildClass' ) ); + } } From fa72f29d8366d3b139ad6ee625f949c7b8c61cf7 Mon Sep 17 00:00:00 2001 From: Daryll Doyle Date: Tue, 6 May 2025 11:39:22 +0100 Subject: [PATCH 3/3] Adjust PHPStan config to exclude specific invalid test class Excluded `InvalidChildClass.php` from analysis to avoid unnecessary errors in test fixtures. This ensures PHPStan runs more cleanly and focuses on relevant code paths. --- phpstan.neon | 2 ++ 1 file changed, 2 insertions(+) diff --git a/phpstan.neon b/phpstan.neon index 1e346c3..4d9795d 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -6,6 +6,8 @@ parameters: paths: - src/ - fixtures/classes/ + excludePaths: + - fixtures/classes/Loadable/InvalidChildClass.php reportUnmatchedIgnoredErrors: false level: 10 ignoreErrors: