diff --git a/src/Psl/Option/Option.php b/src/Psl/Option/Option.php index e852bf59..e97b1725 100644 --- a/src/Psl/Option/Option.php +++ b/src/Psl/Option/Option.php @@ -206,19 +206,18 @@ public function contains(mixed $value): bool /** * Matches the contained option value with the provided closures and returns the result. * - * @template TNone - * @template TSome + * @template Ts * - * @param (Closure(): TNone) $none A closure to be called when the option is none. - * The closure must not accept any arguments and can return a value. - * Example: `fn() => 'Default value'` - * @param (Closure(T): TSome) $some A closure to be called when the option is some. - * The closure must accept the option value as its only argument and can return a value. - * Example: `fn($value) => $value + 10` + * @param (Closure(T): Ts) $some A closure to be called when the option is some. + * The closure must accept the option value as its only argument and can return a value. + * Example: `fn($value) => $value + 10` + * @param (Closure(): Ts) $none A closure to be called when the option is none. + * The closure must not accept any arguments and can return a value. + * Example: `fn() => 'Default value'` * - * @return TNone|TSome The result of calling the appropriate closure. + * @return Ts The result of calling the appropriate closure. */ - public function match(Closure $none, Closure $some): mixed + public function proceed(Closure $some, Closure $none): mixed { if ($this->option !== null) { return $some($this->option[0]); diff --git a/tests/static-analysis/Option/proceed.php b/tests/static-analysis/Option/proceed.php new file mode 100644 index 00000000..d853568f --- /dev/null +++ b/tests/static-analysis/Option/proceed.php @@ -0,0 +1,20 @@ + $option + * @return non-empty-string + */ + function test_proceed(Option\Option $option): string + { + return $option->proceed( + static fn (int $value) => "There is $value of them.", + static fn () => 'There are none.', + ); + } +} diff --git a/tests/unit/Option/NoneTest.php b/tests/unit/Option/NoneTest.php index 62e4a953..1888903b 100644 --- a/tests/unit/Option/NoneTest.php +++ b/tests/unit/Option/NoneTest.php @@ -84,7 +84,7 @@ public function testContains(): void static::assertFalse($option->contains(4)); } - public function testMatch(): void + public function testProceed(): void { $checked_divisor = static function (int $dividend, int $divisor): Option\Option { if ($divisor === 0) { @@ -95,9 +95,9 @@ public function testMatch(): void }; $try_division = static function (int $dividend, int $divisor) use ($checked_divisor): string { - return $checked_divisor($dividend, $divisor)->match( - none: static fn () => sprintf('%s / %s failed!', $dividend, $divisor), + return $checked_divisor($dividend, $divisor)->proceed( some: static fn ($value) => sprintf('%s / %s = %s', $dividend, $divisor, $value), + none: static fn() => sprintf('%s / %s failed!', $dividend, $divisor), ); }; diff --git a/tests/unit/Option/SomeTest.php b/tests/unit/Option/SomeTest.php index 6229cdb3..8bd10509 100644 --- a/tests/unit/Option/SomeTest.php +++ b/tests/unit/Option/SomeTest.php @@ -81,7 +81,7 @@ public function testContains(): void static::assertTrue($option->contains(2)); } - public function testMatch(): void + public function testProceed(): void { $checked_divisor = static function (int $dividend, int $divisor): Option\Option { if ($divisor === 0) { @@ -92,9 +92,9 @@ public function testMatch(): void }; $try_division = static function (int $dividend, int $divisor) use ($checked_divisor): string { - return $checked_divisor($dividend, $divisor)->match( - none: static fn () => sprintf('%s / %s failed!', $dividend, $divisor), + return $checked_divisor($dividend, $divisor)->proceed( some: static fn ($value) => sprintf('%s / %s = %s', $dividend, $divisor, $value), + none: static fn () => sprintf('%s / %s failed!', $dividend, $divisor), ); };