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()` (#466)
  • Loading branch information
simPod committed Apr 18, 2024
1 parent 9762298 commit d211454
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
Expand Up @@ -115,6 +115,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 @@ -126,6 +128,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
@@ -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<int, 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 d211454

Please sign in to comment.