1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-03 19:57:41 +02:00

Add a lot more scalar types and go strict in Logger

This commit is contained in:
Jordi Boggiano
2015-12-18 17:47:20 +00:00
parent c9aa0368bf
commit 6c424f851c
15 changed files with 67 additions and 75 deletions

View File

@@ -36,7 +36,7 @@ abstract class AbstractHandler extends Handler
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function isHandling(array $record) public function isHandling(array $record): bool
{ {
return $record['level'] >= $this->level; return $record['level'] >= $this->level;
} }
@@ -47,7 +47,7 @@ abstract class AbstractHandler extends Handler
* @param int|string $level Level or level name * @param int|string $level Level or level name
* @return self * @return self
*/ */
public function setLevel($level) public function setLevel($level): self
{ {
$this->level = Logger::toMonologLevel($level); $this->level = Logger::toMonologLevel($level);
@@ -59,7 +59,7 @@ abstract class AbstractHandler extends Handler
* *
* @return int * @return int
*/ */
public function getLevel() public function getLevel(): int
{ {
return $this->level; return $this->level;
} }
@@ -71,7 +71,7 @@ abstract class AbstractHandler extends Handler
* false means that bubbling is not permitted. * false means that bubbling is not permitted.
* @return self * @return self
*/ */
public function setBubble($bubble) public function setBubble(bool $bubble): self
{ {
$this->bubble = $bubble; $this->bubble = $bubble;
@@ -84,7 +84,7 @@ abstract class AbstractHandler extends Handler
* @return Boolean true means that this handler allows bubbling. * @return Boolean true means that this handler allows bubbling.
* false means that bubbling is not permitted. * false means that bubbling is not permitted.
*/ */
public function getBubble() public function getBubble(): bool
{ {
return $this->bubble; return $this->bubble;
} }

View File

@@ -27,7 +27,7 @@ abstract class AbstractProcessingHandler extends AbstractHandler implements Proc
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function handle(array $record) public function handle(array $record): bool
{ {
if (!$this->isHandling($record)) { if (!$this->isHandling($record)) {
return false; return false;

View File

@@ -50,7 +50,7 @@ class BufferHandler extends AbstractHandler implements ProcessableHandlerInterfa
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function handle(array $record) public function handle(array $record): bool
{ {
if ($record['level'] < $this->level) { if ($record['level'] < $this->level) {
return false; return false;

View File

@@ -66,7 +66,7 @@ class FilterHandler extends Handler implements ProcessableHandlerInterface
/** /**
* @return array * @return array
*/ */
public function getAcceptedLevels() public function getAcceptedLevels(): array
{ {
return array_flip($this->acceptedLevels); return array_flip($this->acceptedLevels);
} }
@@ -92,7 +92,7 @@ class FilterHandler extends Handler implements ProcessableHandlerInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function isHandling(array $record) public function isHandling(array $record): bool
{ {
return isset($this->acceptedLevels[$record['level']]); return isset($this->acceptedLevels[$record['level']]);
} }
@@ -100,7 +100,7 @@ class FilterHandler extends Handler implements ProcessableHandlerInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function handle(array $record) public function handle(array $record): bool
{ {
if (!$this->isHandling($record)) { if (!$this->isHandling($record)) {
return false; return false;

View File

@@ -76,7 +76,7 @@ class FingersCrossedHandler extends Handler implements ProcessableHandlerInterfa
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function isHandling(array $record) public function isHandling(array $record): bool
{ {
return true; return true;
} }
@@ -84,7 +84,7 @@ class FingersCrossedHandler extends Handler implements ProcessableHandlerInterfa
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function handle(array $record) public function handle(array $record): bool
{ {
if ($this->processors) { if ($this->processors) {
$record = $this->processRecord($record); $record = $this->processRecord($record);

View File

@@ -41,7 +41,7 @@ class GroupHandler extends Handler implements ProcessableHandlerInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function isHandling(array $record) public function isHandling(array $record): bool
{ {
foreach ($this->handlers as $handler) { foreach ($this->handlers as $handler) {
if ($handler->isHandling($record)) { if ($handler->isHandling($record)) {
@@ -55,7 +55,7 @@ class GroupHandler extends Handler implements ProcessableHandlerInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function handle(array $record) public function handle(array $record): bool
{ {
if ($this->processors) { if ($this->processors) {
$record = $this->processRecord($record); $record = $this->processRecord($record);

View File

@@ -31,7 +31,7 @@ interface HandlerInterface
* *
* @return Boolean * @return Boolean
*/ */
public function isHandling(array $record); public function isHandling(array $record): bool;
/** /**
* Handles a record. * Handles a record.
@@ -47,7 +47,7 @@ interface HandlerInterface
* @return Boolean true means that this handler handled the record, and that bubbling is not permitted. * @return Boolean true means that this handler handled the record, and that bubbling is not permitted.
* false means the record was either not processed or that this handler allows bubbling. * false means the record was either not processed or that this handler allows bubbling.
*/ */
public function handle(array $record); public function handle(array $record): bool;
/** /**
* Handles a set of records at once. * Handles a set of records at once.

View File

@@ -36,7 +36,7 @@ class NullHandler extends Handler
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function isHandling(array $record) public function isHandling(array $record): bool
{ {
return $record['level'] >= $this->level; return $record['level'] >= $this->level;
} }
@@ -44,7 +44,7 @@ class NullHandler extends Handler
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function handle(array $record) public function handle(array $record): bool
{ {
if ($record['level'] < $this->level) { if ($record['level'] < $this->level) {
return false; return false;

View File

@@ -157,7 +157,7 @@ class PHPConsoleHandler extends AbstractProcessingHandler
return $this->options; return $this->options;
} }
public function handle(array $record) public function handle(array $record): bool
{ {
if ($this->options['enabled'] && $this->connector->isActiveClient()) { if ($this->options['enabled'] && $this->connector->isActiveClient()) {
return parent::handle($record); return parent::handle($record);

View File

@@ -43,7 +43,7 @@ class PsrHandler extends AbstractHandler
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function handle(array $record) public function handle(array $record): bool
{ {
if (!$this->isHandling($record)) { if (!$this->isHandling($record)) {
return false; return false;

View File

@@ -54,12 +54,12 @@ class SamplingHandler extends AbstractHandler implements ProcessableHandlerInter
} }
} }
public function isHandling(array $record) public function isHandling(array $record): bool
{ {
return $this->handler->isHandling($record); return $this->handler->isHandling($record);
} }
public function handle(array $record) public function handle(array $record): bool
{ {
if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) { if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) {
// The same logic as in FingersCrossedHandler // The same logic as in FingersCrossedHandler

View File

@@ -22,7 +22,7 @@ class WhatFailureGroupHandler extends GroupHandler
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function handle(array $record) public function handle(array $record): bool
{ {
if ($this->processors) { if ($this->processors) {
foreach ($this->processors as $processor) { foreach ($this->processors as $processor) {

View File

@@ -1,4 +1,5 @@
<?php <?php
declare(strict_types=1);
/* /*
* This file is part of the Monolog package. * This file is part of the Monolog package.
@@ -11,6 +12,7 @@
namespace Monolog; namespace Monolog;
use DateTimeZone;
use Monolog\Handler\HandlerInterface; use Monolog\Handler\HandlerInterface;
use Monolog\Handler\StreamHandler; use Monolog\Handler\StreamHandler;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
@@ -91,7 +93,7 @@ class Logger implements LoggerInterface
* *
* @var array $levels Logging levels * @var array $levels Logging levels
*/ */
protected static $levels = array( protected static $levels = [
self::DEBUG => 'DEBUG', self::DEBUG => 'DEBUG',
self::INFO => 'INFO', self::INFO => 'INFO',
self::NOTICE => 'NOTICE', self::NOTICE => 'NOTICE',
@@ -100,7 +102,7 @@ class Logger implements LoggerInterface
self::CRITICAL => 'CRITICAL', self::CRITICAL => 'CRITICAL',
self::ALERT => 'ALERT', self::ALERT => 'ALERT',
self::EMERGENCY => 'EMERGENCY', self::EMERGENCY => 'EMERGENCY',
); ];
/** /**
* @var string * @var string
@@ -129,7 +131,7 @@ class Logger implements LoggerInterface
protected $microsecondTimestamps = true; protected $microsecondTimestamps = true;
/** /**
* @var \DateTimeZone * @var DateTimeZone
*/ */
protected $timezone; protected $timezone;
@@ -139,18 +141,18 @@ class Logger implements LoggerInterface
* @param callable[] $processors Optional array of processors * @param callable[] $processors Optional array of processors
* @param DateTimeZone $timezone Optional timezone, if not provided date_default_timezone_get() will be used * @param DateTimeZone $timezone Optional timezone, if not provided date_default_timezone_get() will be used
*/ */
public function __construct($name, array $handlers = array(), array $processors = array(), $timezone = null) public function __construct(string $name, array $handlers = array(), array $processors = array(), DateTimeZone $timezone = null)
{ {
$this->name = $name; $this->name = $name;
$this->handlers = $handlers; $this->handlers = $handlers;
$this->processors = $processors; $this->processors = $processors;
$this->timezone = new \DateTimeZone($timezone ?: date_default_timezone_get() ?: 'UTC'); $this->timezone = new DateTimeZone($timezone ?: date_default_timezone_get() ?: 'UTC');
} }
/** /**
* @return string * @return string
*/ */
public function getName() public function getName(): string
{ {
return $this->name; return $this->name;
} }
@@ -161,7 +163,7 @@ class Logger implements LoggerInterface
* @param HandlerInterface $handler * @param HandlerInterface $handler
* @return $this * @return $this
*/ */
public function pushHandler(HandlerInterface $handler) public function pushHandler(HandlerInterface $handler): self
{ {
array_unshift($this->handlers, $handler); array_unshift($this->handlers, $handler);
@@ -173,7 +175,7 @@ class Logger implements LoggerInterface
* *
* @return HandlerInterface * @return HandlerInterface
*/ */
public function popHandler() public function popHandler(): HandlerInterface
{ {
if (!$this->handlers) { if (!$this->handlers) {
throw new \LogicException('You tried to pop from an empty handler stack.'); throw new \LogicException('You tried to pop from an empty handler stack.');
@@ -190,7 +192,7 @@ class Logger implements LoggerInterface
* @param HandlerInterface[] $handlers * @param HandlerInterface[] $handlers
* @return $this * @return $this
*/ */
public function setHandlers(array $handlers) public function setHandlers(array $handlers): self
{ {
$this->handlers = array(); $this->handlers = array();
foreach (array_reverse($handlers) as $handler) { foreach (array_reverse($handlers) as $handler) {
@@ -203,7 +205,7 @@ class Logger implements LoggerInterface
/** /**
* @return HandlerInterface[] * @return HandlerInterface[]
*/ */
public function getHandlers() public function getHandlers(): array
{ {
return $this->handlers; return $this->handlers;
} }
@@ -214,11 +216,8 @@ class Logger implements LoggerInterface
* @param callable $callback * @param callable $callback
* @return $this * @return $this
*/ */
public function pushProcessor($callback) public function pushProcessor(callable $callback): self
{ {
if (!is_callable($callback)) {
throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given');
}
array_unshift($this->processors, $callback); array_unshift($this->processors, $callback);
return $this; return $this;
@@ -229,7 +228,7 @@ class Logger implements LoggerInterface
* *
* @return callable * @return callable
*/ */
public function popProcessor() public function popProcessor(): callable
{ {
if (!$this->processors) { if (!$this->processors) {
throw new \LogicException('You tried to pop from an empty processor stack.'); throw new \LogicException('You tried to pop from an empty processor stack.');
@@ -241,7 +240,7 @@ class Logger implements LoggerInterface
/** /**
* @return callable[] * @return callable[]
*/ */
public function getProcessors() public function getProcessors(): array
{ {
return $this->processors; return $this->processors;
} }
@@ -259,9 +258,9 @@ class Logger implements LoggerInterface
* *
* @param bool $micro True to use microtime() to create timestamps * @param bool $micro True to use microtime() to create timestamps
*/ */
public function useMicrosecondTimestamps($micro) public function useMicrosecondTimestamps(bool $micro)
{ {
$this->microsecondTimestamps = (bool) $micro; $this->microsecondTimestamps = $micro;
} }
/** /**
@@ -272,7 +271,7 @@ class Logger implements LoggerInterface
* @param array $context The log context * @param array $context The log context
* @return Boolean Whether the record has been processed * @return Boolean Whether the record has been processed
*/ */
public function addRecord($level, $message, array $context = array()) public function addRecord(int $level, string $message, array $context = array()): bool
{ {
$levelName = static::getLevelName($level); $levelName = static::getLevelName($level);
@@ -295,12 +294,12 @@ class Logger implements LoggerInterface
if ($this->microsecondTimestamps) { if ($this->microsecondTimestamps) {
$ts = \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), $this->timezone); $ts = \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true)), $this->timezone);
} else { } else {
$ts = new \DateTime(null, $this->timezone); $ts = new \DateTime('', $this->timezone);
} }
$ts->setTimezone($this->timezone); $ts->setTimezone($this->timezone);
$record = array( $record = array(
'message' => (string) $message, 'message' => $message,
'context' => $context, 'context' => $context,
'level' => $level, 'level' => $level,
'level_name' => $levelName, 'level_name' => $levelName,
@@ -329,7 +328,7 @@ class Logger implements LoggerInterface
* *
* @return array Assoc array with human-readable level names => level codes. * @return array Assoc array with human-readable level names => level codes.
*/ */
public static function getLevels() public static function getLevels(): array
{ {
return array_flip(static::$levels); return array_flip(static::$levels);
} }
@@ -340,7 +339,7 @@ class Logger implements LoggerInterface
* @param int $level * @param int $level
* @return string * @return string
*/ */
public static function getLevelName($level) public static function getLevelName(int $level): string
{ {
if (!isset(static::$levels[$level])) { if (!isset(static::$levels[$level])) {
throw new InvalidArgumentException('Level "'.$level.'" is not defined, use one of: '.implode(', ', array_keys(static::$levels))); throw new InvalidArgumentException('Level "'.$level.'" is not defined, use one of: '.implode(', ', array_keys(static::$levels)));
@@ -355,10 +354,14 @@ class Logger implements LoggerInterface
* @param string|int Level number (monolog) or name (PSR-3) * @param string|int Level number (monolog) or name (PSR-3)
* @return int * @return int
*/ */
public static function toMonologLevel($level) public static function toMonologLevel($level): int
{ {
if (is_string($level) && defined(__CLASS__.'::'.strtoupper($level))) { if (is_string($level)) {
return constant(__CLASS__.'::'.strtoupper($level)); if (defined(__CLASS__.'::'.strtoupper($level))) {
return constant(__CLASS__.'::'.strtoupper($level));
}
throw new InvalidArgumentException('Level "'.$level.'" is not defined, use one of: '.implode(', ', array_keys(static::$levels)));
} }
return $level; return $level;
@@ -370,7 +373,7 @@ class Logger implements LoggerInterface
* @param int $level * @param int $level
* @return Boolean * @return Boolean
*/ */
public function isHandling($level) public function isHandling(int $level): bool
{ {
$record = array( $record = array(
'level' => $level, 'level' => $level,
@@ -399,7 +402,7 @@ class Logger implements LoggerInterface
{ {
$level = static::toMonologLevel($level); $level = static::toMonologLevel($level);
return $this->addRecord($level, $message, $context); return $this->addRecord($level, (string) $message, $context);
} }
/** /**
@@ -413,7 +416,7 @@ class Logger implements LoggerInterface
*/ */
public function debug($message, array $context = array()) public function debug($message, array $context = array())
{ {
return $this->addRecord(static::DEBUG, $message, $context); return $this->addRecord(static::DEBUG, (string) $message, $context);
} }
/** /**
@@ -427,7 +430,7 @@ class Logger implements LoggerInterface
*/ */
public function info($message, array $context = array()) public function info($message, array $context = array())
{ {
return $this->addRecord(static::INFO, $message, $context); return $this->addRecord(static::INFO, (string) $message, $context);
} }
/** /**
@@ -441,7 +444,7 @@ class Logger implements LoggerInterface
*/ */
public function notice($message, array $context = array()) public function notice($message, array $context = array())
{ {
return $this->addRecord(static::NOTICE, $message, $context); return $this->addRecord(static::NOTICE, (string) $message, $context);
} }
/** /**
@@ -455,7 +458,7 @@ class Logger implements LoggerInterface
*/ */
public function warning($message, array $context = array()) public function warning($message, array $context = array())
{ {
return $this->addRecord(static::WARNING, $message, $context); return $this->addRecord(static::WARNING, (string) $message, $context);
} }
/** /**
@@ -469,7 +472,7 @@ class Logger implements LoggerInterface
*/ */
public function error($message, array $context = array()) public function error($message, array $context = array())
{ {
return $this->addRecord(static::ERROR, $message, $context); return $this->addRecord(static::ERROR, (string) $message, $context);
} }
/** /**
@@ -483,7 +486,7 @@ class Logger implements LoggerInterface
*/ */
public function critical($message, array $context = array()) public function critical($message, array $context = array())
{ {
return $this->addRecord(static::CRITICAL, $message, $context); return $this->addRecord(static::CRITICAL, (string) $message, $context);
} }
/** /**
@@ -497,7 +500,7 @@ class Logger implements LoggerInterface
*/ */
public function alert($message, array $context = array()) public function alert($message, array $context = array())
{ {
return $this->addRecord(static::ALERT, $message, $context); return $this->addRecord(static::ALERT, (string) $message, $context);
} }
/** /**
@@ -511,15 +514,15 @@ class Logger implements LoggerInterface
*/ */
public function emergency($message, array $context = array()) public function emergency($message, array $context = array())
{ {
return $this->addRecord(static::EMERGENCY, $message, $context); return $this->addRecord(static::EMERGENCY, (string) $message, $context);
} }
/** /**
* Set the timezone to be used for the timestamp of log records. * Set the timezone to be used for the timestamp of log records.
* *
* @param \DateTimeZone $tz Timezone object * @param DateTimeZone $tz Timezone object
*/ */
public function setTimezone(\DateTimeZone $tz) public function setTimezone(DateTimeZone $tz)
{ {
$this->timezone = $tz; $this->timezone = $tz;
} }
@@ -527,9 +530,9 @@ class Logger implements LoggerInterface
/** /**
* Set the timezone to be used for the timestamp of log records. * Set the timezone to be used for the timestamp of log records.
* *
* @return \DateTimeZone * @return DateTimeZone
*/ */
public function getTimezone() public function getTimezone(): DateTimeZone
{ {
return $this->timezone; return $this->timezone;
} }

View File

@@ -112,7 +112,7 @@ class ExceptionTestHandler extends TestHandler
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function handle(array $record) public function handle(array $record): bool
{ {
parent::handle($record); parent::handle($record);

View File

@@ -182,17 +182,6 @@ class LoggerTest extends \PHPUnit_Framework_TestCase
$logger->popProcessor(); $logger->popProcessor();
} }
/**
* @covers Monolog\Logger::pushProcessor
* @expectedException InvalidArgumentException
*/
public function testPushProcessorWithNonCallable()
{
$logger = new Logger(__METHOD__);
$logger->pushProcessor(new \stdClass());
}
/** /**
* @covers Monolog\Logger::addRecord * @covers Monolog\Logger::addRecord
*/ */