Skip to content

Commit

Permalink
feat: narrow ResultInterface to concrete implementation after calling…
Browse files Browse the repository at this point in the history
… `isSucceeded()` and `isFailed()`
  • Loading branch information
simPod committed Apr 18, 2024
1 parent c34e6c2 commit 6106ad5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Psl/Result/ResultInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function getThrowable(): Throwable;
* @return bool - `true` if the operation succeeded; `false` otherwise
*
* @psalm-mutation-free
*
* @psalm-assert-if-true Success<T> $this
*/
public function isSucceeded(): bool;

Expand All @@ -64,6 +66,8 @@ public function isSucceeded(): bool;
* @return bool - `true` if the operation failed; `false` otherwise
*
* @psalm-mutation-free
*
* @psalm-assert-if-true Failure<T> $this
*/
public function isFailed(): bool;

Expand Down
42 changes: 42 additions & 0 deletions tests/static-analysis/Result/result_interface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace Psl\Tests\StaticAnalysis\Iter;

use Exception;
use Psl\Result\Failure;
use Psl\Result\ResultInterface;
use Psl\Result\Success;

/**
* @param ResultInterface<int> $result
*
* @throws Exception
*
* @return Failure<mixed, Exception>
*/
function return_failure(ResultInterface $result): Failure
{
if ($result->isFailed()) {
return $result;
}

throw new Exception();
}

/**
* @param ResultInterface<int> $result
*
* @throws Exception
*
* @return Success<int>
*/
function return_success(ResultInterface $result): Success
{
if ($result->isSucceeded()) {
return $result;
}

throw new Exception();
}

0 comments on commit 6106ad5

Please sign in to comment.