From ca824157bfcbb25b709be583fed246d165fa3f51 Mon Sep 17 00:00:00 2001 From: Petr Knap <8299754+petrknap@users.noreply.github.com> Date: Sat, 24 Oct 2026 10:04:03 +0200 Subject: [PATCH] refactor: improved getters --- src/Optional.php | 20 ++++--------- tests/JavaSe8/AlternativeException.php | 11 ++++++++ ...notherOption.php => AlternativeOption.php} | 2 +- tests/JavaSe8/OptionalTest.java | 28 +++++++++++++------ tests/JavaSe8/OptionalTestCase.php | 2 +- 5 files changed, 37 insertions(+), 26 deletions(-) create mode 100644 tests/JavaSe8/AlternativeException.php rename tests/JavaSe8/{AnotherOption.php => AlternativeOption.php} (96%) diff --git a/src/Optional.php b/src/Optional.php index 0b2c931..e6bb022 100644 --- a/src/Optional.php +++ b/src/Optional.php @@ -14,8 +14,6 @@ */ abstract class Optional implements JavaSe8\Optional { - private bool|null $wasPresent = null; - /** * @param T|null $value */ @@ -168,11 +166,11 @@ public function flatMap(callable $mapper): JavaSe8\Optional return $mapper($this->value); } + /** + * @see self::orElseThrow() - more explicit alternative + */ public function get(): mixed { - if ($this->wasPresent === null) { - self::triggerNotice('Call `isPresent()` before accessing the value.'); - } return $this->orElseThrow(); } @@ -190,7 +188,7 @@ public function ifPresent(callable $consumer, callable|null $else = null): void public function isPresent(): bool { - return $this->wasPresent = $this->value !== null; + return $this->value !== null; } /** @@ -230,7 +228,7 @@ public function orElseGet(callable $otherSupplier): mixed * * @return T * - * @throws E + * @throws ($exceptionSupplier is null ? Exception\CouldNotGetValueOfEmptyOptional : E) */ public function orElseThrow( null|string|callable $exceptionSupplier = null, @@ -263,12 +261,4 @@ public function orElseThrow( throw new InvalidArgumentException('Exception supplier must return ' . Throwable::class . '.'); }); } - - private static function triggerNotice(string $message): void - { - trigger_error( - $message, - error_level: E_USER_NOTICE, - ); - } } diff --git a/tests/JavaSe8/AlternativeException.php b/tests/JavaSe8/AlternativeException.php new file mode 100644 index 0000000..58d1079 --- /dev/null +++ b/tests/JavaSe8/AlternativeException.php @@ -0,0 +1,11 @@ +empty().equals(Optional.empty()) == true); + assert (Optional.empty().equals(Optional.empty()) == true); // wrong type assert (Optional.of(1).equals(Optional.of("1")) == false); } @@ -43,24 +43,31 @@ private static void testFilter() { } private static void testFlatMap() { - Optional $anotherOption, $thisOption; + Optional $alternativeOption, $thisOption; // present option returns mapped option - $anotherOption = Optional.empty(); assert (Optional.of(1).flatMap($v -> $anotherOption) == $anotherOption); + $alternativeOption = Optional.empty(); assert (Optional.of(1).flatMap($v -> $alternativeOption) == $alternativeOption); // empty option returns itself - $thisOption = Optional.empty(); assert ($thisOption.flatMap($v -> Optional.of(new Another())) == $thisOption); + $thisOption = Optional.empty(); assert ($thisOption.flatMap($v -> Optional.of(new Alternative())) == $thisOption); // empty option does not call mapper - Optional.empty().flatMap($v -> { assert (false); return Optional.empty(); }); + Optional.empty().flatMap($v -> { assert (false); return Optional.empty(); }); } private static void testGetters() { + // present option returns value assert (Optional.of(1).get() == 1); + assert (Optional.of(1).orElse(2) == 1); + assert (Optional.of(1).orElseGet(() -> 2) == 1); + assert (Optional.of(1).orElseThrow() == 1); + // empty option returns fallback assert (Optional.empty().orElse(1) == 1); assert (Optional.empty().orElseGet(() -> 1) == 1); - assert (Optional.of(1).orElseThrow() == 1); + // empty option throws + try { Optional.empty().get(); } catch (NoSuchElementException $e) { assert (true); } try { Optional.empty().orElseThrow(); } catch (NoSuchElementException $e) { assert (true); } - // to nullable - assert (Optional.of(1).orElse(null) == 1); + try { Optional.empty().orElseThrow(() -> new AlternativeException()); } catch (AlternativeException $e) { assert (true); } + // fallback can be null assert (Optional.empty().orElse(null) == null); + assert (Optional.empty().orElseGet(() -> null) == null); } private static void testIfPresent() { @@ -90,6 +97,9 @@ private static void testMap() { private static record Record() { } - private static class Another { + private static class Alternative { + } + + private static class AlternativeException extends RuntimeException { } } diff --git a/tests/JavaSe8/OptionalTestCase.php b/tests/JavaSe8/OptionalTestCase.php index 87da9d0..6862c85 100644 --- a/tests/JavaSe8/OptionalTestCase.php +++ b/tests/JavaSe8/OptionalTestCase.php @@ -32,7 +32,7 @@ final public static function dataMatchesJavaBehavior(): iterable if (!str_starts_with($javaLine, '*') && str_contains($javaLine, 'assert (')) { yield $javaLine => [$headers . strtr($javaLine, [ 'assert (' => '\\' . self::class . '::assertTrue(', - 'Optional.' => 'AnotherOption::', + 'Optional.' => 'AlternativeOption::', 'Optional.' => '\\' . static::getClassName() . '::', 'Record' => '\\' . stdClass::class, '' => '',