From 66a3a4ae635fe0c2e81d05c778d81363a875ac34 Mon Sep 17 00:00:00 2001 From: Takuma Kajikawa Date: Fri, 8 Aug 2025 14:04:54 +0900 Subject: [PATCH] feat: improve PHPStan type annotations for Result interface - Add @phpstan-assert-if-false annotations to isOk() and isErr() - Use conditional return types for unwrap(), unwrapErr(), and unwrapOr() - These improvements enable better type inference and reduce false positives This change makes the Result type more closely aligned with Rust's type safety --- src/Result.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Result.php b/src/Result.php index 8aa24d0..a9a0d83 100644 --- a/src/Result.php +++ b/src/Result.php @@ -16,6 +16,7 @@ interface Result * 結果が成功(Ok)の場合に true を返します. * * @phpstan-assert-if-true Ok $this + * @phpstan-assert-if-false Err $this * * @return bool */ @@ -34,6 +35,7 @@ public function isOkAnd(callable $fn): bool; * 結果が失敗(Err)の場合に true を返します. * * @phpstan-assert-if-true Err $this + * @phpstan-assert-if-false Ok $this * * @return bool */ @@ -51,14 +53,14 @@ public function isErrAnd(callable $fn): bool; /** * 成功値を返します。失敗の場合は例外を投げます. * - * @return T + * @return ($this is Ok ? T : never) */ public function unwrap(): mixed; /** * エラー値を返します。成功の場合は例外を投げます. * - * @return E + * @return ($this is Err ? E : never) */ public function unwrapErr(): mixed; @@ -67,7 +69,7 @@ public function unwrapErr(): mixed; * * @template U * @param U $default - * @return T|U + * @return ($this is Ok ? T : U) */ public function unwrapOr(mixed $default): mixed;