From 09304240b0297d433242ef9733ab31192e4bef55 Mon Sep 17 00:00:00 2001 From: Dustin Wilson Date: Wed, 29 Mar 2023 22:27:18 -0500 Subject: [PATCH] Cleaning up --- lib/Logger.php | 10 ++++------ lib/Logger/IOException.php | 14 +------------- lib/Logger/InvalidArgumentException.php | 19 +------------------ lib/Logger/RangeException.php | 20 +------------------- lib/Logger/StreamHandler.php | 2 +- tests/cases/TestLogger.php | 10 ++++++---- 6 files changed, 14 insertions(+), 61 deletions(-) diff --git a/lib/Logger.php b/lib/Logger.php index 64ef237..b5f5091 100644 --- a/lib/Logger.php +++ b/lib/Logger.php @@ -9,13 +9,11 @@ declare(strict_types=1); namespace MensBeam; use MensBeam\Logger\{ Handler, + InvalidArgumentException, Level, StreamHandler }; -use Psr\Log\{ - InvalidArgumentException, - LoggerInterface -}; +use Psr\Log\LoggerInterface; class Logger implements LoggerInterface { @@ -115,7 +113,7 @@ class Logger implements LoggerInterface { /** * Logs with an arbitrary level. - * @throws \Psr\Log\InvalidArgumentException + * @throws \MensBeam\Logger\InvalidArgumentException */ public function log($level, string|\Stringable $message, array $context = []): void { // Because the interface won't allow limiting $level to just int|string this is @@ -123,7 +121,7 @@ class Logger implements LoggerInterface { if (!is_int($level) && !is_string($level)) { $type = gettype($level); $type = ($type === 'object') ? $level::class : $type; - throw new \TypeError(sprintf("Expected type 'int|string'. Found '%s'.", $type)); + throw new InvalidArgumentException(sprintf('Argument #1 ($level) must be of type int|string, %s given', $type)); } // If the level is a string convert it to a RFC5424 level integer. diff --git a/lib/Logger/IOException.php b/lib/Logger/IOException.php index e6378c1..528a1a1 100644 --- a/lib/Logger/IOException.php +++ b/lib/Logger/IOException.php @@ -8,16 +8,4 @@ declare(strict_types=1); namespace MensBeam\Logger; -class IOException extends \RuntimeException { - protected ?string $path; - - public function __construct(string $message, int $code = 0, \Throwable $previous = null, string $path = null) { - $this->path = $path; - - parent::__construct($message, $code, $previous); - } - - public function getPath(): ?string { - return $this->path; - } -} \ No newline at end of file +class IOException extends \RuntimeException {} \ No newline at end of file diff --git a/lib/Logger/InvalidArgumentException.php b/lib/Logger/InvalidArgumentException.php index 12ff5e4..60b7898 100644 --- a/lib/Logger/InvalidArgumentException.php +++ b/lib/Logger/InvalidArgumentException.php @@ -8,22 +8,5 @@ declare(strict_types=1); namespace MensBeam\Logger; -class InvalidArgumentException extends \InvalidArgumentException { - public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null) { - // Make output a bit more useful by making it show the file and line of where the constructor was called. - $backtrace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 5); - $b = null; - foreach ($backtrace as $k => $v) { - if ($v['function'] === '__construct' && $v['class'] === __NAMESPACE__ . '\Handler') { - $b = $backtrace[$k + 1] ?? null; - break; - } - } - if ($b !== null) { - $this->file = $b['file']; - $this->line = $b['line']; - } - parent::__construct($message, $code, $previous); - } -} \ No newline at end of file +class InvalidArgumentException extends \Psr\Log\InvalidArgumentException {} \ No newline at end of file diff --git a/lib/Logger/RangeException.php b/lib/Logger/RangeException.php index 10a45a5..2b7fd1a 100644 --- a/lib/Logger/RangeException.php +++ b/lib/Logger/RangeException.php @@ -8,22 +8,4 @@ declare(strict_types=1); namespace MensBeam\Logger; -class RangeException extends \RangeException { - public function __construct(string $message = '', int $code = 0, ?\Throwable $previous = null) { - // Make output a bit more useful by making it show the file and line of where the constructor was called. - $backtrace = debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS, 5); - $b = null; - foreach ($backtrace as $k => $v) { - if ($v['function'] === '__construct' && $v['class'] === __NAMESPACE__ . '\Handler') { - $b = $backtrace[$k + 1] ?? null; - break; - } - } - if ($b !== null) { - $this->file = $b['file']; - $this->line = $b['line']; - } - - parent::__construct($message, $code, $previous); - } -} \ No newline at end of file +class RangeException extends \RangeException {} \ No newline at end of file diff --git a/lib/Logger/StreamHandler.php b/lib/Logger/StreamHandler.php index 42fc10a..9f8aa36 100644 --- a/lib/Logger/StreamHandler.php +++ b/lib/Logger/StreamHandler.php @@ -58,7 +58,7 @@ class StreamHandler extends Handler { } else { $type = gettype($stream); $type = ($type === 'object') ? $stream::class : $stream; - throw new \TypeError(sprintf("Expected type 'resource|string'. Found '%s'", $type)); + throw new InvalidArgumentException(sprintf('Argument #1 ($stream) must be of type resource|string, %s given', $type)); } parent::__construct($levels, $options); diff --git a/tests/cases/TestLogger.php b/tests/cases/TestLogger.php index 166bda2..dcb548c 100644 --- a/tests/cases/TestLogger.php +++ b/tests/cases/TestLogger.php @@ -7,9 +7,11 @@ declare(strict_types=1); namespace MensBeam\Logger\Test; -use MensBeam\Logger, - MensBeam\Logger\StreamHandler, - Psr\Log\InvalidArgumentException; +use MensBeam\Logger; +use MensBeam\Logger\{ + InvalidArgumentException, + StreamHandler +}; /** @covers \MensBeam\Logger */ @@ -66,7 +68,7 @@ class TestLogger extends \PHPUnit\Framework\TestCase { public static function provideErrorTests(): iterable { $iterable = [ [ - \TypeError::class, + InvalidArgumentException::class, function (Logger $l): void { $l->log(3.14, 'Ook!'); }