diff --git a/README.md b/README.md index 902fc81..f256a2c 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,8 @@ It is an easy way to make sure that everyone has to check if they have (not) rec ```php namespace PetrKnap\Optional; +Optional::setLogger(new \Psr\Log\NullLogger()); + $optionalString = OptionalString::of('value'); echo $optionalString->isPresent() ? $optionalString->get() : 'empty'; diff --git a/composer.json b/composer.json index 4b9f68a..aadaa33 100755 --- a/composer.json +++ b/composer.json @@ -28,7 +28,8 @@ "license": "LGPL-3.0-or-later", "name": "petrknap/optional", "require": { - "php": ">=8.1" + "php": ">=8.1", + "psr/log": "^2.0" }, "require-dev": { "nunomaduro/phpinsights": "^2.11", diff --git a/src/AbstractOptional.php b/src/AbstractOptional.php index acf1995..dae4ec9 100644 --- a/src/AbstractOptional.php +++ b/src/AbstractOptional.php @@ -19,6 +19,8 @@ */ abstract class AbstractOptional { + use LoggerTrait; + private bool|null $wasPresent = null; /** @@ -109,10 +111,7 @@ public function flatMap(callable $mapper): self public function get(): mixed { if ($this->wasPresent === null) { - trigger_error( - 'Call `isPresent()` before accessing the value.', - error_level: E_USER_NOTICE, - ); + self::logNotice('Call `isPresent()` before accessing the value.'); } return $this->orElseThrow(static fn (): Exception\NoSuchElement => new Exception\NoSuchElement()); } diff --git a/src/LoggerTrait.php b/src/LoggerTrait.php new file mode 100644 index 0000000..c38ab83 --- /dev/null +++ b/src/LoggerTrait.php @@ -0,0 +1,29 @@ +notice($message); + } + } +} diff --git a/src/Optional.php b/src/Optional.php index bef2471..9d38012 100644 --- a/src/Optional.php +++ b/src/Optional.php @@ -17,10 +17,7 @@ { /* abstract */ protected static function isSupported(mixed $value): bool { - trigger_error( - static::class . ' does not check the type of value.', - error_level: E_USER_NOTICE, - ); + self::logNotice(static::class . ' does not check the type of value.'); return true; } } diff --git a/src/OptionalObject.php b/src/OptionalObject.php index a9ec9f0..90187f0 100644 --- a/src/OptionalObject.php +++ b/src/OptionalObject.php @@ -17,10 +17,7 @@ { /* abstract */ protected static function getObjectClassName(): string { - trigger_error( - static::class . ' does not check the instance of object.', - error_level: E_USER_NOTICE, - ); + self::logNotice(static::class . ' does not check the instance of object.'); /** @var class-string */ return ''; } diff --git a/src/OptionalResource.php b/src/OptionalResource.php index 574c588..ef03d57 100644 --- a/src/OptionalResource.php +++ b/src/OptionalResource.php @@ -12,10 +12,7 @@ { /* abstract */ protected static function getResourceType(): string { - trigger_error( - static::class . ' does not check the type of resource.', - error_level: E_USER_NOTICE, - ); + self::logNotice(static::class . ' does not check the type of resource.'); /** @var non-empty-string */ return ''; } diff --git a/tests/OptionalTest.php b/tests/OptionalTest.php index df23d15..bd32889 100644 --- a/tests/OptionalTest.php +++ b/tests/OptionalTest.php @@ -8,12 +8,19 @@ use LogicException; use PHPUnit\Framework\Attributes\DataProvider; use PHPUnit\Framework\TestCase; +use Psr\Log\NullLogger; class OptionalTest extends TestCase { private const VALUE = 'value'; private const OTHER = 'other'; + public static function setUpBeforeClass(): void + { + Optional::setLogger(new NullLogger()); + parent::setUpBeforeClass(); + } + public function testMethodEmptyReturnsEmptyOptional(): void { self::assertFalse(Optional::empty()->isPresent());