From 6f26801be6563ef20fe6fab7deda2adefe704b44 Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Thu, 26 May 2016 17:40:25 +0100 Subject: [PATCH] More type hints --- src/Monolog/Formatter/JsonFormatter.php | 9 ++------- src/Monolog/Formatter/LineFormatter.php | 7 +------ src/Monolog/Formatter/NormalizerFormatter.php | 16 ++++++---------- 3 files changed, 9 insertions(+), 23 deletions(-) diff --git a/src/Monolog/Formatter/JsonFormatter.php b/src/Monolog/Formatter/JsonFormatter.php index a985e2ab..52c50dfa 100644 --- a/src/Monolog/Formatter/JsonFormatter.php +++ b/src/Monolog/Formatter/JsonFormatter.php @@ -163,17 +163,12 @@ class JsonFormatter extends NormalizerFormatter * Normalizes given exception with or without its own stack trace based on * `includeStacktraces` property. * - * @param Exception|Throwable $e + * @param Throwable $e * * @return array */ - protected function normalizeException($e) + protected function normalizeException(\Throwable $e) { - // TODO 2.0 only check for Throwable - if (!$e instanceof Exception && !$e instanceof \Throwable) { - throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.get_class($e)); - } - $data = array( 'class' => get_class($e), 'message' => $e->getMessage(), diff --git a/src/Monolog/Formatter/LineFormatter.php b/src/Monolog/Formatter/LineFormatter.php index 08929d2d..96033ba5 100644 --- a/src/Monolog/Formatter/LineFormatter.php +++ b/src/Monolog/Formatter/LineFormatter.php @@ -119,13 +119,8 @@ class LineFormatter extends NormalizerFormatter return $this->replaceNewlines($this->convertToString($value)); } - protected function normalizeException($e) + protected function normalizeException(\Throwable $e) { - // TODO 2.0 only check for Throwable - if (!$e instanceof \Exception && !$e instanceof \Throwable) { - throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.get_class($e)); - } - $previousText = ''; if ($previous = $e->getPrevious()) { do { diff --git a/src/Monolog/Formatter/NormalizerFormatter.php b/src/Monolog/Formatter/NormalizerFormatter.php index d73ac654..bd524bd6 100644 --- a/src/Monolog/Formatter/NormalizerFormatter.php +++ b/src/Monolog/Formatter/NormalizerFormatter.php @@ -28,7 +28,7 @@ class NormalizerFormatter implements FormatterInterface /** * @param string $dateFormat The format of the timestamp: one supported by DateTime::format */ - public function __construct($dateFormat = null) + public function __construct(string $dateFormat = null) { $this->dateFormat = $dateFormat; if (!function_exists('json_encode')) { @@ -116,12 +116,8 @@ class NormalizerFormatter implements FormatterInterface return '[unknown('.gettype($data).')]'; } - protected function normalizeException($e) + protected function normalizeException(Throwable $e) { - if (!$e instanceof Throwable) { - throw new \InvalidArgumentException('Throwable expected, got '.gettype($e).' / '.get_class($e)); - } - $data = array( 'class' => get_class($e), 'message' => $e->getMessage(), @@ -155,7 +151,7 @@ class NormalizerFormatter implements FormatterInterface * @param mixed $data * @param bool $ignoreErrors * @throws \RuntimeException if encoding fails and errors are not ignored - * @return string + * @return string|bool */ protected function toJson($data, $ignoreErrors = false) { @@ -175,7 +171,7 @@ class NormalizerFormatter implements FormatterInterface /** * @param mixed $data - * @return string JSON encoded data or null on failure + * @return string|bool JSON encoded data or false on failure */ private function jsonEncode($data) { @@ -195,7 +191,7 @@ class NormalizerFormatter implements FormatterInterface * @throws \RuntimeException if failure can't be corrected * @return string JSON encoded data after error correction */ - private function handleJsonError($code, $data) + private function handleJsonError(int $code, $data): string { if ($code !== JSON_ERROR_UTF8) { $this->throwEncodeError($code, $data); @@ -225,7 +221,7 @@ class NormalizerFormatter implements FormatterInterface * @param mixed $data data that was meant to be encoded * @throws \RuntimeException */ - private function throwEncodeError($code, $data) + private function throwEncodeError(int $code, $data) { switch ($code) { case JSON_ERROR_DEPTH: