Skip to content

Commit

Permalink
Merge pull request #14 from petrknap/logger
Browse files Browse the repository at this point in the history
Added logger support
  • Loading branch information
petrknap authored May 21, 2024
2 parents a94583f + c24db18 commit 586fa25
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 17 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 3 additions & 4 deletions src/AbstractOptional.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
abstract class AbstractOptional
{
use LoggerTrait;

private bool|null $wasPresent = null;

/**
Expand Down Expand Up @@ -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());
}
Expand Down
29 changes: 29 additions & 0 deletions src/LoggerTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace PetrKnap\Optional;

use Psr\Log\LoggerInterface;

trait LoggerTrait
{
private static ?LoggerInterface $logger = null;

public static function setLogger(LoggerInterface $logger): void
{
self::$logger = $logger;
}

protected static function logNotice(string $message): void
{
if (self::$logger === null) {
trigger_error(
$message,
error_level: E_USER_NOTICE,
);
} else {
self::$logger->notice($message);
}
}
}
5 changes: 1 addition & 4 deletions src/Optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
5 changes: 1 addition & 4 deletions src/OptionalObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}
Expand Down
5 changes: 1 addition & 4 deletions src/OptionalResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 '';
}
Expand Down
7 changes: 7 additions & 0 deletions tests/OptionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down

0 comments on commit 586fa25

Please sign in to comment.