Compare commits

..

No commits in common. "main" and "v1" have entirely different histories.
main ... v1

7 changed files with 163 additions and 132 deletions

View File

@ -2,8 +2,6 @@
This project contains PHP utility classes whose functionality is inspired by their F# counterparts. This project contains PHP utility classes whose functionality is inspired by their F# counterparts.
The v2 series requires at least PHP 8.4. A similar API exists for PHP 8.2 - 8.3 in version 1 of this project; see its README for specifics.
## What It Provides ## What It Provides
This early-stage library currently provides two classes, both of which are designed to wrap values and indicate the state of the action that produced them. `Option<T>` represents a variable that may or may not have a value. `Result<TOK, TError>` represents the result of an action; the "ok" and "error" states both provide a value. This early-stage library currently provides two classes, both of which are designed to wrap values and indicate the state of the action that produced them. `Option<T>` represents a variable that may or may not have a value. `Result<TOK, TError>` represents the result of an action; the "ok" and "error" states both provide a value.
@ -13,12 +11,12 @@ This early-stage library currently provides two classes, both of which are desig
| **Creating** | `::Some(T)` for Some | `::OK(TOK)` for OK | | **Creating** | `::Some(T)` for Some | `::OK(TOK)` for OK |
| | `::None()` for None | `::Error(TError)` for Error | | | `::None()` for None | `::Error(TError)` for Error |
| | `::of($value)` _None if `null`_ | | | | `::of($value)` _None if `null`_ | |
| **Querying** | `->isSome: bool` | `->isOK: bool` | | **Querying** | `->isSome(): bool` | `->isOK(): bool` |
| | `->isNone: bool` | `->isError: bool` | | | `->isNone(): bool` | `->isError(): bool` |
| | `->contains(T, $strict = true): bool` | `->contains(TOK, $strict = true): bool` | | | `->contains(T, $strict = true): bool` | `->contains(TOK, $strict = true): bool` |
| | `->exists(callable(T): bool): bool` | `->exists(callable(TOK): bool): bool` | | | `->exists(callable(T): bool): bool` | `->exists(callable(TOK): bool): bool` |
| **Reading**<br> | `->value: T` | `->ok: TOK` | | **Reading**<br> | `->get(): T` | `->getOK(): TOK` |
| _all throw if called on missing value_ | | `->error: TError` | | _all throw if called on missing value_ | | `->getError(): TError` |
| **Transforming**<br> | `->map(callable(T): TMapped): Option<TMapped>` | `->map(callable(TOK): TMapped): Result<TMapped, TError>` | | **Transforming**<br> | `->map(callable(T): TMapped): Option<TMapped>` | `->map(callable(TOK): TMapped): Result<TMapped, TError>` |
| _all still `Option` or `Result`_ | | `->mapError(callable(TError): TMapped): Result<TOK, TMapped>` | | _all still `Option` or `Result`_ | | `->mapError(callable(TError): TMapped): Result<TOK, TMapped>` |
| **Iterating** | `->iter(callable(T): void): void` | `->iter(callable(TOK): void): void` | | **Iterating** | `->iter(callable(T): void): void` | `->iter(callable(TOK): void): void` |

View File

@ -17,7 +17,7 @@
"rss": "https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp.rss" "rss": "https://git.bitbadger.solutions/bit-badger/inspired-by-fsharp.rss"
}, },
"require": { "require": {
"php": ">=8.4" "php": "8.2 - 8.3"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^11", "phpunit/phpunit": "^11",

4
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "0cd12c9ee6159a96158d62691add3401", "content-hash": "e08185f1629cdda5af3af5405a707797",
"packages": [], "packages": [],
"packages-dev": [ "packages-dev": [
{ {
@ -1721,7 +1721,7 @@
"prefer-stable": false, "prefer-stable": false,
"prefer-lowest": false, "prefer-lowest": false,
"platform": { "platform": {
"php": ">=8.4" "php": "8.2 - 8.3"
}, },
"platform-dev": [], "platform-dev": [],
"plugin-api-version": "2.6.0" "plugin-api-version": "2.6.0"

View File

@ -25,38 +25,50 @@ use InvalidArgumentException;
* *
* @template T The type of value represented by this option * @template T The type of value represented by this option
*/ */
class Option readonly class Option
{ {
/** @var T|null $value The value for this option */ /** @var T|null $value The value for this option */
private mixed $val; private mixed $value;
/** /**
* @param T|null $value The possibly null value for this option * @param T|null $value The possibly null value for this option
*/ */
private function __construct(mixed $value = null) private function __construct(mixed $value = null)
{ {
$this->val = $value; $this->value = $value;
} }
/** /**
* @var T The value of this option (read-only) * Get the value of this option
* @throws InvalidArgumentException If called on a `None` option *
* @return T The value of the option
*/ */
public mixed $value { public function get(): mixed
get => match ($this->val) { {
null => throw new InvalidArgumentException('Cannot get the value of a None option'), return match (true) {
default => $this->val, $this->isSome() => $this->value,
default => throw new InvalidArgumentException('Cannot get the value of a None option'),
}; };
} }
/** @var bool True if the option is `None`, false if it is `Some` */ /**
public bool $isNone { * Does this option have a `None` value?
get => is_null($this->val); *
* @return bool True if the option is `None`, false if it is `Some`
*/
public function isNone(): bool
{
return is_null($this->value);
} }
/** @var bool True if the option is `Some`, false if it is `None` */ /**
public bool $isSome{ * Does this option have a `Some` value?
get => !$this->isNone; *
* @return bool True if the option is `Some`, false if it is `None`
*/
public function isSome(): bool
{
return !$this->isNone();
} }
/** /**
@ -67,7 +79,7 @@ class Option
*/ */
public function getOrDefault(mixed $default): mixed public function getOrDefault(mixed $default): mixed
{ {
return $this->val ?? $default; return $this->value ?? $default;
} }
/** /**
@ -79,19 +91,18 @@ class Option
*/ */
public function getOrCall(callable $f): mixed public function getOrCall(callable $f): mixed
{ {
return $this->val ?? $f(); return $this->value ?? $f();
} }
/** /**
* Get the value, or throw the exception using the given function * Get the value, or throw the
*
* @param callable(): Exception $exFunc A function to construct the exception to throw * @param callable(): Exception $exFunc A function to construct the exception to throw
* @return T The value of the option if `Some` * @return T The value of the option if `Some`
* @throws Exception If the option is `None` * @throws Exception If the option is `None`
*/ */
public function getOrThrow(callable $exFunc): mixed public function getOrThrow(callable $exFunc): mixed
{ {
return $this->val ?? throw $exFunc(); return $this->value ?? throw $exFunc();
} }
/** /**
@ -106,7 +117,7 @@ class Option
*/ */
public function bind(callable $f): Option public function bind(callable $f): Option
{ {
return $this->isNone ? $this : $f($this->val); return $this->isNone() ? $this : $f($this->get());
} }
/** /**
@ -119,8 +130,8 @@ class Option
public function contains(mixed $value, bool $strict = true): bool public function contains(mixed $value, bool $strict = true): bool
{ {
return match (true) { return match (true) {
$this->isNone => false, $this->isNone() => false,
default => $strict ? $this->val === $value : $this->val == $value, default => $strict ? $this->value === $value : $this->value == $value,
}; };
} }
@ -132,7 +143,7 @@ class Option
*/ */
public function exists(callable $f): bool public function exists(callable $f): bool
{ {
return $this->isSome ? $f($this->val) : false; return $this->isSome() ? $f($this->value) : false;
} }
/** /**
@ -144,7 +155,7 @@ class Option
*/ */
public function map(callable $f): self public function map(callable $f): self
{ {
return $this->isSome ? self::Some($f($this->val)) : $this; return $this->isSome() ? self::Some($f($this->get())) : $this;
} }
/** /**
@ -154,8 +165,8 @@ class Option
*/ */
public function iter(callable $f): void public function iter(callable $f): void
{ {
if ($this->isSome) { if ($this->isSome()) {
$f($this->val); $f($this->value);
} }
} }
@ -167,7 +178,7 @@ class Option
*/ */
public function filter(callable $f): self public function filter(callable $f): self
{ {
return $this->isNone || $this->exists($f) ? $this : self::None(); return $this->isNone() || $this->exists($f) ? $this : self::None();
} }
/** /**
@ -177,7 +188,7 @@ class Option
*/ */
public function unwrap(): mixed public function unwrap(): mixed
{ {
return $this->val; return $this->value;
} }
/** /**
@ -199,7 +210,7 @@ class Option
*/ */
public function toArray(): array public function toArray(): array
{ {
return $this->isSome ? [$this->val] : []; return $this->isSome() ? [$this->value] : [];
} }
/** /**
@ -210,8 +221,8 @@ class Option
public function toPhpOption(): mixed public function toPhpOption(): mixed
{ {
return match (true) { return match (true) {
$this->isNone && class_exists('PhpOption\None') => call_user_func('PhpOption\None::create'), $this->isNone() && class_exists('PhpOption\None') => call_user_func('PhpOption\None::create'),
class_exists('PhpOption\Some') => call_user_func('PhpOption\Some::create', $this->val), class_exists('PhpOption\Some') => call_user_func('PhpOption\Some::create', $this->value),
default => throw new Error('PhpOption types could not be found'), default => throw new Error('PhpOption types could not be found'),
}; };
} }

View File

@ -25,7 +25,7 @@ use InvalidArgumentException;
* @template TOK The type of the OK result * @template TOK The type of the OK result
* @template TError The type of the error result * @template TError The type of the error result
*/ */
class Result readonly class Result
{ {
/** @var Option<TOK> The OK value for this result */ /** @var Option<TOK> The OK value for this result */
private Option $okValue; private Option $okValue;
@ -45,24 +45,46 @@ class Result
$this->errorValue = Option::of($errorValue); $this->errorValue = Option::of($errorValue);
} }
/** @var TOK The OK value (will throw if result is not OK) */ /**
public mixed $ok { * Get the value for an `OK` result
get => $this->okValue->value; *
* @return TOK The OK value for this result
* @throws InvalidArgumentException If the result is an `Error` result
*/
public function getOK(): mixed
{
return $this->okValue->get();
} }
/** @var TError The error value (will throw if result is not Error) */ /**
public mixed $error { * Get the value for an `Error` result
get => $this->errorValue->value; *
* @return TError The error value for this result
* @throws InvalidArgumentException If the result is an `OK` result
*/
public function getError(): mixed
{
return $this->errorValue->get();
} }
/** @var bool True if the result is `OK`, false if it is `Error` */ /**
public bool $isOK { * Is this result `OK`?
get => $this->okValue->isSome; *
* @return bool True if the result is `OK`, false if it is `Error`
*/
public function isOK(): bool
{
return $this->okValue->isSome();
} }
/** @var bool True if the result is `Error`, false if it is `OK` */ /**
public bool $isError { * Is this result `Error`?
get => $this->errorValue->isSome; *
* @return bool True if the result is `Error`, false if it is `OK`
*/
public function isError(): bool
{
return $this->errorValue->isSome();
} }
/** /**
@ -78,7 +100,7 @@ class Result
*/ */
public function bind(callable $f): Result public function bind(callable $f): Result
{ {
return $this->isError ? $this : $f($this->ok); return $this->isError() ? $this : $f($this->getOK());
} }
/** /**
@ -91,7 +113,7 @@ class Result
public function contains(mixed $value, bool $strict = true): bool public function contains(mixed $value, bool $strict = true): bool
{ {
return match (true) { return match (true) {
$this->isError => false, $this->isError() => false,
default => $this->okValue->contains($value, $strict), default => $this->okValue->contains($value, $strict),
}; };
} }
@ -104,7 +126,7 @@ class Result
*/ */
public function exists(callable $f): bool public function exists(callable $f): bool
{ {
return $this->isOK ? $f($this->ok) : false; return $this->isOK() ? $f($this->okValue->get()) : false;
} }
/** /**
@ -116,7 +138,7 @@ class Result
*/ */
public function map(callable $f): self public function map(callable $f): self
{ {
return $this->isOK ? self::OK($f($this->ok)) : $this; return $this->isOK() ? self::OK($f($this->getOK())) : $this;
} }
/** /**
@ -128,7 +150,7 @@ class Result
*/ */
public function mapError(callable $f): self public function mapError(callable $f): self
{ {
return $this->isError ? self::Error($f($this->error)) : $this; return $this->isError() ? self::Error($f($this->getError())) : $this;
} }
/** /**
@ -138,8 +160,8 @@ class Result
*/ */
public function iter(callable $f): void public function iter(callable $f): void
{ {
if ($this->isOK) { if ($this->isOK()) {
$f($this->ok); $f($this->getOK());
} }
} }
@ -160,7 +182,7 @@ class Result
*/ */
public function toOption(): Option public function toOption(): Option
{ {
return $this->isOK ? Option::Some($this->ok) : Option::None(); return $this->isOK() ? Option::Some($this->getOK()) : Option::None();
} }
/** /**

View File

@ -20,43 +20,43 @@ use PHPUnit\Framework\TestCase;
*/ */
class OptionTest extends TestCase class OptionTest extends TestCase
{ {
#[TestDox('Value succeeds for Some')] #[TestDox('Get succeeds for Some')]
public function testValueSucceedsForSome(): void public function testGetSucceedsForSome(): void
{ {
$it = Option::Some(9); $it = Option::Some(9);
$this->assertTrue($it->isSome, 'The option should have been "Some"'); $this->assertTrue($it->isSome(), 'The option should have been "Some"');
$this->assertEquals(9, $it->value, 'The value was incorrect'); $this->assertEquals(9, $it->get(), 'The value was incorrect');
} }
#[TestDox('Value fails for None')] #[TestDox('Get fails for None')]
public function testValueFailsForNone(): void public function testGetFailsForNone(): void
{ {
$this->expectException(InvalidArgumentException::class); $this->expectException(InvalidArgumentException::class);
Option::None()->value; Option::None()->get();
} }
#[TestDox('IsNone succeeds with None')] #[TestDox('IsNone succeeds with None')]
public function testIsNoneSucceedsWithNone(): void public function testIsNoneSucceedsWithNone(): void
{ {
$this->assertTrue(Option::None()->isNone, '"None" should return true'); $this->assertTrue(Option::None()->isNone(), '"None" should return true');
} }
#[TestDox('IsNone succeeds with Some')] #[TestDox('IsNone succeeds with Some')]
public function testIsNoneSucceedsWithSome(): void public function testIsNoneSucceedsWithSome(): void
{ {
$this->assertFalse(Option::Some(8)->isNone, '"Some" should return false'); $this->assertFalse(Option::Some(8)->isNone(), '"Some" should return false');
} }
#[TestDox('IsSome succeeds with None')] #[TestDox('IsSome succeeds with None')]
public function testIsSomeSucceedsWithNone(): void public function testIsSomeSucceedsWithNone(): void
{ {
$this->assertFalse(Option::None()->isSome, '"None" should return false'); $this->assertFalse(Option::None()->isSome(), '"None" should return false');
} }
#[TestDox('IsSome succeeds with Some')] #[TestDox('IsSome succeeds with Some')]
public function testIsSomeSucceedsWithSome(): void public function testIsSomeSucceedsWithSome(): void
{ {
$this->assertTrue(Option::Some('boo')->isSome, '"Some" should return true'); $this->assertTrue(Option::Some('boo')->isSome(), '"Some" should return true');
} }
#[TestDox('GetOrDefault succeeds with None')] #[TestDox('GetOrDefault succeeds with None')]
@ -105,7 +105,7 @@ class OptionTest extends TestCase
{ {
$original = Option::None(); $original = Option::None();
$bound = $original->bind(fn($it) => Option::Some('value')); $bound = $original->bind(fn($it) => Option::Some('value'));
$this->assertTrue($bound->isNone, 'The option should have been None'); $this->assertTrue($bound->isNone(), 'The option should have been None');
$this->assertSame($original, $bound, 'The same None instance should have been returned'); $this->assertSame($original, $bound, 'The same None instance should have been returned');
} }
@ -113,15 +113,15 @@ class OptionTest extends TestCase
public function testBindSucceedsWithSomeAndSome(): void public function testBindSucceedsWithSomeAndSome(): void
{ {
$bound = Option::Some('hello')->bind(fn($it) => Option::Some('goodbye')); $bound = Option::Some('hello')->bind(fn($it) => Option::Some('goodbye'));
$this->assertTrue($bound->isSome, 'The option should have been Some'); $this->assertTrue($bound->isSome(), 'The option should have been Some');
$this->assertEquals('goodbye', $bound->value, 'The bound function was not called'); $this->assertEquals('goodbye', $bound->get(), 'The bound function was not called');
} }
#[TestDox('Bind succeeds with Some and None')] #[TestDox('Bind succeeds with Some and None')]
public function testBindSucceedsWithSomeAndNone(): void public function testBindSucceedsWithSomeAndNone(): void
{ {
$bound = Option::Some('greetings')->bind(fn($it) => Option::None()); $bound = Option::Some('greetings')->bind(fn($it) => Option::None());
$this->assertTrue($bound->isNone, 'The option should have been None'); $this->assertTrue($bound->isNone(), 'The option should have been None');
} }
#[TestDox('Contains succeeds with None')] #[TestDox('Contains succeeds with None')]
@ -182,7 +182,7 @@ class OptionTest extends TestCase
$tattle->called = true; $tattle->called = true;
return 'hello'; return 'hello';
}); });
$this->assertTrue($mapped->isNone, 'The mapped option should be "None"'); $this->assertTrue($mapped->isNone(), 'The mapped option should be "None"');
$this->assertFalse($tattle->called, 'The mapping function should not have been called'); $this->assertFalse($tattle->called, 'The mapping function should not have been called');
$this->assertSame($none, $mapped, 'The same "None" instance should have been returned'); $this->assertSame($none, $mapped, 'The same "None" instance should have been returned');
} }
@ -191,8 +191,8 @@ class OptionTest extends TestCase
public function testMapSucceedsWithSome(): void public function testMapSucceedsWithSome(): void
{ {
$mapped = Option::Some('abc ')->map(fn($it) => str_repeat($it, 2)); $mapped = Option::Some('abc ')->map(fn($it) => str_repeat($it, 2));
$this->assertTrue($mapped->isSome, 'The mapped option should be "Some"'); $this->assertTrue($mapped->isSome(), 'The mapped option should be "Some"');
$this->assertEquals('abc abc ', $mapped->value, 'The mapping function was not called correctly'); $this->assertEquals('abc abc ', $mapped->get(), 'The mapping function was not called correctly');
} }
#[TestDox('Map fails with Some when mapping is null')] #[TestDox('Map fails with Some when mapping is null')]
@ -228,7 +228,7 @@ class OptionTest extends TestCase
$tattle->called = true; $tattle->called = true;
return true; return true;
}); });
$this->assertTrue($filtered->isNone, 'The filtered option should have been "None"'); $this->assertTrue($filtered->isNone(), 'The filtered option should have been "None"');
$this->assertFalse($tattle->called, 'The callable should not have been called'); $this->assertFalse($tattle->called, 'The callable should not have been called');
$this->assertSame($none, $filtered, 'The "None" instance returned should have been the one passed'); $this->assertSame($none, $filtered, 'The "None" instance returned should have been the one passed');
} }
@ -238,8 +238,8 @@ class OptionTest extends TestCase
{ {
$some = Option::Some(12); $some = Option::Some(12);
$filtered = $some->filter(fn($it) => $it % 2 === 0); $filtered = $some->filter(fn($it) => $it % 2 === 0);
$this->assertTrue($filtered->isSome, 'The filtered option should have been "Some"'); $this->assertTrue($filtered->isSome(), 'The filtered option should have been "Some"');
$this->assertEquals(12, $filtered->value, 'The filtered option value is incorrect'); $this->assertEquals(12, $filtered->get(), 'The filtered option value is incorrect');
$this->assertSame($some, $filtered, 'The same "Some" instance should have been returned'); $this->assertSame($some, $filtered, 'The same "Some" instance should have been returned');
} }
@ -248,7 +248,7 @@ class OptionTest extends TestCase
{ {
$some = Option::Some(23); $some = Option::Some(23);
$filtered = $some->filter(fn($it) => $it % 2 === 0); $filtered = $some->filter(fn($it) => $it % 2 === 0);
$this->assertTrue($filtered->isNone, 'The filtered option should have been "None"'); $this->assertTrue($filtered->isNone(), 'The filtered option should have been "None"');
} }
#[TestDox('Unwrap succeeds with None')] #[TestDox('Unwrap succeeds with None')]
@ -269,7 +269,7 @@ class OptionTest extends TestCase
$value = ''; $value = '';
$original = Option::Some('testing'); $original = Option::Some('testing');
$tapped = $original->tap( $tapped = $original->tap(
function (Option $it) use (&$value) { $value = $it->isSome ? $it->value : 'none'; }); function (Option $it) use (&$value) { $value = $it->isSome() ? $it->get() : 'none'; });
$this->assertEquals('testing', $value, 'The tapped function was not called'); $this->assertEquals('testing', $value, 'The tapped function was not called');
$this->assertSame($original, $tapped, 'The same option should have been returned'); $this->assertSame($original, $tapped, 'The same option should have been returned');
} }
@ -280,7 +280,7 @@ class OptionTest extends TestCase
$value = ''; $value = '';
$original = Option::None(); $original = Option::None();
$tapped = $original->tap( $tapped = $original->tap(
function (Option $it) use (&$value) { $value = $it->isSome ? $it->value : 'none'; }); function (Option $it) use (&$value) { $value = $it->isSome() ? $it->get() : 'none'; });
$this->assertEquals('none', $value, 'The tapped function was not called'); $this->assertEquals('none', $value, 'The tapped function was not called');
$this->assertSame($original, $tapped, 'The same option should have been returned'); $this->assertSame($original, $tapped, 'The same option should have been returned');
} }
@ -323,7 +323,7 @@ class OptionTest extends TestCase
public function testSomeSucceedsWithValue(): void public function testSomeSucceedsWithValue(): void
{ {
$it = Option::Some('hello'); $it = Option::Some('hello');
$this->assertTrue($it->isSome, 'The option should have been "Some"'); $this->assertTrue($it->isSome(), 'The option should have been "Some"');
} }
public function testSomeFailsWithNull(): void public function testSomeFailsWithNull(): void
@ -335,34 +335,34 @@ class OptionTest extends TestCase
public function testNoneSucceeds(): void public function testNoneSucceeds(): void
{ {
$it = Option::None(); $it = Option::None();
$this->assertTrue($it->isNone, 'The option should have been "None"'); $this->assertTrue($it->isNone(), 'The option should have been "None"');
} }
public function testOfSucceedsWithNull(): void public function testOfSucceedsWithNull(): void
{ {
$it = Option::of(null); $it = Option::of(null);
$this->assertTrue($it->isNone, '"null" should have created a "None" option'); $this->assertTrue($it->isNone(), '"null" should have created a "None" option');
} }
public function testOfSucceedsWithNonNull(): void public function testOfSucceedsWithNonNull(): void
{ {
$it = Option::of('test'); $it = Option::of('test');
$this->assertTrue($it->isSome, 'A non-null value should have created a "Some" option'); $this->assertTrue($it->isSome(), 'A non-null value should have created a "Some" option');
$this->assertEquals('test', $it->value, 'The value was not assigned correctly'); $this->assertEquals('test', $it->get(), 'The value was not assigned correctly');
} }
#[TestDox('Of succeeds with PhpOption\Some')] #[TestDox('Of succeeds with PhpOption\Some')]
public function testOfSucceedsWithPhpOptionSome(): void public function testOfSucceedsWithPhpOptionSome(): void
{ {
$it = Option::of(Some::create('something')); $it = Option::of(Some::create('something'));
$this->assertTrue($it->isSome, 'A "Some" PhpOption should have created a "Some" option'); $this->assertTrue($it->isSome(), 'A "Some" PhpOption should have created a "Some" option');
$this->assertEquals('something', $it->value, 'The value was not assigned correctly'); $this->assertEquals('something', $it->get(), 'The value was not assigned correctly');
} }
#[TestDox('Of succeeds with PhpOption\None')] #[TestDox('Of succeeds with PhpOption\None')]
public function testOfSucceedsWithPhpOptionNone(): void public function testOfSucceedsWithPhpOptionNone(): void
{ {
$it = Option::of(None::create()); $it = Option::of(None::create());
$this->assertTrue($it->isNone, 'A "None" PhpOption should have created a "None" option'); $this->assertTrue($it->isNone(), 'A "None" PhpOption should have created a "None" option');
} }
} }

View File

@ -18,76 +18,76 @@ use PHPUnit\Framework\TestCase;
*/ */
class ResultTest extends TestCase class ResultTest extends TestCase
{ {
#[TestDox('OK property succeeds for OK result')] #[TestDox('GetOK succeeds for OK result')]
public function testOKPropertySucceedsForOKResult(): void public function testGetOKSucceedsForOKResult(): void
{ {
$result = Result::OK('yay'); $result = Result::OK('yay');
$this->assertEquals('yay', $result->ok, 'The OK result should have been returned'); $this->assertEquals('yay', $result->getOK(), 'The OK result should have been returned');
} }
#[TestDox('OK property fails for Error result')] #[TestDox('GetOK fails for Error result')]
public function testOKPropertyFailsForErrorResult(): void public function testGetOKFailsForErrorResult(): void
{ {
$this->expectException(InvalidArgumentException::class); $this->expectException(InvalidArgumentException::class);
Result::Error('whoops')->ok; Result::Error('whoops')->getOK();
} }
#[TestDox('Error property succeeds for Error result')] #[TestDox('GetError succeeds for Error result')]
public function testErrorPropertySucceedsForErrorResult(): void public function testGetErrorSucceedsForErrorResult(): void
{ {
$result = Result::Error('boo'); $result = Result::Error('boo');
$this->assertEquals('boo', $result->error, 'The Error result should have been returned'); $this->assertEquals('boo', $result->getError(), 'The Error result should have been returned');
} }
#[TestDox('Error property fails for OK result')] #[TestDox('GetError fails for OK result')]
public function testErrorPropertyFailsForOKResult(): void public function testGetErrorFailsForOKResult(): void
{ {
$this->expectException(InvalidArgumentException::class); $this->expectException(InvalidArgumentException::class);
Result::OK('yeah')->error; Result::OK('yeah')->getError();
} }
#[TestDox('IsOK succeeds for OK result')] #[TestDox('IsOK succeeds for OK result')]
public function testIsOKSucceedsForOKResult(): void public function testIsOKSucceedsForOKResult(): void
{ {
$result = Result::OK('ok'); $result = Result::OK('ok');
$this->assertTrue($result->isOK, 'The check for "OK" should have returned true'); $this->assertTrue($result->isOK(), 'The check for "OK" should have returned true');
} }
#[TestDox('IsOK succeeds for Error result')] #[TestDox('IsOK succeeds for Error result')]
public function testIsOKSucceedsForErrorResult(): void public function testIsOKSucceedsForErrorResult(): void
{ {
$result = Result::Error('error'); $result = Result::Error('error');
$this->assertFalse($result->isOK, 'The check for "OK" should have returned false'); $this->assertFalse($result->isOK(), 'The check for "OK" should have returned false');
} }
#[TestDox('IsError succeeds for Error result')] #[TestDox('IsError succeeds for Error result')]
public function testIsErrorSucceedsForErrorResult(): void public function testIsErrorSucceedsForErrorResult(): void
{ {
$result = Result::Error('not ok'); $result = Result::Error('not ok');
$this->assertTrue($result->isError, 'The check for "Error" should have returned true'); $this->assertTrue($result->isError(), 'The check for "Error" should have returned true');
} }
#[TestDox('IsError succeeds for OK result')] #[TestDox('IsError succeeds for OK result')]
public function testIsErrorSucceedsForOKResult(): void public function testIsErrorSucceedsForOKResult(): void
{ {
$result = Result::OK('fine'); $result = Result::OK('fine');
$this->assertFalse($result->isError, 'The check for "Error" should have returned false'); $this->assertFalse($result->isError(), 'The check for "Error" should have returned false');
} }
#[TestDox('Bind succeeds for OK with OK')] #[TestDox('Bind succeeds for OK with OK')]
public function testBindSucceedsForOKWithOK(): void public function testBindSucceedsForOKWithOK(): void
{ {
$result = Result::OK('one')->bind(fn($it) => Result::OK("$it two")); $result = Result::OK('one')->bind(fn($it) => Result::OK("$it two"));
$this->assertTrue($result->isOK, 'The result should have been OK'); $this->assertTrue($result->isOK(), 'The result should have been OK');
$this->assertEquals('one two', $result->ok, 'The bound function was not called'); $this->assertEquals('one two', $result->getOK(), 'The bound function was not called');
} }
#[TestDox('Bind succeeds for OK with Error')] #[TestDox('Bind succeeds for OK with Error')]
public function testBindSucceedsForOKWithError(): void public function testBindSucceedsForOKWithError(): void
{ {
$result = Result::OK('three')->bind(fn($it) => Result::Error('back to two')); $result = Result::OK('three')->bind(fn($it) => Result::Error('back to two'));
$this->assertTrue($result->isError, 'The result should have been Error'); $this->assertTrue($result->isError(), 'The result should have been Error');
$this->assertEquals('back to two', $result->error, 'The bound function was not called'); $this->assertEquals('back to two', $result->getError(), 'The bound function was not called');
} }
#[TestDox('Bind succeeds for Error')] #[TestDox('Bind succeeds for Error')]
@ -95,7 +95,7 @@ class ResultTest extends TestCase
{ {
$original = Result::Error('oops'); $original = Result::Error('oops');
$result = $original->bind(fn($it) => Result::OK('never mind - it worked!')); $result = $original->bind(fn($it) => Result::OK('never mind - it worked!'));
$this->assertTrue($result->isError, 'The result should have been Error'); $this->assertTrue($result->isError(), 'The result should have been Error');
$this->assertSame($original, $result, 'The same Error result should have been returned'); $this->assertSame($original, $result, 'The same Error result should have been returned');
} }
@ -153,8 +153,8 @@ class ResultTest extends TestCase
{ {
$ok = Result::OK('yard'); $ok = Result::OK('yard');
$mapped = $ok->map(fn($it) => strrev($it)); $mapped = $ok->map(fn($it) => strrev($it));
$this->assertTrue($mapped->isOK, 'The mapped result should be "OK"'); $this->assertTrue($mapped->isOK(), 'The mapped result should be "OK"');
$this->assertEquals('dray', $mapped->ok, 'The mapping function was not called correctly'); $this->assertEquals('dray', $mapped->getOK(), 'The mapping function was not called correctly');
} }
#[TestDox('Map fails for OK result when mapping is null')] #[TestDox('Map fails for OK result when mapping is null')]
@ -174,7 +174,7 @@ class ResultTest extends TestCase
$tattle->called = true; $tattle->called = true;
return 'hello'; return 'hello';
}); });
$this->assertTrue($mapped->isError, 'The mapped result should be "Error"'); $this->assertTrue($mapped->isError(), 'The mapped result should be "Error"');
$this->assertFalse($tattle->called, 'The mapping function should not have been called'); $this->assertFalse($tattle->called, 'The mapping function should not have been called');
$this->assertSame($error, $mapped, 'The same "Error" instance should have been returned'); $this->assertSame($error, $mapped, 'The same "Error" instance should have been returned');
} }
@ -189,7 +189,7 @@ class ResultTest extends TestCase
$tattle->called = true; $tattle->called = true;
return 'hello'; return 'hello';
}); });
$this->assertTrue($mapped->isOK, 'The mapped result should be "OK"'); $this->assertTrue($mapped->isOK(), 'The mapped result should be "OK"');
$this->assertFalse($tattle->called, 'The mapping function should not have been called'); $this->assertFalse($tattle->called, 'The mapping function should not have been called');
$this->assertSame($ok, $mapped, 'The same "OK" instance should have been returned'); $this->assertSame($ok, $mapped, 'The same "OK" instance should have been returned');
} }
@ -199,8 +199,8 @@ class ResultTest extends TestCase
{ {
$error = Result::Error('taco'); $error = Result::Error('taco');
$mapped = $error->mapError(fn($it) => str_repeat('*', strlen($it))); $mapped = $error->mapError(fn($it) => str_repeat('*', strlen($it)));
$this->assertTrue($mapped->isError, 'The mapped result should be "Error"'); $this->assertTrue($mapped->isError(), 'The mapped result should be "Error"');
$this->assertEquals('****', $mapped->error, 'The mapping function was not called correctly'); $this->assertEquals('****', $mapped->getError(), 'The mapping function was not called correctly');
} }
#[TestDox('MapError fails for Error result when mapping is null')] #[TestDox('MapError fails for Error result when mapping is null')]
@ -246,15 +246,15 @@ class ResultTest extends TestCase
public function testToOptionSucceedsForOKResult() public function testToOptionSucceedsForOKResult()
{ {
$value = Result::OK(99)->toOption(); $value = Result::OK(99)->toOption();
$this->assertTrue($value->isSome, 'An "OK" result should map to a "Some" option'); $this->assertTrue($value->isSome(), 'An "OK" result should map to a "Some" option');
$this->assertEquals(99, $value->value, 'The value is not correct'); $this->assertEquals(99, $value->get(), 'The value is not correct');
} }
#[TestDox('ToOption succeeds for Error result')] #[TestDox('ToOption succeeds for Error result')]
public function testToOptionSucceedsForErrorResult() public function testToOptionSucceedsForErrorResult()
{ {
$value = Result::Error('file not found')->toOption(); $value = Result::Error('file not found')->toOption();
$this->assertTrue($value->isNone, 'An "Error" result should map to a "None" option'); $this->assertTrue($value->isNone(), 'An "Error" result should map to a "None" option');
} }
#[TestDox('Tap succeeds for OK result')] #[TestDox('Tap succeeds for OK result')]
@ -263,7 +263,7 @@ class ResultTest extends TestCase
$value = ''; $value = '';
$original = Result::OK('working'); $original = Result::OK('working');
$tapped = $original->tap(function (Result $it) use (&$value) { $tapped = $original->tap(function (Result $it) use (&$value) {
$value = $it->isOK ? 'OK: ' . $it->ok : 'Error: ' . $it->error; $value = $it->isOK() ? 'OK: ' . $it->getOK() : 'Error: ' . $it->getError();
}); });
$this->assertEquals('OK: working', $value, 'The tapped function was not called'); $this->assertEquals('OK: working', $value, 'The tapped function was not called');
$this->assertSame($original, $tapped, 'The same result should have been returned'); $this->assertSame($original, $tapped, 'The same result should have been returned');
@ -275,7 +275,7 @@ class ResultTest extends TestCase
$value = ''; $value = '';
$original = Result::Error('failed'); $original = Result::Error('failed');
$tapped = $original->tap(function (Result $it) use (&$value) { $tapped = $original->tap(function (Result $it) use (&$value) {
$value = $it->isOK ? 'OK: ' . $it->ok : 'Error: ' . $it->error; $value = $it->isOK() ? 'OK: ' . $it->getOK() : 'Error: ' . $it->getError();
}); });
$this->assertEquals('Error: failed', $value, 'The tapped function was not called'); $this->assertEquals('Error: failed', $value, 'The tapped function was not called');
$this->assertSame($original, $tapped, 'The same result should have been returned'); $this->assertSame($original, $tapped, 'The same result should have been returned');
@ -285,8 +285,8 @@ class ResultTest extends TestCase
public function testOKSucceedsForNonNullResult(): void public function testOKSucceedsForNonNullResult(): void
{ {
$result = Result::OK('something'); $result = Result::OK('something');
$this->assertTrue($result->isOK, 'The result should have been "OK"'); $this->assertTrue($result->isOK(), 'The result should have been "OK"');
$this->assertEquals('something', $result->ok, 'The "OK" value was incorrect'); $this->assertEquals('something', $result->getOK(), 'The "OK" value was incorrect');
} }
#[TestDox('OK fails for null result')] #[TestDox('OK fails for null result')]
@ -300,8 +300,8 @@ class ResultTest extends TestCase
public function testErrorSucceedsForNonNullResult(): void public function testErrorSucceedsForNonNullResult(): void
{ {
$result = Result::Error('sad trombone'); $result = Result::Error('sad trombone');
$this->assertTrue($result->isError, 'The result should have been "Error"'); $this->assertTrue($result->isError(), 'The result should have been "Error"');
$this->assertEquals('sad trombone', $result->error, 'The "Error" value was incorrect'); $this->assertEquals('sad trombone', $result->getError(), 'The "Error" value was incorrect');
} }
#[TestDox('Error fails for null result')] #[TestDox('Error fails for null result')]