diff --git a/lib/classes/exception/coding_exception.php b/lib/classes/exception/coding_exception.php index 94936bea7fa..677620df799 100644 --- a/lib/classes/exception/coding_exception.php +++ b/lib/classes/exception/coding_exception.php @@ -25,11 +25,12 @@ */ class coding_exception extends moodle_exception { /** - * Constructor + * Constructor. + * * @param string $hint short description of problem * @param string $debuginfo detailed information how to fix problem */ - function __construct($hint, $debuginfo=null) { + public function __construct($hint, $debuginfo = null) { parent::__construct('codingerror', 'debug', '', $hint, $debuginfo); } } diff --git a/lib/classes/exception/file_serving_exception.php b/lib/classes/exception/file_serving_exception.php index f60c21d0e77..adccfa1a340 100644 --- a/lib/classes/exception/file_serving_exception.php +++ b/lib/classes/exception/file_serving_exception.php @@ -25,10 +25,11 @@ */ class file_serving_exception extends moodle_exception { /** - * Constructor + * Constructor. + * * @param string $debuginfo optional more detailed information */ - function __construct($debuginfo = NULL) { - parent::__construct('cannotservefile', 'error', '', NULL, $debuginfo); + public function __construct($debuginfo = null) { + parent::__construct('cannotservefile', 'error', '', null, $debuginfo); } } diff --git a/lib/classes/exception/invalid_dataroot_permissions.php b/lib/classes/exception/invalid_dataroot_permissions.php index cb7feeaa079..2f39de58fee 100644 --- a/lib/classes/exception/invalid_dataroot_permissions.php +++ b/lib/classes/exception/invalid_dataroot_permissions.php @@ -25,10 +25,11 @@ */ class invalid_dataroot_permissions extends moodle_exception { /** - * Constructor + * Constructor. + * * @param string $debuginfo optional more detailed information */ - function __construct($debuginfo = NULL) { - parent::__construct('invaliddatarootpermissions', 'error', '', NULL, $debuginfo); + public function __construct($debuginfo = null) { + parent::__construct('invaliddatarootpermissions', 'error', '', null, $debuginfo); } } diff --git a/lib/classes/exception/invalid_parameter_exception.php b/lib/classes/exception/invalid_parameter_exception.php index a8b3d58608f..5aa8a4f1a05 100644 --- a/lib/classes/exception/invalid_parameter_exception.php +++ b/lib/classes/exception/invalid_parameter_exception.php @@ -27,10 +27,11 @@ */ class invalid_parameter_exception extends moodle_exception { /** - * Constructor + * Constructor. + * * @param string $debuginfo some detailed information */ - function __construct($debuginfo=null) { + public function __construct($debuginfo = null) { parent::__construct('invalidparameter', 'debug', '', null, $debuginfo); } } diff --git a/lib/classes/exception/invalid_response_exception.php b/lib/classes/exception/invalid_response_exception.php index 22317ee6a7e..03082713e3d 100644 --- a/lib/classes/exception/invalid_response_exception.php +++ b/lib/classes/exception/invalid_response_exception.php @@ -19,13 +19,19 @@ * This exception is not supposed to be thrown when processing * user submitted data in forms. It is more suitable * for WS and other low level stuff. + * + * @package core + * @subpackage lib + * @copyright Jerome Mounerac + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class invalid_response_exception extends moodle_exception { /** - * Constructor + * Constructor. + * * @param string $debuginfo some detailed information */ - function __construct($debuginfo=null) { + public function __construct($debuginfo = null) { parent::__construct('invalidresponse', 'debug', '', null, $debuginfo); } } diff --git a/lib/classes/exception/invalid_state_exception.php b/lib/classes/exception/invalid_state_exception.php index 09aa18a5625..47ee19a5aff 100644 --- a/lib/classes/exception/invalid_state_exception.php +++ b/lib/classes/exception/invalid_state_exception.php @@ -28,11 +28,12 @@ */ class invalid_state_exception extends moodle_exception { /** - * Constructor + * Constructor. + * * @param string $hint short description of problem * @param string $debuginfo optional more detailed information */ - function __construct($hint, $debuginfo=null) { + public function __construct($hint, $debuginfo = null) { parent::__construct('invalidstatedetected', 'debug', '', $hint, $debuginfo); } } diff --git a/lib/classes/exception/moodle_exception.php b/lib/classes/exception/moodle_exception.php index 10c67d661f7..cff748790bc 100644 --- a/lib/classes/exception/moodle_exception.php +++ b/lib/classes/exception/moodle_exception.php @@ -26,41 +26,35 @@ * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class moodle_exception extends \Exception { - - /** - * @var string The name of the string from error.php to print - */ + /** @var string The name of the string from error.php to print */ public $errorcode; - /** - * @var string The name of module - */ + /** @var string The name of module */ public $module; - /** - * @var mixed Extra words and phrases that might be required in the error string - */ + /** @var mixed Extra words and phrases that might be required in the error string */ public $a; /** - * @var string The url where the user will be prompted to continue. If no url is provided the user will be directed to the site index page. + * The url where the user will be prompted to continue. If no url is provided the user will be directed to the site index page. + * + * @var string */ public $link; - /** - * @var string Optional information to aid the debugging process - */ + /** @var string Optional information to aid the debugging process */ public $debuginfo; /** * Constructor * @param string $errorcode The name of the string from error.php to print * @param string $module name of module - * @param string $link The url where the user will be prompted to continue. If no url is provided the user will be directed to the site index page. + * @param string $link The url where the user will be prompted to continue. + * If no url is provided the user will be directed to the site index page. * @param mixed $a Extra words and phrases that might be required in the error string * @param string $debuginfo optional debugging information */ - function __construct($errorcode, $module='', $link='', $a=NULL, $debuginfo=null) { + public function __construct($errorcode, $module = '', $link = '', $a = null, $debuginfo = null) { global $CFG; if (empty($module) || $module == 'moodle' || $module == 'core') { @@ -95,11 +89,11 @@ class moodle_exception extends \Exception { } } - if (!$haserrorstring and $isinphpunittest) { + if (!$haserrorstring && $isinphpunittest) { // Append the contents of $a to $debuginfo so helpful information isn't lost. // This emulates what {@link get_exception_info()} does. Unfortunately that // function is not used by phpunit. - $message .= PHP_EOL.'$a contents: '.print_r($a, true); + $message .= PHP_EOL . '$a contents: ' . print_r($a, true); // phpcs:ignore moodle.PHP.ForbiddenFunctions.Found } parent::__construct($message, 0); diff --git a/lib/classes/exception/require_login_exception.php b/lib/classes/exception/require_login_exception.php index b66dd09b093..5eadb688e2f 100644 --- a/lib/classes/exception/require_login_exception.php +++ b/lib/classes/exception/require_login_exception.php @@ -19,16 +19,17 @@ * * This exception is thrown from require_login() * - * @package core_access + * @package core * @copyright 2010 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class require_login_exception extends moodle_exception { /** - * Constructor + * Constructor. + * * @param string $debuginfo Information to aid the debugging process */ - function __construct($debuginfo) { - parent::__construct('requireloginerror', 'error', '', NULL, $debuginfo); + public function __construct($debuginfo) { + parent::__construct('requireloginerror', 'error', '', null, $debuginfo); } } diff --git a/lib/classes/exception/require_login_session_timeout_exception.php b/lib/classes/exception/require_login_session_timeout_exception.php index b8519c4a2ac..56c70509a61 100644 --- a/lib/classes/exception/require_login_session_timeout_exception.php +++ b/lib/classes/exception/require_login_session_timeout_exception.php @@ -19,7 +19,7 @@ * * This exception is thrown from require_login() * - * @package core_access + * @package core * @copyright 2015 Andrew Nicols * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ diff --git a/lib/classes/exception/required_capability_exception.php b/lib/classes/exception/required_capability_exception.php index 9ea1827c3dd..da8a9062d68 100644 --- a/lib/classes/exception/required_capability_exception.php +++ b/lib/classes/exception/required_capability_exception.php @@ -18,22 +18,24 @@ * Exceptions indicating user does not have permissions to do something * and the execution can not continue. * - * @package core_access + * @package core * @copyright 2009 Petr Skoda {@link http://skodak.org} * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class required_capability_exception extends moodle_exception { /** - * Constructor + * Constructor. + * * @param context $context The context used for the capability check * @param string $capability The required capability * @param string $errormessage The error message to show the user * @param string $stringfile */ - function __construct($context, $capability, $errormessage, $stringfile) { + public function __construct($context, $capability, $errormessage, $stringfile) { $capabilityname = get_capability_string($capability); - if ($context->contextlevel == CONTEXT_MODULE and preg_match('/:view$/', $capability)) { - // we can not go to mod/xx/view.php because we most probably do not have cap to view it, let's go to course instead + if ($context->contextlevel == CONTEXT_MODULE && preg_match('/:view$/', $capability)) { + // Cannot redirect to mod/xx/view.php because we most probably do not have cap to view it. + // Redirect to the course instead. $parentcontext = $context->get_parent_context(); $link = $parentcontext->get_url(); } else { diff --git a/lib/classes/exception/webservice_parameter_exception.php b/lib/classes/exception/webservice_parameter_exception.php index ef6917cc4a2..a2b5165ea8c 100644 --- a/lib/classes/exception/webservice_parameter_exception.php +++ b/lib/classes/exception/webservice_parameter_exception.php @@ -21,15 +21,19 @@ * @deprecated since Moodle 2.2 - use moodle exception instead * This exception must be thrown to the web service client when a web service parameter is invalid * The error string is gotten from webservice.php + * @package core + * @copyright Jerome Mouneray + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ class webservice_parameter_exception extends moodle_exception { /** - * Constructor + * Constructor. + * * @param string $errorcode The name of the string from webservice.php to print * @param string $a The name of the parameter * @param string $debuginfo Optional information to aid debugging */ - function __construct($errorcode=null, $a = '', $debuginfo = null) { + public function __construct($errorcode = null, $a = '', $debuginfo = null) { parent::__construct($errorcode, 'webservice', '', $a, $debuginfo); } } diff --git a/lib/classes/output/bootstrap_renderer.php b/lib/classes/output/bootstrap_renderer.php index 6bc8e1893e2..8ad880d588f 100644 --- a/lib/classes/output/bootstrap_renderer.php +++ b/lib/classes/output/bootstrap_renderer.php @@ -55,13 +55,15 @@ class bootstrap_renderer { /** * Handles re-entrancy. Without this, errors or debugging output that occur * during the initialisation of $OUTPUT, cause infinite recursion. - * @var boolean + * + * @var bool */ protected $initialising = false; /** - * Have we started output yet? - * @return boolean true if the header has been printed. + * Whether output has started yet. + * + * @return bool true if the header has been printed. */ public function has_started() { return false; @@ -69,42 +71,45 @@ class bootstrap_renderer { /** * Constructor - to be used by core code only. + * * @param string $method The method to call * @param array $arguments Arguments to pass to the method being called * @return string */ public function __call($method, $arguments) { + // phpcs:disable moodle.PHP.ForbiddenGlobalUse.BadGlobal global $OUTPUT, $PAGE; $recursing = false; if ($method == 'notification') { // Catch infinite recursion caused by debugging output during print_header. + // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection $backtrace = debug_backtrace(); array_shift($backtrace); array_shift($backtrace); $recursing = is_early_init($backtrace); } - $earlymethods = array( + $earlymethods = [ 'fatal_error' => 'early_error', 'notification' => 'early_notification', - ); + ]; // If lib/outputlib.php has been loaded, call it. if (!empty($PAGE) && !$recursing) { if (array_key_exists($method, $earlymethods)) { - //prevent PAGE->context warnings - exceptions might appear before we set any context + // Prevent PAGE->context warnings - exceptions might appear before we set any context. $PAGE->set_context(null); } $PAGE->initialise_theme_and_output(); - return call_user_func_array(array($OUTPUT, $method), $arguments); + return call_user_func_array([$OUTPUT, $method], $arguments); } $this->initialising = true; // Too soon to initialise $OUTPUT, provide a couple of key methods. if (array_key_exists($method, $earlymethods)) { - return call_user_func_array(array('bootstrap_renderer', $earlymethods[$method]), $arguments); + return call_user_func_array(['bootstrap_renderer', $earlymethods[$method]], $arguments); } throw new coding_exception('Attempt to start output before enough information is known to initialise the theme.'); @@ -112,7 +117,7 @@ class bootstrap_renderer { /** * Returns nicely formatted error message in a div box. - * @static + * * @param string $message error message * @param ?string $moreinfourl (ignored in early errors) * @param ?string $link (ignored in early errors) @@ -137,10 +142,13 @@ class bootstrap_renderer { } else { // Because weblib is not available for these early errors, we // just duplicate s() code here to be safe. - $debuginfo = preg_replace('/&#(\d+|x[0-9a-f]+);/i', '&#$1;', - htmlspecialchars($debuginfo, ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE)); + $debuginfo = preg_replace( + '/&#(\d+|x[0-9a-f]+);/i', + '&#$1;', + htmlspecialchars($debuginfo, ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE) + ); } - $debuginfo = str_replace("\n", '
', $debuginfo); // keep newlines + $debuginfo = str_replace("\n", '
', $debuginfo); // Keep newlines. $content .= '
Debug info: ' . $debuginfo . '
'; } if (!empty($backtrace)) { @@ -153,7 +161,7 @@ class bootstrap_renderer { /** * This function should only be called by this class, or from exception handlers - * @static + * * @param string $message error message * @param string $moreinfourl (ignored in early errors) * @param string $link (ignored in early errors) @@ -166,7 +174,7 @@ class bootstrap_renderer { if (CLI_SCRIPT) { echo "!!! $message !!!\n"; - if (!empty($CFG->debug) and $CFG->debug >= DEBUG_DEVELOPER) { + if (!empty($CFG->debug) && $CFG->debug >= DEBUG_DEVELOPER) { if (!empty($debuginfo)) { echo "\nDebug info: $debuginfo"; } @@ -175,13 +183,12 @@ class bootstrap_renderer { } } return; - } else if (AJAX_SCRIPT) { $e = new stdClass(); $e->error = $message; - $e->stacktrace = NULL; - $e->debuginfo = NULL; - if (!empty($CFG->debug) and $CFG->debug >= DEBUG_DEVELOPER) { + $e->stacktrace = null; + $e->debuginfo = null; + if (!empty($CFG->debug) && $CFG->debug >= DEBUG_DEVELOPER) { if (!empty($debuginfo)) { $e->debuginfo = $debuginfo; } @@ -200,7 +207,7 @@ class bootstrap_renderer { $protocol = (isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : 'HTTP/1.0'); @header($protocol . ' 500 Internal Server Error'); - // better disable any caching + // Better disable any caching. @header('Content-Type: text/html; charset=utf-8'); @header('X-UA-Compatible: IE=edge'); @header('Cache-Control: no-store, no-cache, must-revalidate'); @@ -222,7 +229,7 @@ class bootstrap_renderer { /** * Early notification message - * @static + * * @param string $message * @param string $classes usually notifyproblem or notifysuccess * @return string @@ -233,26 +240,27 @@ class bootstrap_renderer { /** * Page should redirect message. - * @static + * * @param string $encodedurl redirect url * @return string */ public static function plain_redirect_message($encodedurl) { - $message = '
' . get_string('pageshouldredirect') . '
'. get_string('continue') .'
'; + $message = '
'; + $message .= get_string('pageshouldredirect') . '
' . get_string('continue') . '
'; return self::plain_page(get_string('redirect'), $message); } /** - * Early redirection page, used before full init of $PAGE global - * @static + * Early redirection page, used before full init of $PAGE global. + * * @param string $encodedurl redirect url * @param string $message redirect message * @param int $delay time in seconds * @return string redirect page */ public static function early_redirect_message($encodedurl, $message, $delay) { - $meta = ''; + $meta = ''; $content = self::early_error_content($message, null, null, null); $content .= self::plain_redirect_message($encodedurl); @@ -261,7 +269,7 @@ class bootstrap_renderer { /** * Output basic html page. - * @static + * * @param string $title page title * @param string $content page content * @param string $meta meta tag