Skip to content

Commit 2076873

Browse files
committed
Remove old dependency from when the standard was in a plugin
Since we moved the standard from local_codechecker moodle plugin to be standalone repository, CFG won't be defined ever, so we don't need all the code making copies of some CFG settings and restoring them afterwards. Now we just set the minimum needed to run core_component methods and done. No need to reset anything.
1 parent e1f39f5 commit 2076873

File tree

1 file changed

+8
-25
lines changed

1 file changed

+8
-25
lines changed

moodle/Util/MoodleUtil.php

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ abstract class MoodleUtil {
6161
*/
6262
public static function setMockedComponentMappings(array $mappings): void {
6363
if (!defined('PHPUNIT_TEST') || !PHPUNIT_TEST) {
64-
throw new \Exception('Not running in a unit test');
64+
throw new \Exception('Not running in a unit test'); // @codeCoverageIgnore
6565
}
6666

6767
self::$mockedComponentMappings = $mappings;
@@ -85,32 +85,15 @@ protected static function loadCoreComponent(string $moodleRoot): bool {
8585
defined('IGNORE_COMPONENT_CACHE') ?: define('IGNORE_COMPONENT_CACHE', 1);
8686
defined('MOODLE_INTERNAL') ?: define('MOODLE_INTERNAL', 1);
8787

88-
if (!isset($CFG->dirroot)) { // No defined, let's start from scratch.
89-
$CFG = (object) [
90-
'dirroot' => $moodleRoot,
91-
'libdir' => "{$moodleRoot}/lib",
92-
'admin' => 'admin',
93-
];
94-
}
95-
96-
// Save current CFG values.
97-
$olddirroot = $CFG->dirroot ?? null;
98-
$oldlibdir = $CFG->libdir ?? null;
99-
$oldadmin = $CFG->admin ?? null;
100-
101-
if ($CFG->dirroot !== $moodleRoot) { // Different, set the minimum required.
102-
$CFG->dirroot = $moodleRoot;
103-
$CFG->libdir = $CFG->dirroot . '/lib';
104-
$CFG->admin = 'admin';
105-
}
88+
// Let's define CFG from scratch (it's not defined ever, because moodle-cs is not a Moodle plugin at all
89+
$CFG = (object) [
90+
'dirroot' => $moodleRoot,
91+
'libdir' => "{$moodleRoot}/lib",
92+
'admin' => 'admin',
93+
];
10694

10795
require_once($CFG->dirroot . '/lib/classes/component.php'); // Load the class.
10896

109-
// Restore original CFG values.
110-
$CFG->dirroot = $olddirroot ?? null;
111-
$CFG->libdir = $oldlibdir ?? null;
112-
$CFG->admin = $oldadmin ?? null;
113-
11497
return true;
11598
}
11699

@@ -225,7 +208,7 @@ protected static function calculateAllComponents(string $moodleRoot) {
225208
*/
226209
public static function getMoodleComponent(File $file, $selfPath = true) {
227210
if (defined('PHPUNIT_TEST') && PHPUNIT_TEST && !empty(self::$mockedComponentMappings)) {
228-
$components = self::$mockedComponentMappings;
211+
$components = self::$mockedComponentMappings; // @codeCoverageIgnore
229212
} else {
230213
// Verify that we are able to find a valid moodle root.
231214
if (!$moodleRoot = self::getMoodleRoot($file, $selfPath)) {

0 commit comments

Comments
 (0)