Skip to content

Commit

Permalink
Add scalar types to processor/formatters and top level classes
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldaek committed Sep 25, 2016
1 parent 760dc44 commit 6e65862
Show file tree
Hide file tree
Showing 29 changed files with 130 additions and 196 deletions.
20 changes: 13 additions & 7 deletions src/Monolog/ErrorHandler.php
Expand Up @@ -55,7 +55,7 @@ public function __construct(LoggerInterface $logger)
* @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) {
Expand All @@ -71,16 +71,18 @@ public static function register(LoggerInterface $logger, $errorLevelMap = [], $e
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);
Expand All @@ -89,26 +91,30 @@ public function registerErrorHandler(array $levelMap = [], $callPrevious = true,
}

$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,
'Throwable' => LogLevel::ERROR,
];
}

protected function defaultErrorLevelMap()
protected function defaultErrorLevelMap(): array
{
return [
E_ERROR => LogLevel::CRITICAL,
Expand Down Expand Up @@ -200,7 +206,7 @@ public function handleFatalError()
}
}

private static function codeToString($code)
private static function codeToString($code): string
{
switch ($code) {
case E_ERROR:
Expand Down
3 changes: 3 additions & 0 deletions src/Monolog/Formatter/ChromePHPFormatter.php
Expand Up @@ -65,6 +65,9 @@ public function format(array $record)
];
}

/**
* {@inheritdoc}
*/
public function formatBatch(array $records)
{
$formatted = [];
Expand Down
19 changes: 4 additions & 15 deletions src/Monolog/Formatter/ElasticaFormatter.php
Expand Up @@ -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');
Expand All @@ -53,31 +53,20 @@ public function format(array $record)
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);
Expand Down
17 changes: 4 additions & 13 deletions src/Monolog/Formatter/FlowdockFormatter.php
Expand Up @@ -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;
Expand All @@ -41,7 +37,7 @@ public function __construct($source, $sourceEmail)
/**
* {@inheritdoc}
*/
public function format(array $record)
public function format(array $record): array
{
$tags = [
'#logs',
Expand Down Expand Up @@ -75,7 +71,7 @@ public function format(array $record)
/**
* {@inheritdoc}
*/
public function formatBatch(array $records)
public function formatBatch(array $records): array
{
$formatted = [];

Expand All @@ -86,12 +82,7 @@ public function formatBatch(array $records)
return $formatted;
}

/**
* @param string $message
*
* @return string
*/
public function getShortMessage($message)
public function getShortMessage(string $message): string
{
static $hasMbString;

Expand Down
10 changes: 5 additions & 5 deletions src/Monolog/Formatter/FluentdFormatter.php
Expand Up @@ -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) {
Expand All @@ -73,7 +73,7 @@ public function format(array $record)
return json_encode([$tag, $record['datetime']->getTimestamp(), $message]);
}

public function formatBatch(array $records)
public function formatBatch(array $records): string
{
$message = '';
foreach ($records as $record) {
Expand Down
4 changes: 2 additions & 2 deletions src/Monolog/Formatter/GelfMessageFormatter.php
Expand Up @@ -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');

Expand All @@ -66,7 +66,7 @@ public function __construct($systemName = null, $extraPrefix = null, $contextPre
/**
* {@inheritdoc}
*/
public function format(array $record)
public function format(array $record): Message
{
if (isset($record['context'])) {
$record['context'] = parent::format($record['context']);
Expand Down
12 changes: 6 additions & 6 deletions src/Monolog/Formatter/HtmlFormatter.php
Expand Up @@ -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);
}
Expand All @@ -52,7 +52,7 @@ public function __construct($dateFormat = null)
* @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) {
Expand All @@ -69,7 +69,7 @@ protected function addRow($th, $td = ' ', $escapeTd = true)
* @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');

Expand All @@ -82,7 +82,7 @@ protected function addTitle($title, $level)
* @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">';
Expand Down Expand Up @@ -116,7 +116,7 @@ public function format(array $record)
* @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) {
Expand All @@ -126,7 +126,7 @@ public function formatBatch(array $records)
return $message;
}

protected function convertToString($data)
protected function convertToString($data): string
{
if (null === $data || is_scalar($data)) {
return (string) $data;
Expand Down

0 comments on commit 6e65862

Please sign in to comment.