Browse Source

Cleaning up

main
Dustin Wilson 1 year ago
parent
commit
09304240b0
  1. 10
      lib/Logger.php
  2. 14
      lib/Logger/IOException.php
  3. 19
      lib/Logger/InvalidArgumentException.php
  4. 20
      lib/Logger/RangeException.php
  5. 2
      lib/Logger/StreamHandler.php
  6. 10
      tests/cases/TestLogger.php

10
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.

14
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;
}
}
class IOException extends \RuntimeException {}

19
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);
}
}
class InvalidArgumentException extends \Psr\Log\InvalidArgumentException {}

20
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);
}
}
class RangeException extends \RangeException {}

2
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);

10
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!');
}

Loading…
Cancel
Save