Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: narrow ResultInterface to concrete implementation after calling isSucceeded() and isFailed() #466

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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<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();
}