From b6b455ed1150567eebe41adef25cc1e58e981531 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 17 Jul 2018 17:19:56 +0100 Subject: [PATCH 1/5] Add scalar hints and return types to the SlackRecord handler. --- src/Monolog/Handler/Slack/SlackRecord.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Monolog/Handler/Slack/SlackRecord.php b/src/Monolog/Handler/Slack/SlackRecord.php index 90286e88..91413e5f 100755 --- a/src/Monolog/Handler/Slack/SlackRecord.php +++ b/src/Monolog/Handler/Slack/SlackRecord.php @@ -47,7 +47,7 @@ class SlackRecord /** * User icon e.g. 'ghost', 'http://example.com/user.png' - * @var string + * @var string|null */ private $userIcon; @@ -85,7 +85,7 @@ class SlackRecord */ private $normalizerFormatter; - public function __construct($channel = null, $username = null, $useAttachment = true, $userIcon = null, $useShortAttachment = false, $includeContextAndExtra = false, array $excludeFields = array(), FormatterInterface $formatter = null) + public function __construct(?string $channel = null, ?string $username = null, bool $useAttachment = true, ?string $userIcon = null, bool $useShortAttachment = false, bool $includeContextAndExtra = false, array $excludeFields = array(), FormatterInterface $formatter = null) { $this->channel = $channel; $this->username = $username; @@ -101,7 +101,7 @@ class SlackRecord } } - public function getSlackData(array $record) + public function getSlackData(array $record): array { $dataArray = array(); $record = $this->excludeFields($record); @@ -181,7 +181,7 @@ class SlackRecord * @param int $level * @return string */ - public function getAttachmentColor($level) + public function getAttachmentColor(int $level): string { switch (true) { case $level >= Logger::ERROR: @@ -202,7 +202,7 @@ class SlackRecord * * @return string */ - public function stringify($fields) + public function stringify(array $fields): string { $normalized = $this->normalizerFormatter->format($fields); $prettyPrintFlag = defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : 128; @@ -220,7 +220,7 @@ class SlackRecord * * @param FormatterInterface $formatter */ - public function setFormatter(FormatterInterface $formatter) + public function setFormatter(FormatterInterface $formatter): void { $this->formatter = $formatter; } @@ -233,7 +233,7 @@ class SlackRecord * * @return array */ - private function generateAttachmentField($title, $value) + private function generateAttachmentField(string $title, $value): array { $value = is_array($value) ? sprintf('```%s```', $this->stringify($value)) @@ -253,7 +253,7 @@ class SlackRecord * * @return array */ - private function generateAttachmentFields(array $data) + private function generateAttachmentFields(array $data): array { $fields = array(); foreach ($this->normalizerFormatter->format($data) as $key => $value) { @@ -270,7 +270,7 @@ class SlackRecord * * @return array */ - private function excludeFields(array $record) + private function excludeFields(array $record): array { foreach ($this->excludeFields as $field) { $keys = explode('.', $field); From 10bef97dea14cee40beb9864740d9c3cb4c6e1e3 Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 17 Jul 2018 17:25:17 +0100 Subject: [PATCH 2/5] Add scalar hints and return types to the ActivationStrategyInterface interface and its implementations. --- .../Handler/FingersCrossed/ActivationStrategyInterface.php | 2 +- .../Handler/FingersCrossed/ChannelLevelActivationStrategy.php | 2 +- .../Handler/FingersCrossed/ErrorLevelActivationStrategy.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Monolog/Handler/FingersCrossed/ActivationStrategyInterface.php b/src/Monolog/Handler/FingersCrossed/ActivationStrategyInterface.php index b73854ad..3b765969 100644 --- a/src/Monolog/Handler/FingersCrossed/ActivationStrategyInterface.php +++ b/src/Monolog/Handler/FingersCrossed/ActivationStrategyInterface.php @@ -24,5 +24,5 @@ interface ActivationStrategyInterface * @param array $record * @return bool */ - public function isHandlerActivated(array $record); + public function isHandlerActivated(array $record): bool; } diff --git a/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php b/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php index 63a14cb6..8d0e4a2e 100644 --- a/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php +++ b/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php @@ -48,7 +48,7 @@ class ChannelLevelActivationStrategy implements ActivationStrategyInterface $this->channelToActionLevel = array_map('Monolog\Logger::toMonologLevel', $channelToActionLevel); } - public function isHandlerActivated(array $record) + public function isHandlerActivated(array $record): bool { if (isset($this->channelToActionLevel[$record['channel']])) { return $record['level'] >= $this->channelToActionLevel[$record['channel']]; diff --git a/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php b/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php index d0ebd840..5a2a7991 100644 --- a/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php +++ b/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php @@ -27,7 +27,7 @@ class ErrorLevelActivationStrategy implements ActivationStrategyInterface $this->actionLevel = Logger::toMonologLevel($actionLevel); } - public function isHandlerActivated(array $record) + public function isHandlerActivated(array $record): bool { return $record['level'] >= $this->actionLevel; } From dc003d137ebed0e70124e23861e76f2fe9481d0a Mon Sep 17 00:00:00 2001 From: Kat Date: Tue, 17 Jul 2018 17:32:33 +0100 Subject: [PATCH 3/5] Add docblocks and return type to the FingersCrossed strategy classes. --- .../FingersCrossed/ChannelLevelActivationStrategy.php | 9 ++++++++- .../FingersCrossed/ErrorLevelActivationStrategy.php | 10 ++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php b/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php index 8d0e4a2e..56681c11 100644 --- a/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php +++ b/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php @@ -35,14 +35,21 @@ use Monolog\Logger; */ class ChannelLevelActivationStrategy implements ActivationStrategyInterface { + /** + * @var string|int + */ private $defaultActionLevel; + + /** + * @var array + */ private $channelToActionLevel; /** * @param int $defaultActionLevel The default action level to be used if the record's category doesn't match any * @param array $channelToActionLevel An array that maps channel names to action levels. */ - public function __construct($defaultActionLevel, $channelToActionLevel = []) + public function __construct($defaultActionLevel, array $channelToActionLevel = []) { $this->defaultActionLevel = Logger::toMonologLevel($defaultActionLevel); $this->channelToActionLevel = array_map('Monolog\Logger::toMonologLevel', $channelToActionLevel); diff --git a/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php b/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php index 5a2a7991..7c0eb6ae 100644 --- a/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php +++ b/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php @@ -20,13 +20,23 @@ use Monolog\Logger; */ class ErrorLevelActivationStrategy implements ActivationStrategyInterface { + /** + * @var string|int + */ private $actionLevel; + /** + * @param string|int $actionLevel + */ public function __construct($actionLevel) { $this->actionLevel = Logger::toMonologLevel($actionLevel); } + /** + * @param array $record + * @return bool + */ public function isHandlerActivated(array $record): bool { return $record['level'] >= $this->actionLevel; From 228a763397ac4cb0e90349a5afbd6602cb64467a Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 3 Nov 2018 18:02:37 +0100 Subject: [PATCH 4/5] After Logger::toMonologLevel string|int is normalized to int --- .../FingersCrossed/ChannelLevelActivationStrategy.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php b/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php index 56681c11..31e06a27 100644 --- a/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php +++ b/src/Monolog/Handler/FingersCrossed/ChannelLevelActivationStrategy.php @@ -36,7 +36,7 @@ use Monolog\Logger; class ChannelLevelActivationStrategy implements ActivationStrategyInterface { /** - * @var string|int + * @var int */ private $defaultActionLevel; @@ -46,8 +46,8 @@ class ChannelLevelActivationStrategy implements ActivationStrategyInterface private $channelToActionLevel; /** - * @param int $defaultActionLevel The default action level to be used if the record's category doesn't match any - * @param array $channelToActionLevel An array that maps channel names to action levels. + * @param string|int $defaultActionLevel The default action level to be used if the record's category doesn't match any + * @param array $channelToActionLevel An array that maps channel names to action levels. */ public function __construct($defaultActionLevel, array $channelToActionLevel = []) { From bcc4ecfe33c5364de8624cc674b9cc72ce49708f Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 3 Nov 2018 18:03:58 +0100 Subject: [PATCH 5/5] Removed useless type hints and make sure level is only an int after normalization --- .../Handler/FingersCrossed/ErrorLevelActivationStrategy.php | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php b/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php index 7c0eb6ae..54132b20 100644 --- a/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php +++ b/src/Monolog/Handler/FingersCrossed/ErrorLevelActivationStrategy.php @@ -21,7 +21,7 @@ use Monolog\Logger; class ErrorLevelActivationStrategy implements ActivationStrategyInterface { /** - * @var string|int + * @var int */ private $actionLevel; @@ -33,10 +33,6 @@ class ErrorLevelActivationStrategy implements ActivationStrategyInterface $this->actionLevel = Logger::toMonologLevel($actionLevel); } - /** - * @param array $record - * @return bool - */ public function isHandlerActivated(array $record): bool { return $record['level'] >= $this->actionLevel;