From 00fc85e99b467193c45ea9135ed1c860d62476f2 Mon Sep 17 00:00:00 2001 From: Dmitry Ivanov Date: Sun, 5 Jun 2022 14:51:08 +0300 Subject: [PATCH] Type hints and return types (and other small tweaks) --- integrations/guzzle.php | 15 ++-- src/Exceptions/ExceptionHandler.php | 55 +++--------- src/Exceptions/RuntimeException.php | 14 +-- src/Loggable.php | 89 ++++--------------- src/Loggable/FileChannel/FileChannel.php | 12 +-- src/Loggable/FileChannel/MonologFormatter.php | 16 +--- .../DatabaseChannel/DatabaseChannel.php | 20 ++--- .../MonologDatabaseHandler.php | 19 +--- .../EmailChannel/EmailChannel.php | 36 ++------ .../EmailChannel/MonologHtmlFormatter.php | 37 ++------ tests/Exceptions/ExceptionHandlerTest.php | 8 +- .../Loggable/FileChannel/FileChannelTest.php | 6 +- .../EmailChannel/MonologHtmlFormatterTest.php | 25 ++---- tests/LoggableTraitOnMysqlTest.php | 8 +- tests/LoggableTraitTest.php | 4 +- tests/TestCase.php | 33 ++----- .../Commands/ContextLoggingCommand.php | 6 +- .../DatabaseNotificationsCallbackCommand.php | 19 ++-- .../Commands/DatabaseNotificationsCommand.php | 10 +-- .../DatabaseNotificationsDisabledCommand.php | 10 +-- .../Commands/EmailNotificationsCommand.php | 20 ++--- ...EmailNotificationsDeduplicationCommand.php | 20 ++--- ...lNotificationsInvalidRecipientsCommand.php | 15 +--- .../app/Console/Commands/GenericCommand.php | 10 +-- .../Console/Commands/NamespacedCommand.php | 6 +- .../Commands/SeparatorLoggingCommand.php | 6 +- tests/fixture/app/Console/Kernel.php | 2 - tests/fixture/app/CustomNotification.php | 2 - 28 files changed, 122 insertions(+), 401 deletions(-) diff --git a/integrations/guzzle.php b/integrations/guzzle.php index 10e26e8..8ba0f74 100644 --- a/integrations/guzzle.php +++ b/integrations/guzzle.php @@ -1,6 +1,6 @@ getMethod(); + $method = $request->getMethod(); $uri = (string) $request->getUri(); $body = (string) $request->getBody(); @@ -80,6 +74,7 @@ function (ResponseInterface $response) use ($request, $logger, $type, $shouldLog } // Save the parsed body of response, so that it could be re-used instead of double decoding if (!empty($context)) { + /** @noinspection PhpUndefinedFieldInspection */ $response->iclParsedBody = $context; } } @@ -88,7 +83,7 @@ function (ResponseInterface $response) use ($request, $logger, $type, $shouldLog return $response; }, function ($reason) { - return rejection_for($reason); + return Create::rejectionFor($reason); } ); }; diff --git a/src/Exceptions/ExceptionHandler.php b/src/Exceptions/ExceptionHandler.php index c0428c0..5b7fe81 100644 --- a/src/Exceptions/ExceptionHandler.php +++ b/src/Exceptions/ExceptionHandler.php @@ -3,48 +3,32 @@ namespace Illuminated\Console\Exceptions; use Illuminate\Foundation\Exceptions\Handler; -use Psr\Log\LoggerInterface; +use Monolog\Logger; use Throwable; class ExceptionHandler extends Handler { /** * The logger instance. - * - * @var \Psr\Log\LoggerInterface */ - private $logger; + private Logger $logger; /** * Time when execution started. - * - * @var float */ - private $timeStarted; - - /** - * Time when execution finished. - * - * @var float - */ - private $timeFinished; + private float $timeStarted; /** * Reserved memory for the shutdown execution. * * @see https://github.com/dmitry-ivanov/laravel-console-logger/issues/4 - * - * @var string */ - protected $reservedMemory; + protected ?string $reservedMemory; /** * Initialize the exception handler. - * - * @param \Psr\Log\LoggerInterface $logger - * @return void */ - public function initialize(LoggerInterface $logger) + public function initialize(Logger $logger): void { $this->setLogger($logger); $this->registerShutdownFunction(); @@ -52,11 +36,8 @@ public function initialize(LoggerInterface $logger) /** * Set the logger. - * - * @param \Psr\Log\LoggerInterface $logger - * @return void */ - public function setLogger(LoggerInterface $logger) + public function setLogger(Logger $logger): void { $this->logger = $logger; } @@ -66,11 +47,8 @@ public function setLogger(LoggerInterface $logger) * * Note that this method doesn't decorate, but overwrite the parent method: * @see https://github.com/dmitry-ivanov/laravel-console-logger/pull/11 - * - * @param \Throwable $e - * @return void */ - public function report(Throwable $e) + public function report(Throwable $e): void { $context = [ 'code' => $e->getCode(), @@ -93,11 +71,8 @@ public function report(Throwable $e) /** * Add Sentry support. - * - * @param \Throwable $e - * @return void */ - private function addSentrySupport(Throwable $e) + private function addSentrySupport(Throwable $e): void { if (app()->bound('sentry') && $this->shouldReport($e)) { app('sentry')->captureException($e); @@ -106,10 +81,8 @@ private function addSentrySupport(Throwable $e) /** * Register the shutdown function. - * - * @return void */ - private function registerShutdownFunction() + private function registerShutdownFunction(): void { $this->timeStarted = microtime(true); $this->reservedMemory = str_repeat(' ', 20 * 1024); @@ -119,15 +92,13 @@ private function registerShutdownFunction() /** * Callback for the shutdown function. - * - * @return void */ - public function onShutdown() + public function onShutdown(): void { $this->reservedMemory = null; - $this->timeFinished = microtime(true); - $executionTime = round($this->timeFinished - $this->timeStarted, 3); + $timeFinished = microtime(true); + $executionTime = round($timeFinished - $this->timeStarted, 3); $this->logger->info("Execution time: {$executionTime} sec."); $memoryPeak = format_bytes(memory_get_peak_usage(true)); @@ -135,7 +106,7 @@ public function onShutdown() $this->logger->info('%separator%'); - $handlers = (array) $this->logger->getHandlers(); + $handlers = $this->logger->getHandlers(); foreach ($handlers as $handler) { $handler->close(); } diff --git a/src/Exceptions/RuntimeException.php b/src/Exceptions/RuntimeException.php index 7b370cf..cda2009 100644 --- a/src/Exceptions/RuntimeException.php +++ b/src/Exceptions/RuntimeException.php @@ -9,19 +9,11 @@ class RuntimeException extends SymfonyRuntimeException { /** * The context. - * - * @var array */ - private $context; + private array $context; /** * Create a new instance of the exception. - * - * @param string $message - * @param array $context - * @param int $code - * @param \Exception|null $previous - * @return void */ public function __construct(string $message = '', array $context = [], int $code = 0, Exception $previous = null) { @@ -32,10 +24,8 @@ public function __construct(string $message = '', array $context = [], int $code /** * Get the context. - * - * @return array */ - public function getContext() + public function getContext(): array { return $this->context; } diff --git a/src/Loggable.php b/src/Loggable.php index be52a64..c0815ad 100644 --- a/src/Loggable.php +++ b/src/Loggable.php @@ -20,31 +20,24 @@ trait Loggable /** * The logger. - * - * @var \Monolog\Logger */ - protected $icLogger; + protected Logger $icLogger; /** * Overwrite the console command initialization. - * - * @param \Symfony\Component\Console\Input\InputInterface $input - * @param \Symfony\Component\Console\Output\OutputInterface $output - * @return void */ - protected function initialize(InputInterface $input, OutputInterface $output) + protected function initialize(InputInterface $input, OutputInterface $output): void { $this->initializeLogging(); + /** @noinspection PhpMultipleClassDeclarationsInspection */ parent::initialize($input, $output); } /** * Initialize the logging. - * - * @return void */ - protected function initializeLogging() + protected function initializeLogging(): void { $this->initializeICLogger(); $this->initializeErrorHandling(); @@ -53,106 +46,72 @@ protected function initializeLogging() /** * Log debug message. - * - * @param string $message - * @param array $context - * @return void */ - protected function logDebug(string $message, array $context = []) + protected function logDebug(string $message, array $context = []): void { $this->icLogger->debug($message, $context); } /** * Log info message. - * - * @param string $message - * @param array $context - * @return void */ - protected function logInfo(string $message, array $context = []) + protected function logInfo(string $message, array $context = []): void { $this->icLogger->info($message, $context); } /** * Log notice message. - * - * @param string $message - * @param array $context - * @return void */ - protected function logNotice(string $message, array $context = []) + protected function logNotice(string $message, array $context = []): void { $this->icLogger->notice($message, $context); } /** * Log warning message. - * - * @param string $message - * @param array $context - * @return void */ - protected function logWarning(string $message, array $context = []) + protected function logWarning(string $message, array $context = []): void { $this->icLogger->warning($message, $context); } /** * Log error message. - * - * @param string $message - * @param array $context - * @return void */ - protected function logError(string $message, array $context = []) + protected function logError(string $message, array $context = []): void { $this->icLogger->error($message, $context); } /** * Log critical message. - * - * @param string $message - * @param array $context - * @return void */ - protected function logCritical(string $message, array $context = []) + protected function logCritical(string $message, array $context = []): void { $this->icLogger->critical($message, $context); } /** * Log alert message. - * - * @param string $message - * @param array $context - * @return void */ - protected function logAlert(string $message, array $context = []) + protected function logAlert(string $message, array $context = []): void { $this->icLogger->alert($message, $context); } /** * Log emergency message. - * - * @param string $message - * @param array $context - * @return void */ - protected function logEmergency(string $message, array $context = []) + protected function logEmergency(string $message, array $context = []): void { $this->icLogger->emergency($message, $context); } /** * Initialize the logger. - * - * @return void */ - private function initializeICLogger() + private function initializeICLogger(): void { app()->singleton('log.iclogger', function () { return new Logger('ICLogger', $this->getChannelHandlers()); @@ -163,22 +122,19 @@ private function initializeICLogger() /** * Initialize error handling. - * - * @return void */ - private function initializeErrorHandling() + private function initializeErrorHandling(): void { app()->singleton(ExceptionHandlerContract::class, ExceptionHandler::class); + /** @noinspection PhpPossiblePolymorphicInvocationInspection */ app(ExceptionHandlerContract::class)->initialize($this->icLogger); } /** * Log the command iteration's header information. - * - * @return void */ - private function logIterationHeaderInformation() + private function logIterationHeaderInformation(): void { $class = get_class($this); $host = gethostname(); @@ -198,10 +154,8 @@ private function logIterationHeaderInformation() /** * Get used channel handlers. - * - * @return array */ - private function getChannelHandlers() + private function getChannelHandlers(): array { $handlers = []; @@ -222,11 +176,8 @@ private function getChannelHandlers() /** * Check whether the given trait is "loggable channel" trait or not. - * - * @param string $name - * @return bool */ - private function isLoggableChannelTrait(string $name) + private function isLoggableChannelTrait(string $name): bool { return Str::startsWith($name, __NAMESPACE__ . '\Loggable') && Str::endsWith($name, 'Channel'); @@ -234,10 +185,8 @@ private function isLoggableChannelTrait(string $name) /** * Get the logger. - * - * @return \Monolog\Logger */ - protected function icLogger() + protected function icLogger(): Logger { return $this->icLogger; } diff --git a/src/Loggable/FileChannel/FileChannel.php b/src/Loggable/FileChannel/FileChannel.php index 9b65d57..8ff781b 100644 --- a/src/Loggable/FileChannel/FileChannel.php +++ b/src/Loggable/FileChannel/FileChannel.php @@ -9,9 +9,9 @@ trait FileChannel /** * Get the file channel handler. * - * @return \Monolog\Handler\RotatingFileHandler + * @noinspection PhpUnused */ - protected function getFileChannelHandler() + protected function getFileChannelHandler(): RotatingFileHandler { $handler = new RotatingFileHandler($this->getLogPath(), $this->getLogMaxFiles()); @@ -23,10 +23,8 @@ protected function getFileChannelHandler() /** * Get the log file path. - * - * @return string */ - protected function getLogPath() + protected function getLogPath(): string { $name = str_replace(':', '/', $this->getName()); @@ -35,10 +33,8 @@ protected function getLogPath() /** * Get the max number of stored log files. - * - * @return int */ - protected function getLogMaxFiles() + protected function getLogMaxFiles(): int { return 30; } diff --git a/src/Loggable/FileChannel/MonologFormatter.php b/src/Loggable/FileChannel/MonologFormatter.php index c50a161..853efcc 100644 --- a/src/Loggable/FileChannel/MonologFormatter.php +++ b/src/Loggable/FileChannel/MonologFormatter.php @@ -8,8 +8,6 @@ class MonologFormatter extends LineFormatter { /** * Create a new instance of the formatter. - * - * @return void */ public function __construct() { @@ -18,9 +16,6 @@ public function __construct() /** * Formats a log record. - * - * @param array $record - * @return mixed */ public function format(array $record): string { @@ -34,11 +29,8 @@ public function format(array $record): string /** * Convert the given data to string. - * - * @param mixed $data - * @return string */ - protected function convertToString($data): string + protected function convertToString(mixed $data): string { if (is_array($data)) { return get_dump($data); @@ -49,12 +41,8 @@ protected function convertToString($data): string /** * Normalize the given data. - * - * @param mixed $data - * @param int $depth - * @return mixed */ - protected function normalize($data, $depth = 0) + protected function normalize(mixed $data, int $depth = 0): mixed { if (is_iterable($data)) { return collect($data)->map(function ($item) { diff --git a/src/Loggable/Notifications/DatabaseChannel/DatabaseChannel.php b/src/Loggable/Notifications/DatabaseChannel/DatabaseChannel.php index d9be52e..ea7f472 100644 --- a/src/Loggable/Notifications/DatabaseChannel/DatabaseChannel.php +++ b/src/Loggable/Notifications/DatabaseChannel/DatabaseChannel.php @@ -8,10 +8,8 @@ trait DatabaseChannel { /** * Defines whether to use database notifications or not. - * - * @return bool */ - protected function useDatabaseNotifications() + protected function useDatabaseNotifications(): bool { return false; } @@ -19,9 +17,9 @@ protected function useDatabaseNotifications() /** * Get the database channel handler. * - * @return \Illuminated\Console\Loggable\Notifications\DatabaseChannel\MonologDatabaseHandler|false + * @noinspection PhpUnused */ - protected function getDatabaseChannelHandler() + protected function getDatabaseChannelHandler(): MonologDatabaseHandler|false { if (!$this->useDatabaseNotifications()) { return false; @@ -36,30 +34,24 @@ protected function getDatabaseChannelHandler() /** * Get the database notifications level. - * - * @return int */ - protected function getDatabaseNotificationsLevel() + protected function getDatabaseNotificationsLevel(): int { return Logger::NOTICE; } /** * Get the database notifications table name. - * - * @return string */ - protected function getDatabaseNotificationsTable() + protected function getDatabaseNotificationsTable(): string { return 'iclogger_notifications'; } /** * Get the database notifications callback. - * - * @return callable|null */ - protected function getDatabaseNotificationsCallback() + protected function getDatabaseNotificationsCallback(): ?callable { return null; } diff --git a/src/Loggable/Notifications/DatabaseChannel/MonologDatabaseHandler.php b/src/Loggable/Notifications/DatabaseChannel/MonologDatabaseHandler.php index 2140fa7..925933c 100644 --- a/src/Loggable/Notifications/DatabaseChannel/MonologDatabaseHandler.php +++ b/src/Loggable/Notifications/DatabaseChannel/MonologDatabaseHandler.php @@ -13,26 +13,16 @@ class MonologDatabaseHandler extends AbstractProcessingHandler { /** * The table name. - * - * @var string */ - private $table; + private string $table; /** * The callback. - * - * @var callable */ private $callback; /** * Create a new instance of the handler. - * - * @param string $table - * @param callable|null $callback - * @param int $level - * @param bool $bubble - * @return void */ public function __construct(string $table = 'iclogger_notifications', callable $callback = null, int $level = Logger::DEBUG, bool $bubble = true) { @@ -46,10 +36,8 @@ public function __construct(string $table = 'iclogger_notifications', callable $ /** * Guarantee that the database table for notifications exists. - * - * @return void */ - protected function guaranteeTableExists() + protected function guaranteeTableExists(): void { if (Schema::hasTable($this->table)) { return; @@ -68,9 +56,6 @@ protected function guaranteeTableExists() /** * Write the record down to the database. - * - * @param array $record - * @return void */ protected function write(array $record): void { diff --git a/src/Loggable/Notifications/EmailChannel/EmailChannel.php b/src/Loggable/Notifications/EmailChannel/EmailChannel.php index 9d057b9..1722e03 100644 --- a/src/Loggable/Notifications/EmailChannel/EmailChannel.php +++ b/src/Loggable/Notifications/EmailChannel/EmailChannel.php @@ -12,20 +12,16 @@ trait EmailChannel { /** * Defines whether to use email notifications or not. - * - * @return bool */ - protected function useEmailNotifications() + protected function useEmailNotifications(): bool { return true; } /** * Get the email channel handler. - * - * @return \Monolog\Handler\SymfonyMailerHandler|\Monolog\Handler\DeduplicationHandler|false */ - protected function getEmailChannelHandler() + protected function getEmailChannelHandler(): SymfonyMailerHandler|DeduplicationHandler|false { $recipients = $this->filterEmailNotificationsRecipients(); if (!$this->useEmailNotifications() || empty($recipients)) { @@ -55,20 +51,16 @@ protected function getEmailChannelHandler() /** * Get the email notifications level. - * - * @return int */ - protected function getEmailNotificationsLevel() + protected function getEmailNotificationsLevel(): int { return Logger::NOTICE; } /** * Get the email notifications recipients. - * - * @return array */ - protected function getEmailNotificationsRecipients() + protected function getEmailNotificationsRecipients(): array { return [ ['address' => null, 'name' => null], @@ -77,10 +69,8 @@ protected function getEmailNotificationsRecipients() /** * Get the email notifications subject. - * - * @return string */ - protected function getEmailNotificationsSubject() + protected function getEmailNotificationsSubject(): string { $env = Str::upper(app()->environment()); $name = $this->getName(); @@ -90,40 +80,32 @@ protected function getEmailNotificationsSubject() /** * Get the email notifications "from". - * - * @return array */ - protected function getEmailNotificationsFrom() + protected function getEmailNotificationsFrom(): array { return ['address' => 'no-reply@example.com', 'name' => 'ICLogger Notification']; } /** * Defines whether to use email notifications deduplication or not. - * - * @return bool */ - protected function useEmailNotificationsDeduplication() + protected function useEmailNotificationsDeduplication(): bool { return false; } /** * Get email notifications deduplication time in seconds. - * - * @return int */ - protected function getEmailNotificationsDeduplicationTime() + protected function getEmailNotificationsDeduplicationTime(): int { return 60; } /** * Filter email notifications recipients. - * - * @return array */ - private function filterEmailNotificationsRecipients() + private function filterEmailNotificationsRecipients(): array { return collect($this->getEmailNotificationsRecipients()) ->filter(function (array $recipient) { diff --git a/src/Loggable/Notifications/EmailChannel/MonologHtmlFormatter.php b/src/Loggable/Notifications/EmailChannel/MonologHtmlFormatter.php index 44a98e0..7cbbac8 100644 --- a/src/Loggable/Notifications/EmailChannel/MonologHtmlFormatter.php +++ b/src/Loggable/Notifications/EmailChannel/MonologHtmlFormatter.php @@ -9,8 +9,6 @@ class MonologHtmlFormatter extends HtmlFormatter { /** * Create a new instance of the formatter. - * - * @return void */ public function __construct() { @@ -19,15 +17,13 @@ public function __construct() /** * Formats a log record. - * - * @param array $record - * @return string */ public function format(array $record): string { $output = ''; $output .= ''; + /** @noinspection HtmlRequiredTitleElement */ $output .= ''; $output .= ''; $output .= $this->composeStyle($record); @@ -45,22 +41,16 @@ public function format(array $record): string /** * Get color for the given level. - * - * @param int $level - * @return string */ - public function getLevelColor(int $level) + public function getLevelColor(int $level): string { return $this->logLevels[$level]; } /** * Compose style for the given record. - * - * @param array $record - * @return string */ - protected function composeStyle(array $record) + protected function composeStyle(array $record): string { $level = $record['level']; $levelName = $record['level_name']; @@ -100,11 +90,8 @@ protected function composeStyle(array $record) /** * Compose title for the given record. - * - * @param array $record - * @return string */ - protected function composeTitle(array $record) + protected function composeTitle(array $record): string { $levelName = e($record['level_name']); $title = "

{$levelName}

"; @@ -123,11 +110,8 @@ protected function composeTitle(array $record) /** * Compose details for the given record. - * - * @param array $record - * @return string */ - protected function composeDetails(array $record) + protected function composeDetails(array $record): string { $details = ''; @@ -145,12 +129,8 @@ protected function composeDetails(array $record) /** * Compose the details row. - * - * @param string $header - * @param string $body - * @return string */ - protected function composeDetailsRow(string $header, string $body = ' ') + protected function composeDetailsRow(string $header, string $body = ' '): string { $header = e($header); $body = e($body); @@ -168,11 +148,8 @@ protected function composeDetailsRow(string $header, string $body = ' ') /** * Convert the given data to string. - * - * @param mixed $data - * @return string */ - protected function convertToString($data): string + protected function convertToString(mixed $data): string { if (is_array($data)) { return get_dump($data); diff --git a/tests/Exceptions/ExceptionHandlerTest.php b/tests/Exceptions/ExceptionHandlerTest.php index 1f3442b..27f5504 100644 --- a/tests/Exceptions/ExceptionHandlerTest.php +++ b/tests/Exceptions/ExceptionHandlerTest.php @@ -7,14 +7,14 @@ use Illuminated\Console\Exceptions\RuntimeException; use Illuminated\Console\Tests\TestCase; use Mockery; -use Psr\Log\LoggerInterface; +use Monolog\Logger; class ExceptionHandlerTest extends TestCase { /** @test */ public function it_logs_an_error_for_all_occurred_application_notices_warnings_errors_and_exceptions() { - $logger = spy(LoggerInterface::class); + $logger = spy(Logger::class); $handler = app(ExceptionHandler::class); $handler->setLogger($logger); @@ -37,7 +37,7 @@ public function it_supports_sentry() $exception = new Exception('Test exception', 111); $handler = app(ExceptionHandler::class); - $handler->setLogger(spy(LoggerInterface::class)); + $handler->setLogger(spy(Logger::class)); $handler->report($exception); $sentry->shouldHaveReceived('captureException', [$exception]); @@ -46,7 +46,7 @@ public function it_supports_sentry() /** @test */ public function it_supports_custom_runtime_exception_which_has_ability_to_set_optional_context() { - $logger = spy(LoggerInterface::class); + $logger = spy(Logger::class); $handler = app(ExceptionHandler::class); $handler->setLogger($logger); diff --git a/tests/Loggable/FileChannel/FileChannelTest.php b/tests/Loggable/FileChannel/FileChannelTest.php index acd11ed..74cceec 100644 --- a/tests/Loggable/FileChannel/FileChannelTest.php +++ b/tests/Loggable/FileChannel/FileChannelTest.php @@ -52,12 +52,8 @@ public function it_supports_separator_in_psr3_methods_which_is_transformed_to_11 /** * Create log files in the given path. - * - * @param string $path - * @param int $count - * @return void */ - private function createLogFiles(string $path, int $count) + private function createLogFiles(string $path, int $count): void { if (!File::isDirectory($path)) { File::makeDirectory($path); diff --git a/tests/Loggable/Notifications/EmailChannel/MonologHtmlFormatterTest.php b/tests/Loggable/Notifications/EmailChannel/MonologHtmlFormatterTest.php index 366a5ec..e10279d 100644 --- a/tests/Loggable/Notifications/EmailChannel/MonologHtmlFormatterTest.php +++ b/tests/Loggable/Notifications/EmailChannel/MonologHtmlFormatterTest.php @@ -105,13 +105,8 @@ public function it_has_no_environment_subtitle_for_production() /** * Generate the record. - * - * @param string $message - * @param int $level - * @param array|string $context - * @return array */ - protected function generateRecord(string $message, int $level, $context = []) + protected function generateRecord(string $message, int $level, array|string $context = []): array { return [ 'message' => $message, @@ -126,11 +121,8 @@ protected function generateRecord(string $message, int $level, $context = []) /** * Assert that formatter generates expected output. - * - * @param array $record - * @return void */ - protected function assertFormatterGeneratesExpectedOutput(array $record) + protected function assertFormatterGeneratesExpectedOutput(array $record): void { $expected = $this->composeExpectedOutput($record); $actual = (new MonologHtmlFormatter)->format($record); @@ -144,22 +136,16 @@ protected function assertFormatterGeneratesExpectedOutput(array $record) /** * Normalize the given output. - * - * @param string $output - * @return string */ - private function normalizeOutput(string $output) + private function normalizeOutput(string $output): string { - return preg_replace('!\s+!smi', '', $output); + return preg_replace('/\s+/m', '', $output); } /** * Compose expected output by the given record. - * - * @param array $record - * @return string */ - private function composeExpectedOutput(array $record) + private function composeExpectedOutput(array $record): string { $color = (new MonologHtmlFormatter)->getLevelColor($record['level']); @@ -183,6 +169,7 @@ private function composeExpectedOutput(array $record) "; } + /** @noinspection HtmlRequiredTitleElement */ return " diff --git a/tests/LoggableTraitOnMysqlTest.php b/tests/LoggableTraitOnMysqlTest.php index 15f9023..eb5ee43 100644 --- a/tests/LoggableTraitOnMysqlTest.php +++ b/tests/LoggableTraitOnMysqlTest.php @@ -6,16 +6,14 @@ use Illuminated\Console\Tests\App\Console\Commands\GenericCommand; use Mockery; use Monolog\Handler\AbstractProcessingHandler; -use Psr\Log\LoggerInterface; +use Monolog\Logger; class LoggableTraitOnMysqlTest extends TestCase { /** * Set up database. - * - * @return void */ - protected function setUpDatabase() + protected function setUpDatabase(): void { config([ 'database.default' => 'mysql', @@ -60,7 +58,7 @@ public function it_writes_to_log_file_mysql_specific_information_after_header() */ public function it_writes_to_log_file_information_footer_each_iteration_and_close_all_handlers_on_shutdown() { - $logger = spy(LoggerInterface::class); + $logger = spy(Logger::class); $logger->expects('getHandlers')->andReturn([ $processingHandler1 = spy(AbstractProcessingHandler::class), $processingHandler2 = spy(AbstractProcessingHandler::class), diff --git a/tests/LoggableTraitTest.php b/tests/LoggableTraitTest.php index e80c73e..90fdd80 100644 --- a/tests/LoggableTraitTest.php +++ b/tests/LoggableTraitTest.php @@ -6,7 +6,7 @@ use Illuminated\Console\Tests\App\Console\Commands\GenericCommand; use Mockery; use Monolog\Handler\AbstractProcessingHandler; -use Psr\Log\LoggerInterface; +use Monolog\Logger; class LoggableTraitTest extends TestCase { @@ -43,7 +43,7 @@ public function it_does_not_write_mysql_specific_information_for_non_mysql_conne */ public function it_writes_to_log_file_information_footer_each_iteration_and_close_all_handlers_on_shutdown() { - $logger = spy(LoggerInterface::class); + $logger = spy(Logger::class); $logger->expects('getHandlers')->andReturn([ $processingHandler1 = spy(AbstractProcessingHandler::class), $processingHandler2 = spy(AbstractProcessingHandler::class), diff --git a/tests/TestCase.php b/tests/TestCase.php index 165771d..cfa48bb 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -17,22 +17,16 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase /** * Indicates if the console output should be mocked. - * - * @var bool */ public $mockConsoleOutput = false; /** * The date used in tests. - * - * @var string */ - protected $date; + protected string $date; /** * Setup the test environment. - * - * @return void */ protected function setUp(): void { @@ -46,40 +40,32 @@ protected function setUp(): void /** * Set up the date used in tests. - * - * @return void */ - private function setUpDate() + private function setUpDate(): void { $this->date = date('Y-m-d'); } /** * Set up database. - * - * @return void */ - protected function setUpDatabase() + protected function setUpDatabase(): void { config(['database.default' => 'testing']); } /** * Set up "sendmail". - * - * @return void */ - protected function setUpSendmail() + protected function setUpSendmail(): void { config(['mail.sendmail' => '/usr/sbin/sendmail -bs']); } /** * Set up the storage. - * - * @return void */ - private function setUpStorage() + private function setUpStorage(): void { $this->app->useStoragePath(__DIR__ . '/fixture/storage'); } @@ -88,9 +74,8 @@ private function setUpStorage() * Resolve application Console Kernel implementation. * * @param \Illuminate\Foundation\Application $app - * @return void */ - protected function resolveApplicationConsoleKernel($app) + protected function resolveApplicationConsoleKernel($app): void { $app->singleton(KernelContract::class, Kernel::class); @@ -99,8 +84,6 @@ protected function resolveApplicationConsoleKernel($app) /** * Clean up the testing environment before the next test. - * - * @return void */ protected function tearDown(): void { @@ -111,10 +94,8 @@ protected function tearDown(): void /** * Clean up the logs directory. - * - * @return void */ - private function cleanLogsDirectory() + private function cleanLogsDirectory(): void { $objects = (new Finder)->in(storage_path('logs'))->depth(0); foreach ($objects as $object) { diff --git a/tests/fixture/app/Console/Commands/ContextLoggingCommand.php b/tests/fixture/app/Console/Commands/ContextLoggingCommand.php index fbe9abb..32fd6d4 100644 --- a/tests/fixture/app/Console/Commands/ContextLoggingCommand.php +++ b/tests/fixture/app/Console/Commands/ContextLoggingCommand.php @@ -11,17 +11,13 @@ class ContextLoggingCommand extends Command /** * The name and signature of the console command. - * - * @var string */ protected $signature = 'context-logging-command'; /** * Handle the command. - * - * @return void */ - public function handle() + public function handle(): void { $this->logInfo('Testing context!'); $this->logInfo('Some log with data.', [ diff --git a/tests/fixture/app/Console/Commands/DatabaseNotificationsCallbackCommand.php b/tests/fixture/app/Console/Commands/DatabaseNotificationsCallbackCommand.php index c07fa6d..7fc9158 100644 --- a/tests/fixture/app/Console/Commands/DatabaseNotificationsCallbackCommand.php +++ b/tests/fixture/app/Console/Commands/DatabaseNotificationsCallbackCommand.php @@ -12,39 +12,32 @@ class DatabaseNotificationsCallbackCommand extends Command /** * The name and signature of the console command. - * - * @var string */ protected $signature = 'database-notifications-callback-command'; /** * Defines whether to use database notifications or not. - * - * @return bool */ - protected function useDatabaseNotifications() + protected function useDatabaseNotifications(): bool { return true; } /** * Get the database notifications table name. - * - * @return string */ - protected function getDatabaseNotificationsTable() + protected function getDatabaseNotificationsTable(): string { return 'custom_notifications'; } /** * Get the database notifications callback. - * - * @return callable|null */ - protected function getDatabaseNotificationsCallback() + protected function getDatabaseNotificationsCallback(): ?callable { return function (array $record) { + /** @noinspection PhpUndefinedMethodInspection */ CustomNotification::create([ 'level' => $record['level'], 'level_name' => $record['level_name'], @@ -59,10 +52,8 @@ protected function getDatabaseNotificationsCallback() /** * Handle the command. - * - * @return void */ - public function handle() + public function handle(): void { $this->logDebug('Debug!', ['foo' => 'bar']); $this->logInfo('Info!', ['foo' => 'bar']); diff --git a/tests/fixture/app/Console/Commands/DatabaseNotificationsCommand.php b/tests/fixture/app/Console/Commands/DatabaseNotificationsCommand.php index 4252f16..fff9060 100644 --- a/tests/fixture/app/Console/Commands/DatabaseNotificationsCommand.php +++ b/tests/fixture/app/Console/Commands/DatabaseNotificationsCommand.php @@ -11,27 +11,21 @@ class DatabaseNotificationsCommand extends Command /** * The name and signature of the console command. - * - * @var string */ protected $signature = 'database-notifications-command'; /** * Defines whether to use database notifications or not. - * - * @return bool */ - protected function useDatabaseNotifications() + protected function useDatabaseNotifications(): bool { return true; } /** * Handle the command. - * - * @return void */ - public function handle() + public function handle(): void { $this->logDebug('Debug!', ['foo' => 'bar']); $this->logInfo('Info!', ['foo' => 'bar']); diff --git a/tests/fixture/app/Console/Commands/DatabaseNotificationsDisabledCommand.php b/tests/fixture/app/Console/Commands/DatabaseNotificationsDisabledCommand.php index 7dfbab7..71754b9 100644 --- a/tests/fixture/app/Console/Commands/DatabaseNotificationsDisabledCommand.php +++ b/tests/fixture/app/Console/Commands/DatabaseNotificationsDisabledCommand.php @@ -11,27 +11,21 @@ class DatabaseNotificationsDisabledCommand extends Command /** * The name and signature of the console command. - * - * @var string */ protected $signature = 'database-notifications-disabled-command'; /** * Defines whether to use database notifications or not. - * - * @return bool */ - protected function useDatabaseNotifications() + protected function useDatabaseNotifications(): bool { return false; } /** * Handle the command. - * - * @return void */ - public function handle() + public function handle(): void { $this->logDebug('Debug!', ['foo' => 'bar']); $this->logInfo('Info!', ['foo' => 'bar']); diff --git a/tests/fixture/app/Console/Commands/EmailNotificationsCommand.php b/tests/fixture/app/Console/Commands/EmailNotificationsCommand.php index 7141d93..7dc2074 100644 --- a/tests/fixture/app/Console/Commands/EmailNotificationsCommand.php +++ b/tests/fixture/app/Console/Commands/EmailNotificationsCommand.php @@ -4,6 +4,8 @@ use Illuminate\Console\Command; use Illuminated\Console\Loggable; +use Monolog\Handler\DeduplicationHandler; +use Monolog\Handler\SymfonyMailerHandler; class EmailNotificationsCommand extends Command { @@ -11,17 +13,13 @@ class EmailNotificationsCommand extends Command /** * The name and signature of the console command. - * - * @var string */ protected $signature = 'email-notifications-command'; /** * Get the email notifications recipients. - * - * @return array */ - protected function getEmailNotificationsRecipients() + protected function getEmailNotificationsRecipients(): array { return [ ['address' => 'john.doe@example.com', 'name' => 'John Doe'], @@ -31,30 +29,24 @@ protected function getEmailNotificationsRecipients() /** * Handle the command. - * - * @return void */ - public function handle() + public function handle(): void { $this->logInfo('Done!'); } /** * Create the email channel handler. - * - * @return \Monolog\Handler\SymfonyMailerHandler|\Monolog\Handler\DeduplicationHandler|false */ - public function createEmailChannelHandler() + public function createEmailChannelHandler(): SymfonyMailerHandler|DeduplicationHandler|false { return $this->getEmailChannelHandler(); } /** * Get the email channel handler. - * - * @return \Monolog\Handler\SymfonyMailerHandler|\Monolog\Handler\DeduplicationHandler|false */ - public function emailChannelHandler() + public function emailChannelHandler(): SymfonyMailerHandler|DeduplicationHandler|false { return last($this->icLogger()->getHandlers()); } diff --git a/tests/fixture/app/Console/Commands/EmailNotificationsDeduplicationCommand.php b/tests/fixture/app/Console/Commands/EmailNotificationsDeduplicationCommand.php index a1e5ab3..4a355b7 100644 --- a/tests/fixture/app/Console/Commands/EmailNotificationsDeduplicationCommand.php +++ b/tests/fixture/app/Console/Commands/EmailNotificationsDeduplicationCommand.php @@ -4,6 +4,8 @@ use Illuminate\Console\Command; use Illuminated\Console\Loggable; +use Monolog\Handler\DeduplicationHandler; +use Monolog\Handler\SymfonyMailerHandler; class EmailNotificationsDeduplicationCommand extends Command { @@ -11,17 +13,13 @@ class EmailNotificationsDeduplicationCommand extends Command /** * The name and signature of the console command. - * - * @var string */ protected $signature = 'email-notifications-deduplication-command'; /** * Get the email notifications recipients. - * - * @return array */ - protected function getEmailNotificationsRecipients() + protected function getEmailNotificationsRecipients(): array { return [ ['address' => 'john.doe@example.com', 'name' => 'John Doe'], @@ -31,30 +29,24 @@ protected function getEmailNotificationsRecipients() /** * Defines whether to use email notifications deduplication or not. - * - * @return bool */ - protected function useEmailNotificationsDeduplication() + protected function useEmailNotificationsDeduplication(): bool { return true; } /** * Handle the command. - * - * @return void */ - public function handle() + public function handle(): void { $this->logInfo('Done!'); } /** * Get the email channel handler. - * - * @return \Monolog\Handler\SymfonyMailerHandler|\Monolog\Handler\DeduplicationHandler|false */ - public function emailChannelHandler() + public function emailChannelHandler(): SymfonyMailerHandler|DeduplicationHandler|false { return last($this->icLogger()->getHandlers()); } diff --git a/tests/fixture/app/Console/Commands/EmailNotificationsInvalidRecipientsCommand.php b/tests/fixture/app/Console/Commands/EmailNotificationsInvalidRecipientsCommand.php index fd016fd..953bf49 100644 --- a/tests/fixture/app/Console/Commands/EmailNotificationsInvalidRecipientsCommand.php +++ b/tests/fixture/app/Console/Commands/EmailNotificationsInvalidRecipientsCommand.php @@ -4,6 +4,7 @@ use Illuminate\Console\Command; use Illuminated\Console\Loggable; +use Monolog\Handler\AbstractHandler; class EmailNotificationsInvalidRecipientsCommand extends Command { @@ -11,17 +12,13 @@ class EmailNotificationsInvalidRecipientsCommand extends Command /** * The name and signature of the console command. - * - * @var string */ protected $signature = 'email-notifications-invalid-recipients-command'; /** * Get the email notifications recipients. - * - * @return array */ - protected function getEmailNotificationsRecipients() + protected function getEmailNotificationsRecipients(): array { return [ ['address' => 'not_an_email', 'name' => 'John Doe'], @@ -33,20 +30,16 @@ protected function getEmailNotificationsRecipients() /** * Handle the command. - * - * @return void */ - public function handle() + public function handle(): void { $this->logInfo('Done!'); } /** * Get the email channel handler. - * - * @return \Monolog\Handler\SymfonyMailerHandler|\Monolog\Handler\DeduplicationHandler|false */ - public function emailChannelHandler() + public function emailChannelHandler(): AbstractHandler|false { return last($this->icLogger()->getHandlers()); } diff --git a/tests/fixture/app/Console/Commands/GenericCommand.php b/tests/fixture/app/Console/Commands/GenericCommand.php index cc572e8..e3b39a5 100644 --- a/tests/fixture/app/Console/Commands/GenericCommand.php +++ b/tests/fixture/app/Console/Commands/GenericCommand.php @@ -12,17 +12,13 @@ class GenericCommand extends Command /** * The name and signature of the console command. - * - * @var string */ protected $signature = 'generic'; /** * Handle the command. - * - * @return void */ - public function handle() + public function handle(): void { $this->logDebug('Debug!'); $this->logInfo('Info!'); @@ -36,10 +32,8 @@ public function handle() /** * Emulate the closing of the file handler. - * - * @return void */ - public function emulateFileHandlerClose() + public function emulateFileHandlerClose(): void { $this->icLogger()->popHandler()->close(); $this->icLogger()->pushHandler(new NullHandler); diff --git a/tests/fixture/app/Console/Commands/NamespacedCommand.php b/tests/fixture/app/Console/Commands/NamespacedCommand.php index 9d115e0..c38f565 100644 --- a/tests/fixture/app/Console/Commands/NamespacedCommand.php +++ b/tests/fixture/app/Console/Commands/NamespacedCommand.php @@ -11,17 +11,13 @@ class NamespacedCommand extends Command /** * The name and signature of the console command. - * - * @var string */ protected $signature = 'namespaced:command'; /** * Handle the command. - * - * @return void */ - public function handle() + public function handle(): void { $this->logInfo('Done!'); } diff --git a/tests/fixture/app/Console/Commands/SeparatorLoggingCommand.php b/tests/fixture/app/Console/Commands/SeparatorLoggingCommand.php index d359581..f02b12a 100644 --- a/tests/fixture/app/Console/Commands/SeparatorLoggingCommand.php +++ b/tests/fixture/app/Console/Commands/SeparatorLoggingCommand.php @@ -11,17 +11,13 @@ class SeparatorLoggingCommand extends Command /** * The name and signature of the console command. - * - * @var string */ protected $signature = 'separator-logging-command'; /** * Handle the command. - * - * @return void */ - public function handle() + public function handle(): void { $this->logInfo('Testing separator!'); $this->logInfo('%separator%'); diff --git a/tests/fixture/app/Console/Kernel.php b/tests/fixture/app/Console/Kernel.php index 7dce839..b251ea0 100644 --- a/tests/fixture/app/Console/Kernel.php +++ b/tests/fixture/app/Console/Kernel.php @@ -17,8 +17,6 @@ class Kernel extends \Orchestra\Testbench\Foundation\Console\Kernel { /** * The Artisan commands provided by your application. - * - * @var array */ protected $commands = [ GenericCommand::class, diff --git a/tests/fixture/app/CustomNotification.php b/tests/fixture/app/CustomNotification.php index 3e42c3d..f7a15b8 100644 --- a/tests/fixture/app/CustomNotification.php +++ b/tests/fixture/app/CustomNotification.php @@ -8,8 +8,6 @@ class CustomNotification extends Model { /** * The attributes that are mass assignable. - * - * @var array */ protected $fillable = [ 'level',