diff --git a/lib/classes/component.php b/lib/classes/component.php index bb85492c6f4..c0eba687b43 100644 --- a/lib/classes/component.php +++ b/lib/classes/component.php @@ -251,7 +251,6 @@ class core_component { return $file; } - /** * Initialise caches, always call before accessing self:: caches. */ @@ -371,6 +370,17 @@ class core_component { } } + /** + * Reset the initialisation of the component utility. + * + * Note: It should not be necessary to call this in regular code. + * Please only use it where strictly required. + */ + public static function reset(): void { + // The autoloader will re-initialise if plugintypes is null. + self::$plugintypes = null; + } + /** * Are we in developer debug mode? * diff --git a/lib/phpunit/classes/advanced_testcase.php b/lib/phpunit/classes/advanced_testcase.php index 048e71f024f..6c1669e426a 100644 --- a/lib/phpunit/classes/advanced_testcase.php +++ b/lib/phpunit/classes/advanced_testcase.php @@ -87,8 +87,9 @@ abstract class advanced_testcase extends base_testcase { // Reset global state after test and test failure. $CFG = phpunit_util::get_global_backup('CFG'); $DB = phpunit_util::get_global_backup('DB'); - // This is _hacky_. We need to reset the autoloader, and this is the only way to do so right now. - (new ReflectionProperty(\core_component::class, 'plugintypes'))->setValue(null, null); + + // We need to reset the autoloader. + \core_component::reset(); } if (isset($e)) { diff --git a/lib/tests/component_test.php b/lib/tests/component_test.php index f11168185e8..3836a2f9b32 100644 --- a/lib/tests/component_test.php +++ b/lib/tests/component_test.php @@ -48,9 +48,7 @@ final class component_test extends advanced_testcase { $psr4namespaces->setAccessible(true); $psr4namespaces->setValue(null, $this->oldpsr4namespaces); - $plugintypes = new ReflectionProperty(\core_component::class, 'plugintypes'); - $plugintypes->setAccessible(true); - $plugintypes->setValue(null, null); + \core_component::reset(); } public function test_get_core_subsystems() { @@ -915,7 +913,7 @@ final class component_test extends advanced_testcase { // We have to override the dirroot and libdir, and then reset the plugintypes property. $CFG->dirroot = $vfileroot->url(); $CFG->libdir = $vfileroot->url() . '/lib'; - (new ReflectionProperty('core_component', 'plugintypes'))->setValue(null, null); + \core_component::reset(); // Existing classes do not break. $this->assertTrue(