From 4077f16aecafbc5d07a43f458aeb67d3edf1213d Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Mon, 26 Feb 2018 22:34:13 +0100 Subject: [PATCH 1/3] Make ILogger strict * Make implementations strict * Add scalar typehints Signed-off-by: Roeland Jago Douma --- .../Sabre/ExceptionLoggerPluginTest.php | 2 +- lib/private/Log.php | 33 ++++++++++--------- lib/public/ILogger.php | 21 ++++++------ tests/lib/TempManagerTest.php | 2 +- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php index 9cfb2c465e..ba28c39e09 100644 --- a/apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php @@ -41,7 +41,7 @@ class TestLogger extends Log { //disable original constructor } - public function log($level, $message, array $context = array()) { + public function log($level, string $message, array $context = array()) { $this->level = $level; $this->message = $message; } diff --git a/lib/private/Log.php b/lib/private/Log.php index bed0321bef..d59d8b5ff0 100644 --- a/lib/private/Log.php +++ b/lib/private/Log.php @@ -1,4 +1,5 @@ config->getValue('log_type', 'file'); $this->logger = static::getLogClass($logType); - call_user_func(array($this->logger, 'init')); + call_user_func([$this->logger, 'init']); } else { $this->logger = $logger; } @@ -148,7 +149,7 @@ class Log implements ILogger { * @param array $context * @return void */ - public function emergency($message, array $context = array()) { + public function emergency(string $message, array $context = []) { $this->log(Util::FATAL, $message, $context); } @@ -162,7 +163,7 @@ class Log implements ILogger { * @param array $context * @return void */ - public function alert($message, array $context = array()) { + public function alert(string $message, array $context = []) { $this->log(Util::ERROR, $message, $context); } @@ -175,7 +176,7 @@ class Log implements ILogger { * @param array $context * @return void */ - public function critical($message, array $context = array()) { + public function critical(string $message, array $context = []) { $this->log(Util::ERROR, $message, $context); } @@ -187,7 +188,7 @@ class Log implements ILogger { * @param array $context * @return void */ - public function error($message, array $context = array()) { + public function error(string $message, array $context = []) { $this->log(Util::ERROR, $message, $context); } @@ -201,7 +202,7 @@ class Log implements ILogger { * @param array $context * @return void */ - public function warning($message, array $context = array()) { + public function warning(string $message, array $context = []) { $this->log(Util::WARN, $message, $context); } @@ -212,7 +213,7 @@ class Log implements ILogger { * @param array $context * @return void */ - public function notice($message, array $context = array()) { + public function notice(string $message, array $context = []) { $this->log(Util::INFO, $message, $context); } @@ -225,7 +226,7 @@ class Log implements ILogger { * @param array $context * @return void */ - public function info($message, array $context = array()) { + public function info(string $message, array $context = []) { $this->log(Util::INFO, $message, $context); } @@ -236,7 +237,7 @@ class Log implements ILogger { * @param array $context * @return void */ - public function debug($message, array $context = array()) { + public function debug(string $message, array $context = []) { $this->log(Util::DEBUG, $message, $context); } @@ -249,7 +250,7 @@ class Log implements ILogger { * @param array $context * @return void */ - public function log($level, $message, array $context = array()) { + public function log($level, string $message, array $context = []) { $minLevel = min($this->config->getValue('loglevel', Util::WARN), Util::FATAL); $logCondition = $this->config->getValue('log.condition', []); @@ -272,7 +273,7 @@ class Log implements ILogger { $app = 'no app in context'; } // interpolate $message as defined in PSR-3 - $replace = array(); + $replace = []; foreach ($context as $key => $val) { $replace['{' . $key . '}'] = $val; } @@ -318,7 +319,7 @@ class Log implements ILogger { if ($level >= $minLevel) { $logger = $this->logger; - call_user_func(array($logger, 'write'), $app, $message, $level); + call_user_func([$logger, 'write'], $app, $message, $level); } } @@ -330,20 +331,20 @@ class Log implements ILogger { * @return void * @since 8.2.0 */ - public function logException($exception, array $context = array()) { + public function logException($exception, array $context = []) { $level = Util::ERROR; if (isset($context['level'])) { $level = $context['level']; unset($context['level']); } - $data = array( + $data = [ 'Exception' => get_class($exception), 'Message' => $exception->getMessage(), 'Code' => $exception->getCode(), 'Trace' => $exception->getTraceAsString(), 'File' => $exception->getFile(), 'Line' => $exception->getLine(), - ); + ]; $data['Trace'] = preg_replace('!(' . implode('|', $this->methodsWithSensitiveParameters) . ')\(.*\)!', '$1(*** sensitive parameters replaced ***)', $data['Trace']); if ($exception instanceof HintException) { $data['Hint'] = $exception->getHint(); @@ -362,7 +363,7 @@ class Log implements ILogger { * @return string * @internal */ - public static function getLogClass($logType) { + public static function getLogClass(string $logType): string { switch (strtolower($logType)) { case 'errorlog': return \OC\Log\Errorlog::class; diff --git a/lib/public/ILogger.php b/lib/public/ILogger.php index 6ee850ee8a..2d44244075 100644 --- a/lib/public/ILogger.php +++ b/lib/public/ILogger.php @@ -1,4 +1,5 @@ Date: Tue, 27 Feb 2018 10:43:34 +0100 Subject: [PATCH 2/3] Typehint Throwable Signed-off-by: Roeland Jago Douma --- lib/private/Log.php | 2 +- lib/public/ILogger.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/private/Log.php b/lib/private/Log.php index d59d8b5ff0..a18b5db7d0 100644 --- a/lib/private/Log.php +++ b/lib/private/Log.php @@ -331,7 +331,7 @@ class Log implements ILogger { * @return void * @since 8.2.0 */ - public function logException($exception, array $context = []) { + public function logException(\Throwable $exception, array $context = []) { $level = Util::ERROR; if (isset($context['level'])) { $level = $context['level']; diff --git a/lib/public/ILogger.php b/lib/public/ILogger.php index 2d44244075..9370913a82 100644 --- a/lib/public/ILogger.php +++ b/lib/public/ILogger.php @@ -144,5 +144,5 @@ interface ILogger { * @return void * @since 8.2.0 */ - public function logException($exception, array $context = []); + public function logException(\Throwable $exception, array $context = []); } From 6994bce951dd9d1d57b6e8c003f9f3cab16f47d0 Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Tue, 27 Feb 2018 10:45:35 +0100 Subject: [PATCH 3/3] Loglevel is an int Signed-off-by: Roeland Jago Douma --- .../tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php | 2 +- lib/private/Log.php | 4 ++-- lib/public/ILogger.php | 4 ++-- tests/lib/TempManagerTest.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php b/apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php index ba28c39e09..ff928ccede 100644 --- a/apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php +++ b/apps/dav/tests/unit/Connector/Sabre/ExceptionLoggerPluginTest.php @@ -41,7 +41,7 @@ class TestLogger extends Log { //disable original constructor } - public function log($level, string $message, array $context = array()) { + public function log(int $level, string $message, array $context = array()) { $this->level = $level; $this->message = $message; } diff --git a/lib/private/Log.php b/lib/private/Log.php index a18b5db7d0..e47f68807d 100644 --- a/lib/private/Log.php +++ b/lib/private/Log.php @@ -245,12 +245,12 @@ class Log implements ILogger { /** * Logs with an arbitrary level. * - * @param mixed $level + * @param int $level * @param string $message * @param array $context * @return void */ - public function log($level, string $message, array $context = []) { + public function log(int $level, string $message, array $context = []) { $minLevel = min($this->config->getValue('loglevel', Util::WARN), Util::FATAL); $logCondition = $this->config->getValue('log.condition', []); diff --git a/lib/public/ILogger.php b/lib/public/ILogger.php index 9370913a82..b6e945546e 100644 --- a/lib/public/ILogger.php +++ b/lib/public/ILogger.php @@ -120,13 +120,13 @@ interface ILogger { /** * Logs with an arbitrary level. * - * @param mixed $level + * @param int $level * @param string $message * @param array $context * @return mixed * @since 7.0.0 */ - public function log($level, string $message, array $context = []); + public function log(int $level, string $message, array $context = []); /** * Logs an exception very detailed diff --git a/tests/lib/TempManagerTest.php b/tests/lib/TempManagerTest.php index 15b22a7b30..c9e069d9d0 100644 --- a/tests/lib/TempManagerTest.php +++ b/tests/lib/TempManagerTest.php @@ -17,7 +17,7 @@ class NullLogger extends Log { //disable original constructor } - public function log($level, string $message, array $context = array()) { + public function log(int $level, string $message, array $context = array()) { //noop } }