From dd0fa1902def1066ec6b9d9658da54ba845385cc Mon Sep 17 00:00:00 2001 From: kakiuchi-shigenao Date: Wed, 13 Aug 2025 19:43:53 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20PHPDoc=E3=81=AE=E5=9E=8B=E6=B3=A8?= =?UTF-8?q?=E9=87=88=E3=82=92=E4=BF=AE=E6=AD=A3=E3=81=97=E3=80=81=E4=B8=8D?= =?UTF-8?q?=E8=A6=81=E3=81=AA=E3=83=86=E3=82=B9=E3=83=88=E3=82=92=E5=89=8A?= =?UTF-8?q?=E9=99=A4=20Fixes=20#50?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- phpunit.xml.dist | 1 + src/Option/functions.php | 49 +++++++++++++++++++------------ tests/Unit/Option/OfTest.php | 20 ------------- tests/Unit/Result/CombineTest.php | 10 ------- 4 files changed, 31 insertions(+), 49 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 63199ef..c63000d 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -11,6 +11,7 @@ timeoutForSmallTests="1" timeoutForMediumTests="10" timeoutForLargeTests="60" + displayDetailsOnTestsThatTriggerWarnings="true" > diff --git a/src/Option/functions.php b/src/Option/functions.php index 77b1404..41204c8 100644 --- a/src/Option/functions.php +++ b/src/Option/functions.php @@ -36,16 +36,16 @@ function none(): Option\None * It will be a `Some` option containing `$value` if `$value` is different from `$noneValue` (default `null`) * * @template U - * @param U|null $value - * @return Option + * @template NoneValue + * + * @param U $value + * @param NoneValue|null $noneValue + * + * @return ($noneValue is null ? Option : Option) */ -function fromValue($value, mixed $noneValue = null, bool $strict = true): Option +function fromValue($value, mixed $noneValue = null): Option { - $same = $strict - ? ($value === $noneValue) - : ($value == $noneValue); - - return $same + return $value === $noneValue ? Option\none() : Option\some($value); } @@ -55,12 +55,16 @@ function fromValue($value, mixed $noneValue = null, bool $strict = true): Option * It will be a `Some` option containing the result if it is different from `$noneValue` (default `null`). * * @template U - * @param callable():U|null $callback - * @return Option + * @template NoneValue + * + * @param callable():U $callback + * @param NoneValue|null $noneValue + * + * @return ($noneValue is null ? Option : Option) */ -function of(callable $callback, mixed $noneValue = null, bool $strict = true): Option +function of(callable $callback, mixed $noneValue = null): Option { - return Option\fromValue($callback(), $noneValue, $strict); + return Option\fromValue($callback(), $noneValue); } /** @@ -68,20 +72,24 @@ function of(callable $callback, mixed $noneValue = null, bool $strict = true): O * but also return `Option\None` if it an exception matching $exceptionClass was thrown. * * @template U + * @template NoneValue * @template E of \Throwable - * @param callable():U|null $callback - * @param class-string $exceptionClass - * @return Option + * + * @param callable():U $callback + * @param NoneValue|null $noneValue + * @param class-string $exceptionClass + * + * @return ($noneValue is null ? Option : Option) + * * @throws Throwable */ function tryOf( callable $callback, mixed $noneValue = null, - bool $strict = true, string $exceptionClass = Exception::class, ): Option { try { - return Option\of($callback, $noneValue, $strict); + return Option\of($callback, $noneValue); } catch (Throwable $th) { if (is_a($th, $exceptionClass)) { return Option\none(); @@ -95,7 +103,8 @@ function tryOf( * Converts from `Option>` to `Option`. * * @template U - * @param Option> $option + * @param Option> $option + * * @return Option */ function flatten(Option $option): Option @@ -113,7 +122,9 @@ function flatten(Option $option): Option * * @template U * @template E - * @param Option> $option + * + * @param Option> $option + * * @return Result, E> */ function transpose(Option $option): Result diff --git a/tests/Unit/Option/OfTest.php b/tests/Unit/Option/OfTest.php index a26941e..f958bc1 100644 --- a/tests/Unit/Option/OfTest.php +++ b/tests/Unit/Option/OfTest.php @@ -59,26 +59,6 @@ public function tryOfDefaultToNull(): void Assert::assertEquals(Option\some(1), Option\tryOf(static fn () => 1)); } - #[Test] - #[TestDox('ofDefaultToStrict test')] - public function ofDefaultToStrict(): void - { - $o = (object)[]; - - Assert::assertEquals(Option\none(), Option\of(static fn () => $o, (object)[], strict: false)); - Assert::assertEquals($o, Option\of(static fn () => $o, (object)[])->unwrap()); - } - - #[Test] - #[TestDox('tryOfDefaultToStrict test')] - public function tryOfDefaultToStrict(): void - { - $o = (object)[]; - - Assert::assertEquals(Option\none(), Option\tryOf(static fn () => $o, (object)[], strict: false)); - Assert::assertEquals($o, Option\tryOf(static fn () => $o, (object)[])->unwrap()); - } - #[Test] #[TestDox('tryOfExeptions test')] public function tryOfExeptions(): void diff --git a/tests/Unit/Result/CombineTest.php b/tests/Unit/Result/CombineTest.php index 36749d7..41bf258 100644 --- a/tests/Unit/Result/CombineTest.php +++ b/tests/Unit/Result/CombineTest.php @@ -54,14 +54,4 @@ public function combineWithErrors(): void Assert::assertTrue($errors[0] === 'error1'); Assert::assertTrue($errors[1] === 'error2'); } - - #[Test] - #[TestDox('空の配列を渡した場合はOk(true)を返すcombineのテスト')] - public function combineEmpty(): void - { - $result = Result\combine(); - - Assert::assertTrue($result->isOk()); - Assert::assertTrue($result->unwrap()); - } }