From 827f447548de3f73bf6dc8156b668d9bf94ed90f Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 16 Feb 2022 09:50:41 +0800 Subject: [PATCH] Apply sensible default values for $CFG if not specified In order to use the core_component functions, sensible defaults are required. These sensible defaults are available, but the check of `$CFG->dirroot` when $CFG is empty lead to a php warning, causing phpcs failures. Even if this error is fixed, the default values in $CFG are then set to NULL after including core_component, which means that uses of it work with null values and cause require errors. Fixes #184 --- moodle/Util/MoodleUtil.php | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/moodle/Util/MoodleUtil.php b/moodle/Util/MoodleUtil.php index 69c9092d..e46c7d01 100644 --- a/moodle/Util/MoodleUtil.php +++ b/moodle/Util/MoodleUtil.php @@ -67,15 +67,19 @@ protected static function loadCoreComponent(string $moodleRoot): bool { defined('IGNORE_COMPONENT_CACHE') ?: define('IGNORE_COMPONENT_CACHE', 1); defined('MOODLE_INTERNAL') ?: define('MOODLE_INTERNAL', 1); + if (!isset($CFG->dirroot)) { // No defined, let's start from scratch. + $CFG = (object) [ + 'dirroot' => $moodleRoot, + 'libdir' => "${moodleRoot}/lib", + 'admin' => 'admin', + ]; + } + // Save current CFG values. $olddirroot = $CFG->dirroot ?? null; $oldlibdir = $CFG->libdir ?? null; $oldadmin = $CFG->admin ?? null; - if (!isset($CFG->dirroot)) { // No defined, let's start from scratch. - $CFG = new \stdClass(); - } - if ($CFG->dirroot !== $moodleRoot) { // Different, set the minimum required. $CFG->dirroot = $moodleRoot; $CFG->libdir = $CFG->dirroot . '/lib';