Skip to content
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
12 changes: 12 additions & 0 deletions src/Result.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,16 @@ public function ok(): Option;
* @return Option<E>
*/
public function err(): Option;

/**
* Applies one of two functions depending on whether the result is Ok or Err.
* Comparable to neverthrow's `match` method.
*
* @template U
* @template V
* @param Closure(T): U $okFn Function to apply if the Result is Ok
* @param Closure(E): V $errFn Function to apply if the Result is Err
* @return U|V The result of applying the appropriate function
*/
public function match(Closure $okFn, Closure $errFn): mixed;
}
6 changes: 6 additions & 0 deletions src/Result/Err.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,4 +245,10 @@ public function getIterator(): Traversable
{
return new EmptyIterator();
}

#[Override]
public function match(Closure $okFn, Closure $errFn): mixed
{
return $errFn($this->value);
}
}
6 changes: 6 additions & 0 deletions src/Result/Ok.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,4 +238,10 @@ public function getIterator(): Traversable
{
yield $this->value;
}

#[Override]
public function match(Closure $okFn, Closure $errFn): mixed
{
return $okFn($this->value);
}
}
93 changes: 93 additions & 0 deletions tests/Unit/Result/MatchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

namespace WizDevelop\PhpMonad\Tests\Unit\Result;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\TestDox;
use WizDevelop\PhpMonad\Result;
use WizDevelop\PhpMonad\Tests\Assert;
use WizDevelop\PhpMonad\Tests\TestCase;

#[TestDox('Result - MatchTest')]
#[CoversClass(Result::class)]
final class MatchTest extends TestCase
{
/**
* @return Result<int, string>
*/
public function createResult(): Result
{
return Result\ok(42);
}

/**
* @template T
* @template E
* @template U
* @param Result<T, E> $result
* @param U $okValue Value to return when Result is Ok
* @param U $errValue Value to return when Result is Err
* @param U $expected Expected outcome
* @param array<T|E> $expectedCalls Values that should be passed to the callbacks
*/
#[Test]
#[TestDox('match test')]
#[DataProvider('matchMatrix')]
public function match(
Result $result,
mixed $okValue,
mixed $errValue,
mixed $expected,
array $expectedCalls
): void {
$calls = [];

$actual = $result->match(
static function (mixed $value) use ($okValue, &$calls): mixed {
$calls[] = $value;

return $okValue;
},
static function (mixed $value) use ($errValue, &$calls): mixed {
$calls[] = $value;

return $errValue;
}
);

Assert::assertSame($expected, $actual);
Assert::assertSame($expectedCalls, $calls);
}

/**
* @return iterable<array{
* Result\Ok<int>|Result\Err<string>,
* string,
* string,
* string,
* array<int|string>
* }>
*/
public static function matchMatrix(): iterable
{
yield 'ok' => [
Result\ok(42),
'success',
'failure',
'success',
[42],
];

yield 'err' => [
Result\err('error'),
'success',
'failure',
'failure',
['error'],
];
}
}
Loading