ソースを参照

Add scalar types to processor/formatters and top level classes

Jordi Boggiano 9 年 前
コミット
6e6586257d
29 ファイル変更130 行追加196 行削除
  1. 13 7
      src/Monolog/ErrorHandler.php
  2. 3 0
      src/Monolog/Formatter/ChromePHPFormatter.php
  3. 4 15
      src/Monolog/Formatter/ElasticaFormatter.php
  4. 4 13
      src/Monolog/Formatter/FlowdockFormatter.php
  5. 5 5
      src/Monolog/Formatter/FluentdFormatter.php
  6. 2 2
      src/Monolog/Formatter/GelfMessageFormatter.php
  7. 6 6
      src/Monolog/Formatter/HtmlFormatter.php
  8. 12 29
      src/Monolog/Formatter/JsonFormatter.php
  9. 10 10
      src/Monolog/Formatter/LineFormatter.php
  10. 2 4
      src/Monolog/Formatter/LogglyFormatter.php
  11. 1 1
      src/Monolog/Formatter/LogmaticFormatter.php
  12. 2 3
      src/Monolog/Formatter/LogstashFormatter.php
  13. 11 8
      src/Monolog/Formatter/MongoDBFormatter.php
  14. 15 9
      src/Monolog/Formatter/NormalizerFormatter.php
  15. 1 1
      src/Monolog/Formatter/ScalarFormatter.php
  16. 8 2
      src/Monolog/Formatter/WildfireFormatter.php
  17. 6 17
      src/Monolog/Logger.php
  18. 2 6
      src/Monolog/Processor/GitProcessor.php
  19. 3 7
      src/Monolog/Processor/IntrospectionProcessor.php
  20. 1 5
      src/Monolog/Processor/MemoryPeakUsageProcessor.php
  21. 5 7
      src/Monolog/Processor/MemoryProcessor.php
  22. 1 5
      src/Monolog/Processor/MemoryUsageProcessor.php
  23. 2 6
      src/Monolog/Processor/MercurialProcessor.php
  24. 1 5
      src/Monolog/Processor/ProcessIdProcessor.php
  25. 1 1
      src/Monolog/Processor/PsrLogMessageProcessor.php
  26. 1 1
      src/Monolog/Processor/TagProcessor.php
  27. 3 3
      src/Monolog/Processor/UidProcessor.php
  28. 3 16
      src/Monolog/Processor/WebProcessor.php
  29. 2 2
      src/Monolog/Registry.php

+ 13 - 7
src/Monolog/ErrorHandler.php

@@ -55,7 +55,7 @@ class ErrorHandler
      * @param  int|false       $fatalLevel        a LogLevel::* constant, or false to disable fatal error handling
      * @return ErrorHandler
      */
-    public static function register(LoggerInterface $logger, $errorLevelMap = [], $exceptionLevelMap = [], $fatalLevel = null)
+    public static function register(LoggerInterface $logger, $errorLevelMap = [], $exceptionLevelMap = [], $fatalLevel = null): self
     {
         $handler = new static($logger);
         if ($errorLevelMap !== false) {
@@ -71,16 +71,18 @@ class ErrorHandler
         return $handler;
     }
 
-    public function registerExceptionHandler($levelMap = [], $callPrevious = true)
+    public function registerExceptionHandler($levelMap = [], $callPrevious = true): self
     {
         $prev = set_exception_handler([$this, 'handleException']);
         $this->uncaughtExceptionLevelMap = array_replace($this->defaultExceptionLevelMap(), $levelMap);
         if ($callPrevious && $prev) {
             $this->previousExceptionHandler = $prev;
         }
+
+        return $this;
     }
 
-    public function registerErrorHandler(array $levelMap = [], $callPrevious = true, $errorTypes = -1, $handleOnlyReportedErrors = true)
+    public function registerErrorHandler(array $levelMap = [], $callPrevious = true, $errorTypes = -1, $handleOnlyReportedErrors = true): self
     {
         $prev = set_error_handler([$this, 'handleError'], $errorTypes);
         $this->errorLevelMap = array_replace($this->defaultErrorLevelMap(), $levelMap);
@@ -89,18 +91,22 @@ class ErrorHandler
         }
 
         $this->handleOnlyReportedErrors = $handleOnlyReportedErrors;
+
+        return $this;
     }
 
-    public function registerFatalHandler($level = null, $reservedMemorySize = 20)
+    public function registerFatalHandler($level = null, $reservedMemorySize = 20): self
     {
         register_shutdown_function([$this, 'handleFatalError']);
 
         $this->reservedMemory = str_repeat(' ', 1024 * $reservedMemorySize);
         $this->fatalLevel = $level;
         $this->hasFatalErrorHandler = true;
+
+        return $this;
     }
 
-    protected function defaultExceptionLevelMap()
+    protected function defaultExceptionLevelMap(): array
     {
         return [
             'ParseError' => LogLevel::CRITICAL,
@@ -108,7 +114,7 @@ class ErrorHandler
         ];
     }
 
-    protected function defaultErrorLevelMap()
+    protected function defaultErrorLevelMap(): array
     {
         return [
             E_ERROR             => LogLevel::CRITICAL,
@@ -200,7 +206,7 @@ class ErrorHandler
         }
     }
 
-    private static function codeToString($code)
+    private static function codeToString($code): string
     {
         switch ($code) {
             case E_ERROR:

+ 3 - 0
src/Monolog/Formatter/ChromePHPFormatter.php

@@ -65,6 +65,9 @@ class ChromePHPFormatter implements FormatterInterface
         ];
     }
 
+    /**
+     * {@inheritdoc}
+     */
     public function formatBatch(array $records)
     {
         $formatted = [];

+ 4 - 15
src/Monolog/Formatter/ElasticaFormatter.php

@@ -34,7 +34,7 @@ class ElasticaFormatter extends NormalizerFormatter
      * @param string $index Elastic Search index name
      * @param string $type  Elastic Search document type
      */
-    public function __construct($index, $type)
+    public function __construct(string $index, string $type)
     {
         // elasticsearch requires a ISO 8601 format date with optional millisecond precision.
         parent::__construct('Y-m-d\TH:i:s.uP');
@@ -53,31 +53,20 @@ class ElasticaFormatter extends NormalizerFormatter
         return $this->getDocument($record);
     }
 
-    /**
-     * Getter index
-     * @return string
-     */
-    public function getIndex()
+    public function getIndex(): string
     {
         return $this->index;
     }
 
-    /**
-     * Getter type
-     * @return string
-     */
-    public function getType()
+    public function getType(): string
     {
         return $this->type;
     }
 
     /**
      * Convert a log message into an Elastica Document
-     *
-     * @param  array    $record Log message
-     * @return Document
      */
-    protected function getDocument($record)
+    protected function getDocument(array $record): Document
     {
         $document = new Document();
         $document->setData($record);

+ 4 - 13
src/Monolog/Formatter/FlowdockFormatter.php

@@ -28,11 +28,7 @@ class FlowdockFormatter implements FormatterInterface
      */
     private $sourceEmail;
 
-    /**
-     * @param string $source
-     * @param string $sourceEmail
-     */
-    public function __construct($source, $sourceEmail)
+    public function __construct(string $source, string $sourceEmail)
     {
         $this->source = $source;
         $this->sourceEmail = $sourceEmail;
@@ -41,7 +37,7 @@ class FlowdockFormatter implements FormatterInterface
     /**
      * {@inheritdoc}
      */
-    public function format(array $record)
+    public function format(array $record): array
     {
         $tags = [
             '#logs',
@@ -75,7 +71,7 @@ class FlowdockFormatter implements FormatterInterface
     /**
      * {@inheritdoc}
      */
-    public function formatBatch(array $records)
+    public function formatBatch(array $records): array
     {
         $formatted = [];
 
@@ -86,12 +82,7 @@ class FlowdockFormatter implements FormatterInterface
         return $formatted;
     }
 
-    /**
-     * @param string $message
-     *
-     * @return string
-     */
-    public function getShortMessage($message)
+    public function getShortMessage(string $message): string
     {
         static $hasMbString;
 

+ 5 - 5
src/Monolog/Formatter/FluentdFormatter.php

@@ -39,21 +39,21 @@ class FluentdFormatter implements FormatterInterface
      */
     protected $levelTag = false;
 
-    public function __construct($levelTag = false)
+    public function __construct(bool $levelTag = false)
     {
         if (!function_exists('json_encode')) {
             throw new \RuntimeException('PHP\'s json extension is required to use Monolog\'s FluentdUnixFormatter');
         }
 
-        $this->levelTag = (bool) $levelTag;
+        $this->levelTag = $levelTag;
     }
 
-    public function isUsingLevelsInTag()
+    public function isUsingLevelsInTag(): bool
     {
         return $this->levelTag;
     }
 
-    public function format(array $record)
+    public function format(array $record): string
     {
         $tag = $record['channel'];
         if ($this->levelTag) {
@@ -73,7 +73,7 @@ class FluentdFormatter implements FormatterInterface
         return json_encode([$tag, $record['datetime']->getTimestamp(), $message]);
     }
 
-    public function formatBatch(array $records)
+    public function formatBatch(array $records): string
     {
         $message = '';
         foreach ($records as $record) {

+ 2 - 2
src/Monolog/Formatter/GelfMessageFormatter.php

@@ -53,7 +53,7 @@ class GelfMessageFormatter extends NormalizerFormatter
         Logger::EMERGENCY => 0,
     ];
 
-    public function __construct($systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_')
+    public function __construct(string $systemName = null, string $extraPrefix = null, string $contextPrefix = 'ctxt_')
     {
         parent::__construct('U.u');
 
@@ -66,7 +66,7 @@ class GelfMessageFormatter extends NormalizerFormatter
     /**
      * {@inheritdoc}
      */
-    public function format(array $record)
+    public function format(array $record): Message
     {
         if (isset($record['context'])) {
             $record['context'] = parent::format($record['context']);

+ 6 - 6
src/Monolog/Formatter/HtmlFormatter.php

@@ -39,7 +39,7 @@ class HtmlFormatter extends NormalizerFormatter
     /**
      * @param string $dateFormat The format of the timestamp: one supported by DateTime::format
      */
-    public function __construct($dateFormat = null)
+    public function __construct(string $dateFormat = null)
     {
         parent::__construct($dateFormat);
     }
@@ -52,7 +52,7 @@ class HtmlFormatter extends NormalizerFormatter
      * @param  bool   $escapeTd false if td content must not be html escaped
      * @return string
      */
-    protected function addRow($th, $td = ' ', $escapeTd = true)
+    protected function addRow(string $th, string $td = ' ', bool $escapeTd = true): string
     {
         $th = htmlspecialchars($th, ENT_NOQUOTES, 'UTF-8');
         if ($escapeTd) {
@@ -69,7 +69,7 @@ class HtmlFormatter extends NormalizerFormatter
      * @param  int    $level Error level
      * @return string
      */
-    protected function addTitle($title, $level)
+    protected function addTitle(string $title, int $level)
     {
         $title = htmlspecialchars($title, ENT_NOQUOTES, 'UTF-8');
 
@@ -82,7 +82,7 @@ class HtmlFormatter extends NormalizerFormatter
      * @param  array $record A record to format
      * @return mixed The formatted record
      */
-    public function format(array $record)
+    public function format(array $record): string
     {
         $output = $this->addTitle($record['level_name'], $record['level']);
         $output .= '<table cellspacing="1" width="100%" class="monolog-output">';
@@ -116,7 +116,7 @@ class HtmlFormatter extends NormalizerFormatter
      * @param  array $records A set of records to format
      * @return mixed The formatted set of records
      */
-    public function formatBatch(array $records)
+    public function formatBatch(array $records): string
     {
         $message = '';
         foreach ($records as $record) {
@@ -126,7 +126,7 @@ class HtmlFormatter extends NormalizerFormatter
         return $message;
     }
 
-    protected function convertToString($data)
+    protected function convertToString($data): string
     {
         if (null === $data || is_scalar($data)) {
             return (string) $data;

+ 12 - 29
src/Monolog/Formatter/JsonFormatter.php

@@ -32,10 +32,7 @@ class JsonFormatter extends NormalizerFormatter
      */
     protected $includeStacktraces = false;
 
-    /**
-     * @param int $batchMode
-     */
-    public function __construct($batchMode = self::BATCH_MODE_JSON, $appendNewline = true)
+    public function __construct(int $batchMode = self::BATCH_MODE_JSON, bool $appendNewline = true)
     {
         $this->batchMode = $batchMode;
         $this->appendNewline = $appendNewline;
@@ -47,20 +44,16 @@ class JsonFormatter extends NormalizerFormatter
      * formatted as a JSON-encoded array. However, for
      * compatibility with some API endpoints, alternative styles
      * are available.
-     *
-     * @return int
      */
-    public function getBatchMode()
+    public function getBatchMode(): int
     {
         return $this->batchMode;
     }
 
     /**
      * True if newlines are appended to every formatted record
-     *
-     * @return bool
      */
-    public function isAppendingNewlines()
+    public function isAppendingNewlines(): bool
     {
         return $this->appendNewline;
     }
@@ -68,7 +61,7 @@ class JsonFormatter extends NormalizerFormatter
     /**
      * {@inheritdoc}
      */
-    public function format(array $record)
+    public function format(array $record): string
     {
         return $this->toJson($this->normalize($record), true) . ($this->appendNewline ? "\n" : '');
     }
@@ -76,7 +69,7 @@ class JsonFormatter extends NormalizerFormatter
     /**
      * {@inheritdoc}
      */
-    public function formatBatch(array $records)
+    public function formatBatch(array $records): string
     {
         switch ($this->batchMode) {
             case static::BATCH_MODE_NEWLINES:
@@ -91,18 +84,15 @@ class JsonFormatter extends NormalizerFormatter
     /**
      * @param bool $include
      */
-    public function includeStacktraces($include = true)
+    public function includeStacktraces(bool $include = true)
     {
         $this->includeStacktraces = $include;
     }
 
     /**
      * Return a JSON-encoded array of records.
-     *
-     * @param  array  $records
-     * @return string
      */
-    protected function formatBatchJson(array $records)
+    protected function formatBatchJson(array $records): string
     {
         return $this->toJson($this->normalize($records), true);
     }
@@ -110,11 +100,8 @@ class JsonFormatter extends NormalizerFormatter
     /**
      * Use new lines to separate records instead of a
      * JSON-encoded array.
-     *
-     * @param  array  $records
-     * @return string
      */
-    protected function formatBatchNewlines(array $records)
+    protected function formatBatchNewlines(array $records): string
     {
         $instance = $this;
 
@@ -135,7 +122,7 @@ class JsonFormatter extends NormalizerFormatter
      *
      * @return mixed
      */
-    protected function normalize($data, $depth = 0)
+    protected function normalize($data, int $depth = 0)
     {
         if ($depth > 9) {
             return 'Over 9 levels deep, aborting normalization';
@@ -157,7 +144,7 @@ class JsonFormatter extends NormalizerFormatter
         }
 
         if ($data instanceof Throwable) {
-            return $this->normalizeException($data);
+            return $this->normalizeException($data, $depth);
         }
 
         return $data;
@@ -166,12 +153,8 @@ class JsonFormatter extends NormalizerFormatter
     /**
      * Normalizes given exception with or without its own stack trace based on
      * `includeStacktraces` property.
-     *
-     * @param Throwable $e
-     *
-     * @return array
      */
-    protected function normalizeException(Throwable $e)
+    protected function normalizeException(Throwable $e, int $depth = 0): array
     {
         $data = [
             'class' => get_class($e),
@@ -196,7 +179,7 @@ class JsonFormatter extends NormalizerFormatter
         }
 
         if ($previous = $e->getPrevious()) {
-            $data['previous'] = $this->normalizeException($previous);
+            $data['previous'] = $this->normalizeException($previous, $depth + 1);
         }
 
         return $data;

+ 10 - 10
src/Monolog/Formatter/LineFormatter.php

@@ -34,7 +34,7 @@ class LineFormatter extends NormalizerFormatter
      * @param bool   $allowInlineLineBreaks      Whether to allow inline line breaks in log entries
      * @param bool   $ignoreEmptyContextAndExtra
      */
-    public function __construct($format = null, $dateFormat = null, $allowInlineLineBreaks = false, $ignoreEmptyContextAndExtra = false)
+    public function __construct(string $format = null, string $dateFormat = null, bool $allowInlineLineBreaks = false, bool $ignoreEmptyContextAndExtra = false)
     {
         $this->format = $format ?: static::SIMPLE_FORMAT;
         $this->allowInlineLineBreaks = $allowInlineLineBreaks;
@@ -42,7 +42,7 @@ class LineFormatter extends NormalizerFormatter
         parent::__construct($dateFormat);
     }
 
-    public function includeStacktraces($include = true)
+    public function includeStacktraces(bool $include = true)
     {
         $this->includeStacktraces = $include;
         if ($this->includeStacktraces) {
@@ -50,12 +50,12 @@ class LineFormatter extends NormalizerFormatter
         }
     }
 
-    public function allowInlineLineBreaks($allow = true)
+    public function allowInlineLineBreaks(bool $allow = true)
     {
         $this->allowInlineLineBreaks = $allow;
     }
 
-    public function ignoreEmptyContextAndExtra($ignore = true)
+    public function ignoreEmptyContextAndExtra(bool $ignore = true)
     {
         $this->ignoreEmptyContextAndExtra = $ignore;
     }
@@ -63,7 +63,7 @@ class LineFormatter extends NormalizerFormatter
     /**
      * {@inheritdoc}
      */
-    public function format(array $record)
+    public function format(array $record): string
     {
         $vars = parent::format($record);
 
@@ -104,7 +104,7 @@ class LineFormatter extends NormalizerFormatter
         return $output;
     }
 
-    public function formatBatch(array $records)
+    public function formatBatch(array $records): string
     {
         $message = '';
         foreach ($records as $record) {
@@ -114,12 +114,12 @@ class LineFormatter extends NormalizerFormatter
         return $message;
     }
 
-    public function stringify($value)
+    public function stringify($value): string
     {
         return $this->replaceNewlines($this->convertToString($value));
     }
 
-    protected function normalizeException(\Throwable $e)
+    protected function normalizeException(\Throwable $e, int $depth = 0): string
     {
         $previousText = '';
         if ($previous = $e->getPrevious()) {
@@ -136,7 +136,7 @@ class LineFormatter extends NormalizerFormatter
         return $str;
     }
 
-    protected function convertToString($data)
+    protected function convertToString($data): string
     {
         if (null === $data || is_bool($data)) {
             return var_export($data, true);
@@ -149,7 +149,7 @@ class LineFormatter extends NormalizerFormatter
         return $this->toJson($data, true);
     }
 
-    protected function replaceNewlines($str)
+    protected function replaceNewlines(string $str): string
     {
         if ($this->allowInlineLineBreaks) {
             if (0 === strpos($str, '{')) {

+ 2 - 4
src/Monolog/Formatter/LogglyFormatter.php

@@ -21,10 +21,8 @@ class LogglyFormatter extends JsonFormatter
     /**
      * Overrides the default batch mode to new lines for compatibility with the
      * Loggly bulk API.
-     *
-     * @param int $batchMode
      */
-    public function __construct($batchMode = self::BATCH_MODE_NEWLINES, $appendNewline = false)
+    public function __construct(int $batchMode = self::BATCH_MODE_NEWLINES, bool $appendNewline = false)
     {
         parent::__construct($batchMode, $appendNewline);
     }
@@ -35,7 +33,7 @@ class LogglyFormatter extends JsonFormatter
      * @see https://www.loggly.com/docs/automated-parsing/#json
      * @see \Monolog\Formatter\JsonFormatter::format()
      */
-    public function format(array $record)
+    public function format(array $record): string
     {
         if (isset($record["datetime"]) && ($record["datetime"] instanceof \DateTimeInterface)) {
             $record["timestamp"] = $record["datetime"]->format("Y-m-d\TH:i:s.uO");

+ 1 - 1
src/Monolog/Formatter/LogmaticFormatter.php

@@ -54,7 +54,7 @@ class LogmaticFormatter extends JsonFormatter
      * @see http://doc.logmatic.io/docs/basics-to-send-data
      * @see \Monolog\Formatter\JsonFormatter::format()
      */
-    public function format(array $record)
+    public function format(array $record): string
     {
         if (!empty($this->hostname)) {
             $record['hostname'] = $this->hostname;

+ 2 - 3
src/Monolog/Formatter/LogstashFormatter.php

@@ -46,9 +46,8 @@ class LogstashFormatter extends NormalizerFormatter
      * @param string $systemName      the system/machine name, used as the "source" field of logstash, defaults to the hostname of the machine
      * @param string $extraPrefix     prefix for extra keys inside logstash "fields"
      * @param string $contextPrefix   prefix for context keys inside logstash "fields", defaults to ctxt_
-     * @param int    $version         the logstash format version to use, defaults to 0
      */
-    public function __construct($applicationName, $systemName = null, $extraPrefix = null, $contextPrefix = 'ctxt_')
+    public function __construct(string $applicationName, string $systemName = null, string $extraPrefix = null, string $contextPrefix = 'ctxt_')
     {
         // logstash requires a ISO 8601 format date with optional millisecond precision.
         parent::__construct('Y-m-d\TH:i:s.uP');
@@ -62,7 +61,7 @@ class LogstashFormatter extends NormalizerFormatter
     /**
      * {@inheritdoc}
      */
-    public function format(array $record)
+    public function format(array $record): string
     {
         $record = parent::format($record);
 

+ 11 - 8
src/Monolog/Formatter/MongoDBFormatter.php

@@ -27,16 +27,16 @@ class MongoDBFormatter implements FormatterInterface
      * @param int  $maxNestingLevel        0 means infinite nesting, the $record itself is level 1, $record['context'] is 2
      * @param bool $exceptionTraceAsString set to false to log exception traces as a sub documents instead of strings
      */
-    public function __construct($maxNestingLevel = 3, $exceptionTraceAsString = true)
+    public function __construct(int $maxNestingLevel = 3, bool $exceptionTraceAsString = true)
     {
         $this->maxNestingLevel = max($maxNestingLevel, 0);
-        $this->exceptionTraceAsString = (bool) $exceptionTraceAsString;
+        $this->exceptionTraceAsString = $exceptionTraceAsString;
     }
 
     /**
      * {@inheritDoc}
      */
-    public function format(array $record)
+    public function format(array $record): array
     {
         return $this->formatArray($record);
     }
@@ -44,7 +44,7 @@ class MongoDBFormatter implements FormatterInterface
     /**
      * {@inheritDoc}
      */
-    public function formatBatch(array $records)
+    public function formatBatch(array $records): array
     {
         foreach ($records as $key => $record) {
             $records[$key] = $this->format($record);
@@ -53,7 +53,10 @@ class MongoDBFormatter implements FormatterInterface
         return $records;
     }
 
-    protected function formatArray(array $record, $nestingLevel = 0)
+    /**
+     * @return array|string Array except when max nesting level is reached then a string "[...]"
+     */
+    protected function formatArray(array $record, int $nestingLevel = 0)
     {
         if ($this->maxNestingLevel == 0 || $nestingLevel <= $this->maxNestingLevel) {
             foreach ($record as $name => $value) {
@@ -74,7 +77,7 @@ class MongoDBFormatter implements FormatterInterface
         return $record;
     }
 
-    protected function formatObject($value, $nestingLevel)
+    protected function formatObject($value, int $nestingLevel)
     {
         $objectVars = get_object_vars($value);
         $objectVars['class'] = get_class($value);
@@ -82,7 +85,7 @@ class MongoDBFormatter implements FormatterInterface
         return $this->formatArray($objectVars, $nestingLevel);
     }
 
-    protected function formatException(\Throwable $exception, $nestingLevel)
+    protected function formatException(\Throwable $exception, int $nestingLevel)
     {
         $formattedException = [
             'class' => get_class($exception),
@@ -100,7 +103,7 @@ class MongoDBFormatter implements FormatterInterface
         return $this->formatArray($formattedException, $nestingLevel);
     }
 
-    protected function formatDate(\DateTimeInterface $value, $nestingLevel)
+    protected function formatDate(\DateTimeInterface $value, int $nestingLevel): UTCDateTime
     {
         return new UTCDateTime((string) floor($value->format('U.u') * 1000));
     }

+ 15 - 9
src/Monolog/Formatter/NormalizerFormatter.php

@@ -39,7 +39,7 @@ class NormalizerFormatter implements FormatterInterface
     /**
      * {@inheritdoc}
      */
-    public function format(array $record)
+    public function format(array $record): array
     {
         return $this->normalize($record);
     }
@@ -47,7 +47,7 @@ class NormalizerFormatter implements FormatterInterface
     /**
      * {@inheritdoc}
      */
-    public function formatBatch(array $records)
+    public function formatBatch(array $records): array
     {
         foreach ($records as $key => $record) {
             $records[$key] = $this->format($record);
@@ -56,7 +56,11 @@ class NormalizerFormatter implements FormatterInterface
         return $records;
     }
 
-    protected function normalize($data, $depth = 0)
+    /**
+     * @param mixed $data
+     * @return int|bool|string|null|array
+     */
+    protected function normalize($data, int $depth = 0)
     {
         if ($depth > 9) {
             return 'Over 9 levels deep, aborting normalization';
@@ -96,7 +100,7 @@ class NormalizerFormatter implements FormatterInterface
 
         if (is_object($data)) {
             if ($data instanceof Throwable) {
-                return $this->normalizeException($data);
+                return $this->normalizeException($data, $depth);
             }
 
             if ($data instanceof \JsonSerializable) {
@@ -123,7 +127,10 @@ class NormalizerFormatter implements FormatterInterface
         return '[unknown('.gettype($data).')]';
     }
 
-    protected function normalizeException(Throwable $e)
+    /**
+     * @return array
+     */
+    protected function normalizeException(Throwable $e, int $depth = 0)
     {
         $data = [
             'class' => get_class($e),
@@ -155,12 +162,12 @@ class NormalizerFormatter implements FormatterInterface
                 $data['trace'][] = $frame['function'];
             } else {
                 // We should again normalize the frames, because it might contain invalid items
-                $data['trace'][] = $this->toJson($this->normalize($frame), true);
+                $data['trace'][] = $this->toJson($this->normalize($frame, $depth + 1), true);
             }
         }
 
         if ($previous = $e->getPrevious()) {
-            $data['previous'] = $this->normalizeException($previous);
+            $data['previous'] = $this->normalizeException($previous, $depth + 1);
         }
 
         return $data;
@@ -170,11 +177,10 @@ class NormalizerFormatter implements FormatterInterface
      * Return the JSON representation of a value
      *
      * @param  mixed             $data
-     * @param  bool              $ignoreErrors
      * @throws \RuntimeException if encoding fails and errors are not ignored
      * @return string|bool
      */
-    protected function toJson($data, $ignoreErrors = false)
+    protected function toJson($data, bool $ignoreErrors = false)
     {
         // suppress json_encode errors since it's twitchy with some inputs
         if ($ignoreErrors) {

+ 1 - 1
src/Monolog/Formatter/ScalarFormatter.php

@@ -22,7 +22,7 @@ class ScalarFormatter extends NormalizerFormatter
     /**
      * {@inheritdoc}
      */
-    public function format(array $record)
+    public function format(array $record): array
     {
         foreach ($record as $key => $value) {
             $record[$key] = $this->normalizeValue($value);

+ 8 - 2
src/Monolog/Formatter/WildfireFormatter.php

@@ -41,7 +41,7 @@ class WildfireFormatter extends NormalizerFormatter
     /**
      * {@inheritdoc}
      */
-    public function format(array $record)
+    public function format(array $record): string
     {
         // Retrieve the line and file if set and remove them from the formatted extra
         $file = $line = '';
@@ -97,12 +97,18 @@ class WildfireFormatter extends NormalizerFormatter
         );
     }
 
+    /**
+     * {@inheritdoc}
+     */
     public function formatBatch(array $records)
     {
         throw new \BadMethodCallException('Batch formatting does not make sense for the WildfireFormatter');
     }
 
-    protected function normalize($data, $depth = 0)
+    /**
+     * {@inheritdoc}
+     */
+    protected function normalize($data, int $depth = 0)
     {
         if (is_object($data) && !$data instanceof \DateTimeInterface) {
             return $data;

+ 6 - 17
src/Monolog/Logger.php

@@ -136,7 +136,7 @@ class Logger implements LoggerInterface
     protected $timezone;
 
     /**
-     * @param string             $name       The logging channel
+     * @param string             $name       The logging channel, a simple descriptive name that is attached to all log records
      * @param HandlerInterface[] $handlers   Optional stack of handlers, the first one in the array is called first, etc.
      * @param callable[]         $processors Optional array of processors
      * @param DateTimeZone       $timezone   Optional timezone, if not provided date_default_timezone_get() will be used
@@ -149,9 +149,6 @@ class Logger implements LoggerInterface
         $this->timezone = $timezone ?: new DateTimeZone(date_default_timezone_get() ?: 'UTC');
     }
 
-    /**
-     * @return string
-     */
     public function getName(): string
     {
         return $this->name;
@@ -159,8 +156,6 @@ class Logger implements LoggerInterface
 
     /**
      * Return a new cloned instance with the name changed
-     *
-     * @return static
      */
     public function withName(string $name): self
     {
@@ -172,9 +167,6 @@ class Logger implements LoggerInterface
 
     /**
      * Pushes a handler on to the stack.
-     *
-     * @param  HandlerInterface $handler
-     * @return $this
      */
     public function pushHandler(HandlerInterface $handler): self
     {
@@ -186,8 +178,7 @@ class Logger implements LoggerInterface
     /**
      * Pops a handler from the stack
      *
-     * @throws \LogicException  If empty handler stack
-     * @return HandlerInterface
+     * @throws \LogicException If empty handler stack
      */
     public function popHandler(): HandlerInterface
     {
@@ -203,8 +194,7 @@ class Logger implements LoggerInterface
      *
      * If a map is passed, keys will be ignored.
      *
-     * @param  HandlerInterface[] $handlers
-     * @return $this
+     * @param HandlerInterface[] $handlers
      */
     public function setHandlers(array $handlers): self
     {
@@ -226,9 +216,6 @@ class Logger implements LoggerInterface
 
     /**
      * Adds a processor on to the stack.
-     *
-     * @param  callable $callback
-     * @return $this
      */
     public function pushProcessor(callable $callback): self
     {
@@ -532,9 +519,11 @@ class Logger implements LoggerInterface
      *
      * @param DateTimeZone $tz Timezone object
      */
-    public function setTimezone(DateTimeZone $tz)
+    public function setTimezone(DateTimeZone $tz): self
     {
         $this->timezone = $tz;
+
+        return $this;
     }
 
     /**

+ 2 - 6
src/Monolog/Processor/GitProcessor.php

@@ -29,11 +29,7 @@ class GitProcessor
         $this->level = Logger::toMonologLevel($level);
     }
 
-    /**
-     * @param  array $record
-     * @return array
-     */
-    public function __invoke(array $record)
+    public function __invoke(array $record): array
     {
         // return if the level is not high enough
         if ($record['level'] < $this->level) {
@@ -45,7 +41,7 @@ class GitProcessor
         return $record;
     }
 
-    private static function getGitInfo()
+    private static function getGitInfo(): array
     {
         if (self::$cache) {
             return self::$cache;

+ 3 - 7
src/Monolog/Processor/IntrospectionProcessor.php

@@ -37,18 +37,14 @@ class IntrospectionProcessor
         'call_user_func_array',
     ];
 
-    public function __construct($level = Logger::DEBUG, array $skipClassesPartials = [], $skipStackFramesCount = 0)
+    public function __construct($level = Logger::DEBUG, array $skipClassesPartials = [], int $skipStackFramesCount = 0)
     {
         $this->level = Logger::toMonologLevel($level);
         $this->skipClassesPartials = array_merge(['Monolog\\'], $skipClassesPartials);
         $this->skipStackFramesCount = $skipStackFramesCount;
     }
 
-    /**
-     * @param  array $record
-     * @return array
-     */
-    public function __invoke(array $record)
+    public function __invoke(array $record): array
     {
         // return if the level is not high enough
         if ($record['level'] < $this->level) {
@@ -96,7 +92,7 @@ class IntrospectionProcessor
         return $record;
     }
 
-    private function isTraceClassOrSkippedFunction(array $trace, $index)
+    private function isTraceClassOrSkippedFunction(array $trace, int $index)
     {
         if (!isset($trace[$index])) {
             return false;

+ 1 - 5
src/Monolog/Processor/MemoryPeakUsageProcessor.php

@@ -19,11 +19,7 @@ namespace Monolog\Processor;
  */
 class MemoryPeakUsageProcessor extends MemoryProcessor
 {
-    /**
-     * @param  array $record
-     * @return array
-     */
-    public function __invoke(array $record)
+    public function __invoke(array $record): array
     {
         $bytes = memory_get_peak_usage($this->realUsage);
         $formatted = $this->formatBytes($bytes);

+ 5 - 7
src/Monolog/Processor/MemoryProcessor.php

@@ -32,22 +32,20 @@ abstract class MemoryProcessor
      * @param bool $realUsage     Set this to true to get the real size of memory allocated from system.
      * @param bool $useFormatting If true, then format memory size to human readable string (MB, KB, B depending on size)
      */
-    public function __construct($realUsage = true, $useFormatting = true)
+    public function __construct(bool $realUsage = true, bool $useFormatting = true)
     {
-        $this->realUsage = (boolean) $realUsage;
-        $this->useFormatting = (boolean) $useFormatting;
+        $this->realUsage = $realUsage;
+        $this->useFormatting = $useFormatting;
     }
 
     /**
      * Formats bytes into a human readable string if $this->useFormatting is true, otherwise return $bytes as is
      *
      * @param  int        $bytes
-     * @return string|int Formatted string if $this->useFormatting is true, otherwise return $bytes as is
+     * @return string|int Formatted string if $this->useFormatting is true, otherwise return $bytes as int
      */
-    protected function formatBytes($bytes)
+    protected function formatBytes(int $bytes)
     {
-        $bytes = (int) $bytes;
-
         if (!$this->useFormatting) {
             return $bytes;
         }

+ 1 - 5
src/Monolog/Processor/MemoryUsageProcessor.php

@@ -19,11 +19,7 @@ namespace Monolog\Processor;
  */
 class MemoryUsageProcessor extends MemoryProcessor
 {
-    /**
-     * @param  array $record
-     * @return array
-     */
-    public function __invoke(array $record)
+    public function __invoke(array $record): array
     {
         $bytes = memory_get_usage($this->realUsage);
         $formatted = $this->formatBytes($bytes);

+ 2 - 6
src/Monolog/Processor/MercurialProcessor.php

@@ -28,11 +28,7 @@ class MercurialProcessor
         $this->level = Logger::toMonologLevel($level);
     }
 
-    /**
-     * @param  array $record
-     * @return array
-     */
-    public function __invoke(array $record)
+    public function __invoke(array $record): arra
     {
         // return if the level is not high enough
         if ($record['level'] < $this->level) {
@@ -44,7 +40,7 @@ class MercurialProcessor
         return $record;
     }
 
-    private static function getMercurialInfo()
+    private static function getMercurialInfo(): array
     {
         if (self::$cache) {
             return self::$cache;

+ 1 - 5
src/Monolog/Processor/ProcessIdProcessor.php

@@ -18,11 +18,7 @@ namespace Monolog\Processor;
  */
 class ProcessIdProcessor
 {
-    /**
-     * @param  array $record
-     * @return array
-     */
-    public function __invoke(array $record)
+    public function __invoke(array $record): array
     {
         $record['extra']['process_id'] = getmypid();
 

+ 1 - 1
src/Monolog/Processor/PsrLogMessageProcessor.php

@@ -24,7 +24,7 @@ class PsrLogMessageProcessor
      * @param  array $record
      * @return array
      */
-    public function __invoke(array $record)
+    public function __invoke(array $record): array
     {
         if (false === strpos($record['message'], '{')) {
             return $record;

+ 1 - 1
src/Monolog/Processor/TagProcessor.php

@@ -35,7 +35,7 @@ class TagProcessor
         $this->tags = $tags;
     }
 
-    public function __invoke(array $record)
+    public function __invoke(array $record): array
     {
         $record['extra']['tags'] = $this->tags;
 

+ 3 - 3
src/Monolog/Processor/UidProcessor.php

@@ -20,7 +20,7 @@ class UidProcessor
 {
     private $uid;
 
-    public function __construct($length = 7)
+    public function __construct(int $length = 7)
     {
         if (!is_int($length) || $length > 32 || $length < 1) {
             throw new \InvalidArgumentException('The uid length must be an integer between 1 and 32');
@@ -29,7 +29,7 @@ class UidProcessor
         $this->uid = substr(hash('md5', uniqid('', true)), 0, $length);
     }
 
-    public function __invoke(array $record)
+    public function __invoke(array $record): array
     {
         $record['extra']['uid'] = $this->uid;
 
@@ -39,7 +39,7 @@ class UidProcessor
     /**
      * @return string
      */
-    public function getUid()
+    public function getUid(): string
     {
         return $this->uid;
     }

+ 3 - 16
src/Monolog/Processor/WebProcessor.php

@@ -65,11 +65,7 @@ class WebProcessor
         }
     }
 
-    /**
-     * @param  array $record
-     * @return array
-     */
-    public function __invoke(array $record)
+    public function __invoke(array $record): array
     {
         // skip processing if for some reason request data
         // is not present (CLI or wonky SAPIs)
@@ -82,23 +78,14 @@ class WebProcessor
         return $record;
     }
 
-    /**
-     * @param  string $extraName
-     * @param  string $serverName
-     * @return $this
-     */
-    public function addExtraField($extraName, $serverName)
+    public function addExtraField(string $extraName, string $serverName): self
     {
         $this->extraFields[$extraName] = $serverName;
 
         return $this;
     }
 
-    /**
-     * @param  array $extra
-     * @return array
-     */
-    private function appendExtraFields(array $extra)
+    private function appendExtraFields(array $extra): array
     {
         foreach ($this->extraFields as $extraName => $serverName) {
             $extra[$extraName] = isset($this->serverData[$serverName]) ? $this->serverData[$serverName] : null;

+ 2 - 2
src/Monolog/Registry.php

@@ -74,9 +74,9 @@ class Registry
             $index = array_search($logger, self::$loggers, true);
 
             return false !== $index;
-        } else {
-            return isset(self::$loggers[$logger]);
         }
+
+        return isset(self::$loggers[$logger]);
     }
 
     /**