From a6ec7bf0c5b9c5ef83e6b8c2ae255ad6fee2cfee Mon Sep 17 00:00:00 2001 From: "John Paul E. Balandan, CPA" Date: Wed, 18 Dec 2024 03:00:33 +0800 Subject: [PATCH] Remove internal `Choice` class --- phpstan-baseline.php | 4 +- src/Nexus/Option/Choice.php | 43 ------------------- src/Nexus/Option/functions.php | 6 ++- .../{ChoiceTest.php => FunctionsTest.php} | 12 +----- tests/Option/data/option.php | 6 +-- 5 files changed, 11 insertions(+), 60 deletions(-) delete mode 100644 src/Nexus/Option/Choice.php rename tests/Option/{ChoiceTest.php => FunctionsTest.php} (67%) diff --git a/phpstan-baseline.php b/phpstan-baseline.php index c8d4cda..2cff2b9 100644 --- a/phpstan-baseline.php +++ b/phpstan-baseline.php @@ -8,10 +8,10 @@ 'path' => __DIR__ . '/src/Nexus/Collection/Collection.php', ]; $ignoreErrors[] = [ - 'message' => '#^Method Nexus\\\\Option\\\\Choice\\:\\:from\\(\\) never returns Nexus\\\\Option\\\\Some\\ so it can be removed from the return type\\.$#', + 'message' => '#^Function Nexus\\\\Option\\\\option\\(\\) never returns Nexus\\\\Option\\\\Some\\ so it can be removed from the return type\\.$#', 'identifier' => 'return.unusedType', 'count' => 1, - 'path' => __DIR__ . '/src/Nexus/Option/Choice.php', + 'path' => __DIR__ . '/src/Nexus/Option/functions.php', ]; $ignoreErrors[] = [ 'message' => '#^Method Nexus\\\\Tests\\\\AutoReview\\\\ComposerJsonTest\\:\\:getComposer\\(\\) should return array\\ but returns mixed\\.$#', diff --git a/src/Nexus/Option/Choice.php b/src/Nexus/Option/Choice.php deleted file mode 100644 index d4b4ea5..0000000 --- a/src/Nexus/Option/Choice.php +++ /dev/null @@ -1,43 +0,0 @@ - - * - * For the full copyright and license information, please view - * the LICENSE file that was distributed with this source code. - */ - -namespace Nexus\Option; - -/** - * @internal - */ -final class Choice -{ - /** - * Creates an option from the given `$value`. - * - * The value of a **None** option can be defined by assigning a `$none` value. - * By default, this is equal to `null` but can be another value. - * - * @template T - * @template S - * - * @param T $value - * @param S $none - * - * @return (T is S ? None : Some) - */ - public static function from(mixed $value, mixed $none = null): Option - { - if ($value === $none) { - return new None(); - } - - return new Some($value); - } -} diff --git a/src/Nexus/Option/functions.php b/src/Nexus/Option/functions.php index a12f25b..9486350 100644 --- a/src/Nexus/Option/functions.php +++ b/src/Nexus/Option/functions.php @@ -24,5 +24,9 @@ */ function option(mixed $value, mixed $none = null): Option { - return Choice::from($value, $none); + if ($value === $none) { + return new None(); + } + + return new Some($value); } diff --git a/tests/Option/ChoiceTest.php b/tests/Option/FunctionsTest.php similarity index 67% rename from tests/Option/ChoiceTest.php rename to tests/Option/FunctionsTest.php index cb7af44..f713cfd 100644 --- a/tests/Option/ChoiceTest.php +++ b/tests/Option/FunctionsTest.php @@ -13,10 +13,8 @@ namespace Nexus\Tests\Option; -use Nexus\Option\Choice; use Nexus\Option\None; use Nexus\Option\Some; -use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\CoversFunction; use PHPUnit\Framework\Attributes\Group; use PHPUnit\Framework\TestCase; @@ -26,18 +24,10 @@ /** * @internal */ -#[CoversClass(Choice::class)] #[CoversFunction('Nexus\Option\option')] #[Group('unit-test')] -final class ChoiceTest extends TestCase +final class FunctionsTest extends TestCase { - public function testChoiceFrom(): void - { - self::assertInstanceOf(Some::class, Choice::from(2)); - self::assertInstanceOf(None::class, Choice::from(null)); - self::assertInstanceOf(Some::class, Choice::from(null, false)); - } - public function testOptionFunction(): void { self::assertInstanceOf(Some::class, option(2)); diff --git a/tests/Option/data/option.php b/tests/Option/data/option.php index 84b89de..013a752 100644 --- a/tests/Option/data/option.php +++ b/tests/Option/data/option.php @@ -13,16 +13,16 @@ namespace Nexus\Tests\Option; -use Nexus\Option\Choice; use Nexus\Option\None; use Nexus\Option\Option; use Nexus\Option\Some; +use function Nexus\Option\option; use function PHPStan\Testing\assertType; -assertType(None::class, Choice::from(null)); +assertType(None::class, option(null)); assertType(None::class, new None()); -assertType('Nexus\Option\Some', Choice::from(null, false)); +assertType('Nexus\Option\Some', option(null, false)); assertType('Nexus\Option\Some', new Some(2)); function testOption(Option $option): void