diff --git a/src/Error/BaseErrorHandler.php b/src/Error/BaseErrorHandler.php index aab65ce1121..758dc914e44 100644 --- a/src/Error/BaseErrorHandler.php +++ b/src/Error/BaseErrorHandler.php @@ -231,7 +231,7 @@ public function handleFatalError($code, $description, $file, $line) * Increases the PHP "memory_limit" ini setting by the specified amount * in kilobytes * - * @param string $additionalKb Number in kilobytes + * @param int $additionalKb Number in kilobytes * @return void */ public function increaseMemoryLimit($additionalKb) @@ -242,7 +242,7 @@ public function increaseMemoryLimit($additionalKb) } $limit = trim($limit); $units = strtoupper(substr($limit, -1)); - $current = substr($limit, 0, strlen($limit) - 1); + $current = (int)substr($limit, 0, strlen($limit) - 1); if ($units === 'M') { $current = $current * 1024; $units = 'K'; diff --git a/src/Error/ErrorHandler.php b/src/Error/ErrorHandler.php index 531d94082d1..fd3c386f6f9 100644 --- a/src/Error/ErrorHandler.php +++ b/src/Error/ErrorHandler.php @@ -94,7 +94,7 @@ public function __construct($options = []) $defaults = [ 'log' => true, 'trace' => false, - 'exceptionRenderer' => 'Cake\Error\ExceptionRenderer', + 'exceptionRenderer' => ExceptionRenderer::class, ]; $this->_options = $options + $defaults; } @@ -127,13 +127,14 @@ protected function _displayError($error, $debug) */ protected function _displayException($exception) { - $renderer = App::className($this->_options['exceptionRenderer'], 'Error'); + $rendererClassName = App::className($this->_options['exceptionRenderer'], 'Error'); try { - if (!$renderer) { - throw new Exception("$renderer is an invalid class."); + if (!$rendererClassName) { + throw new Exception("$rendererClassName is an invalid class."); } - $error = new $renderer($exception); - $response = $error->render(); + /* @var \Cake\Error\ExceptionRendererInterface $renderer */ + $renderer = new $rendererClassName($exception); + $response = $renderer->render(); $this->_clearOutput(); $this->_sendResponse($response); } catch (Exception $e) { diff --git a/src/Error/ExceptionRenderer.php b/src/Error/ExceptionRenderer.php index 3d7bdf41ea7..1008848ed6b 100644 --- a/src/Error/ExceptionRenderer.php +++ b/src/Error/ExceptionRenderer.php @@ -47,7 +47,7 @@ * Using a subclass of ExceptionRenderer gives you full control over how Exceptions are rendered, you * can configure your class in your config/app.php. */ -class ExceptionRenderer +class ExceptionRenderer implements ExceptionRendererInterface { /** @@ -121,6 +121,7 @@ protected function _getController() try { $class = App::className('Error', 'Controller', 'Controller'); + /* @var \Cake\Controller\Controller $controller */ $controller = new $class($request, $response); $controller->startupProcess(); $startup = true; diff --git a/src/Error/ExceptionRendererInterface.php b/src/Error/ExceptionRendererInterface.php new file mode 100644 index 00000000000..5535e356ceb --- /dev/null +++ b/src/Error/ExceptionRendererInterface.php @@ -0,0 +1,26 @@ +exceptionRenderer)) { $class = App::className($this->exceptionRenderer, 'Error'); if (!$class) { - throw new Exception("The '{$this->exceptionRenderer}' renderer class could not be found."); + throw new Exception(sprintf("The '%s' renderer class could not be found.", + $this->exceptionRenderer)); + } + if (!$class instanceof ExceptionRendererInterface) { + throw new Exception(sprintf("The '%s' renderer class must implement %s.", + $this->exceptionRenderer, + ExceptionRendererInterface::class)); } return new $class($exception);