Skip to content

Commit

Permalink
Merge pull request #24 from petrknap/soft-warnings
Browse files Browse the repository at this point in the history
`Logger` replaced by suppressible notices
  • Loading branch information
petrknap authored May 29, 2024
2 parents a69125c + 3e0f4df commit 55222f4
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 46 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@
"license": "LGPL-3.0-or-later",
"name": "petrknap/optional",
"require": {
"php": ">=8.1",
"psr/log": "^3.0|^2.0|^1.0"
"php": ">=8.1"
},
"require-dev": {
"nunomaduro/phpinsights": "^2.11",
Expand Down
39 changes: 0 additions & 39 deletions src/Logger.php

This file was deleted.

19 changes: 14 additions & 5 deletions src/Optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
*/
abstract class Optional implements JavaSe8\Optional
{
use Logger;

private bool|null $wasPresent = null;

/**
Expand All @@ -24,7 +22,7 @@ abstract class Optional implements JavaSe8\Optional
final protected function __construct(
protected readonly mixed $value,
) {
if ($this->value !== null && !static::isSupported($this->value)) {
if ($this->value !== null && !@static::isSupported($this->value)) {
throw new InvalidArgumentException('Value is not supported.');
}
}
Expand Down Expand Up @@ -68,7 +66,7 @@ public static function ofNullable(mixed $value): static
return new class ($value) extends Optional { # @phpstan-ignore-line
protected static function isSupported(mixed $value): bool
{
self::logNotice(Optional::class . ' does not check the type of value.');
TypedOptional::triggerNotice(Optional::class . ' does not check the type of value.');
return true;
}
};
Expand Down Expand Up @@ -113,7 +111,7 @@ public function flatMap(callable $mapper): self # @phpstan-ignore-line
public function get(): mixed
{
if ($this->wasPresent === null) {
self::logNotice('Call `isPresent()` before accessing the value.');
self::triggerNotice('Call `isPresent()` before accessing the value.');
}
return $this->orElseThrow();
}
Expand Down Expand Up @@ -171,4 +169,15 @@ public function orElseThrow(?callable $exceptionSupplier = null): mixed
* @param mixed $value not null
*/
abstract protected static function isSupported(mixed $value): bool;

/**
* @internal you should use {@see TypedOptional::triggerNotice()}
*/
private static function triggerNotice(string $message): void
{
trigger_error(
$message,
error_level: E_USER_NOTICE,
);
}
}
1 change: 1 addition & 0 deletions src/OptionalObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public static function ofNullable(mixed $value): static
return new class ($value) extends OptionalObject { # @phpstan-ignore-line
protected static function getInstanceOf(): string
{
TypedOptional::triggerNotice(OptionalObject::class . ' does not check the instance of object.');
/** @var class-string */
return self::ANY_INSTANCE_OF;
}
Expand Down
1 change: 1 addition & 0 deletions src/OptionalResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public static function ofNullable(mixed $value): static
return new class ($value) extends OptionalResource { # @phpstan-ignore-line
protected static function getResourceType(): string
{
TypedOptional::triggerNotice(OptionalResource::class . ' does not check the type of resource.');
/** @var non-empty-string */
return self::ANY_RESOURCE_TYPE;
}
Expand Down
25 changes: 25 additions & 0 deletions src/TypedOptional.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

final class TypedOptional
{
private static bool|null $enabledErrorTriggering = null;

/** @var array<class-string> must be iterated in reverse order */
private static array $typedOptionals = [
OptionalArray::class,
Expand Down Expand Up @@ -58,6 +60,29 @@ public static function register(string $typedOptionalClassName): void
if (!is_a($typedOptionalClassName, Optional::class, allow_string: true)) {
throw new Exception\CouldNotRegisterNonOptional($typedOptionalClassName);
}
if (self::$enabledErrorTriggering === null) {
self::enableErrorTriggering();
}
self::$typedOptionals[] = $typedOptionalClassName;
}

public static function enableErrorTriggering(): void
{
self::$enabledErrorTriggering = true;
}

public static function disableErrorTriggering(): void
{
self::$enabledErrorTriggering = false;
}

public static function triggerNotice(string $message): void
{
if (self::$enabledErrorTriggering === true) {
trigger_error(
$message,
error_level: E_USER_NOTICE,
);
}
}
}

0 comments on commit 55222f4

Please sign in to comment.