Skip to content

Commit d211454

Browse files
authored
feat: narrow ResultInterface to concrete implementation after calling isSucceeded() and isFailed() (#466)
1 parent 9762298 commit d211454

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

src/Psl/Result/ResultInterface.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,8 @@ public function getThrowable(): Throwable;
115115
* @return bool - `true` if the operation succeeded; `false` otherwise
116116
*
117117
* @psalm-mutation-free
118+
*
119+
* @psalm-assert-if-true Success<T> $this
118120
*/
119121
public function isSucceeded(): bool;
120122

@@ -126,6 +128,8 @@ public function isSucceeded(): bool;
126128
* @return bool - `true` if the operation failed; `false` otherwise
127129
*
128130
* @psalm-mutation-free
131+
*
132+
* @psalm-assert-if-true Failure<T> $this
129133
*/
130134
public function isFailed(): bool;
131135

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Psl\Tests\StaticAnalysis\Iter;
6+
7+
use Exception;
8+
use Psl\Result\Failure;
9+
use Psl\Result\ResultInterface;
10+
use Psl\Result\Success;
11+
12+
/**
13+
* @param ResultInterface<int> $result
14+
*
15+
* @throws Exception
16+
*
17+
* @return Failure<int, Exception>
18+
*/
19+
function return_failure(ResultInterface $result): Failure
20+
{
21+
if ($result->isFailed()) {
22+
return $result;
23+
}
24+
25+
throw new Exception();
26+
}
27+
28+
/**
29+
* @param ResultInterface<int> $result
30+
*
31+
* @throws Exception
32+
*
33+
* @return Success<int>
34+
*/
35+
function return_success(ResultInterface $result): Success
36+
{
37+
if ($result->isSucceeded()) {
38+
return $result;
39+
}
40+
41+
throw new Exception();
42+
}

0 commit comments

Comments
 (0)