2626 * }
2727 *
2828 * $version = parse_version("1.x");
29- * if ($version instanceof Result\Ok ) {
29+ * if ($version->isOk() ) {
3030 * echo "working with version: {$version->unwrap()}";
3131 * } else {
3232 * echo "error parsing header: {$version->unwrapErr()}";
7070 */
7171interface Result extends \IteratorAggregate
7272{
73+ /**
74+ * Returns `true` if the result is the `Ok` variant.
75+ *
76+ * # Examples
77+ *
78+ * ```
79+ * // @var Result<int,string> $x
80+ * $x = Result\ok(2);
81+ * self::assertTrue($x->isOk());
82+ * ```
83+ *
84+ * ```
85+ * // @var Result<int,string> $x
86+ * $x = Result\err(2);
87+ * self::assertFalse($x->isOk());
88+ * ```
89+ *
90+ * @psalm-assert-if-true Result\Ok $this
91+ * @psalm-assert-if-false Result\Err $this
92+ */
93+ public function isOk (): bool ;
94+
95+ /**
96+ * Returns `true` if the result is the `Err` variant.
97+ *
98+ * # Examples
99+ *
100+ * ```
101+ * // @var Result<int,string> $x
102+ * $x = Result\ok(2);
103+ * self::assertFalse($x->isErr());
104+ * ```
105+ *
106+ * ```
107+ * // @var Result<int,string> $x
108+ * $x = Result\err(2);
109+ * self::assertTrue($x->isErr());
110+ * ```
111+ *
112+ * @psalm-assert-if-true Result\Err $this
113+ * @psalm-assert-if-false Result\Ok $this
114+ */
115+ public function isErr (): bool ;
116+
117+ /**
118+ * Returns `true` if the result is the `Ok` variant and the value inside of it matches a predicate.
119+ *
120+ * # Examples
121+ *
122+ * ```
123+ * // @var Result<int,string> $x
124+ * $x = Result\ok(2);
125+ * self::assertTrue($x->isOkAnd(fn ($n) => $n < 5));
126+ * self::assertFalse($x->isOkAnd(fn ($n) => $n > 5));
127+ * ```
128+ *
129+ * ```
130+ * // @var Result<int,string> $x
131+ * $x = Result\err(2);
132+ * self::assertFalse($x->isOkAnd(fn ($n) => $n < 5));
133+ * self::assertFalse($x->isOkAnd(fn ($n) => $n > 5));
134+ * ```
135+ *
136+ * @param callable(T):bool $predicate
137+ * @psalm-assert-if-true Result\Ok $this
138+ */
139+ public function isOkAnd (callable $ predicate ): bool ;
140+
141+ /**
142+ * Returns `true` if the result is the `Err` variant and the value inside of it matches a predicate.
143+ *
144+ * # Examples
145+ *
146+ * ```
147+ * // @var Result<int,string> $x
148+ * $x = Result\err(2);
149+ * self::assertTrue($x->isErrAnd(fn ($n) => $n < 5));
150+ * self::assertFalse($x->isErrAnd(fn ($n) => $n > 5));
151+ * ```
152+ *
153+ * ```
154+ * // @var Result<int,string> $x
155+ * $x = Result\ok(2);
156+ * self::assertFalse($x->isErrAnd(fn ($n) => $n < 5));
157+ * self::assertFalse($x->isErrAnd(fn ($n) => $n > 5));
158+ * ```
159+ *
160+ * @param callable(E):bool $predicate
161+ * @psalm-assert-if-true Result\Err $this
162+ */
163+ public function isErrAnd (callable $ predicate ): bool ;
164+
73165 /**
74166 * Extract the contained value in an `Result<T, E>` when it is the `Ok` variant.
75167 * Throw a `RuntimeException` with a custum provided message if the `Result` is `Err`.
@@ -84,6 +176,7 @@ interface Result extends \IteratorAggregate
84176 *
85177 * @return T
86178 * @throws \RuntimeException
179+ * @psalm-assert Result\Ok $this
87180 */
88181 public function expect (string $ message ): mixed ;
89182
@@ -108,6 +201,7 @@ public function expect(string $message): mixed;
108201 *
109202 * @return T
110203 * @throws \Throwable
204+ * @psalm-assert Result\Ok $this
111205 */
112206 public function unwrap (): mixed ;
113207
@@ -125,6 +219,7 @@ public function unwrap(): mixed;
125219 *
126220 * @return E
127221 * @throws \RuntimeException
222+ * @psalm-assert Result\Err $this
128223 */
129224 public function unwrapErr (): mixed ;
130225
@@ -371,6 +466,8 @@ public function orElse(callable $right): Result;
371466 * $x = Result\err("Some error message");
372467 * self::assertFalse($x->contains(2));
373468 * ```
469+ *
470+ * @psalm-assert-if-true Result\Ok $this
374471 */
375472 public function contains (mixed $ value , bool $ strict = true ): bool ;
376473
@@ -392,6 +489,8 @@ public function contains(mixed $value, bool $strict = true): bool;
392489 * $x = Result\err("Some other error message");
393490 * self::assertFalse($x->containsErr("Some error message"));
394491 * ```
492+ *
493+ * @psalm-assert-if-true Result\Err $this
395494 */
396495 public function containsErr (mixed $ value , bool $ strict = true ): bool ;
397496
@@ -417,7 +516,7 @@ public function containsErr(mixed $value, bool $strict = true): bool;
417516 * foreach(explode(PHP_EOL, $input) as $num) {
418517 * $n = parseInt($num)->map(fn ($i) => $i * 2);
419518 *
420- * if ($n instanceof Result\Ok ) {
519+ * if ($n->isOk() ) {
421520 * echo $n->unwrap(), PHP_EOL;
422521 * }
423522 * }
0 commit comments