From 0be48a7e5ab90d3a9aca6336984ebae99270f11b Mon Sep 17 00:00:00 2001 From: Lctrs Date: Tue, 7 May 2024 21:44:58 +0200 Subject: [PATCH] feat!: remove `$pass` handling --- psalm-baseline.xml | 9 +-------- src/Option.php | 27 ++++++++++----------------- src/Option/None.php | 33 ++++++++------------------------- src/Option/Some.php | 30 ++++++++---------------------- test/Unit/NoneTest.php | 15 --------------- test/Unit/SomeTest.php | 9 --------- 6 files changed, 27 insertions(+), 96 deletions(-) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 016fed4..b5d05c7 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1,9 +1,2 @@ - - - - pass]]> - pass]]> - - - + diff --git a/src/Option.php b/src/Option.php index 26961d0..e311611 100644 --- a/src/Option.php +++ b/src/Option.php @@ -66,7 +66,7 @@ abstract public function unwrapOr($optb); /** * Returns the contained value or computes it from a callable. * - * @param callable(mixed...): T $op + * @param callable(): T $op * @return T */ abstract public function unwrapOrElse(callable $op); @@ -74,7 +74,7 @@ abstract public function unwrapOrElse(callable $op); /** * Calls a function with a reference to the contained value if Some. * - * @param callable(T,mixed...):void $f + * @param callable(T):void $f * @return Option */ abstract public function inspect(callable $f): self; @@ -84,7 +84,7 @@ abstract public function inspect(callable $f): self; * * @template U * - * @param callable(T=,mixed...):U $mapper + * @param callable(T=):U $mapper * @return Option */ abstract public function map(callable $mapper): self; @@ -95,7 +95,7 @@ abstract public function map(callable $mapper): self; * @template U * * @param U $default - * @param callable(T=,mixed...):U $mapper + * @param callable(T=):U $mapper * @return U */ abstract public function mapOr($default, callable $mapper); @@ -105,8 +105,8 @@ abstract public function mapOr($default, callable $mapper); * * @template U * - * @param callable(mixed...):U $default - * @param callable(T=,mixed...):U $mapper + * @param callable():U $default + * @param callable(T=):U $mapper * @return U */ abstract public function mapOrElse(callable $default, callable $mapper); @@ -126,7 +126,7 @@ abstract public function okOr($err): Result; * * @template E * - * @param callable(mixed...):E $err + * @param callable():E $err * @return Result */ abstract public function okOrElse(callable $err): Result; @@ -155,7 +155,7 @@ abstract public function and(self $optb): self; * * @template U * - * @param callable(T=,mixed...):Option $op + * @param callable(T=):Option $op * @return Option */ abstract public function andThen(callable $op): self; @@ -165,7 +165,7 @@ abstract public function andThen(callable $op): self; * - Some(t) if predicate returns true (where t is the wrapped value), and * - None if predicate returns false. * - * @param callable(T,mixed...):bool $predicate + * @param callable(T):bool $predicate * @return Option */ abstract public function filter(callable $predicate): self; @@ -181,18 +181,11 @@ abstract public function or(self $optb): self; /** * Returns the option if it contains a value, otherwise calls op and returns the result. * - * @param callable(mixed...):Option $op + * @param callable():Option $op * @return Option */ abstract public function orElse(callable $op): self; - /** - * The attached pass-through args will be unpacked into extra args into chained callables - * - * @return Option - */ - abstract public function with(mixed ...$args): self; - /** * Create a Some if T is something using isset(T), None otherwise * diff --git a/src/Option/None.php b/src/Option/None.php index 7fb58dc..1df7b71 100644 --- a/src/Option/None.php +++ b/src/Option/None.php @@ -26,16 +26,6 @@ */ class None extends Option { - /** - * @var array - */ - private array $pass; - - public function __construct(mixed ...$pass) - { - $this->pass = $pass; - } - public function isSome(): bool { return false; @@ -69,7 +59,7 @@ public function unwrapOr($optb) public function unwrapOrElse(callable $op) { - return $op(...$this->pass); + return $op(); } public function inspect(callable $f): Option @@ -79,7 +69,7 @@ public function inspect(callable $f): Option public function map(callable $mapper): Option { - return new self(...$this->pass); + return new self(); } public function mapOr($default, callable $mapper) @@ -89,17 +79,17 @@ public function mapOr($default, callable $mapper) public function mapOrElse(callable $default, callable $mapper) { - return $default(...$this->pass); + return $default(); } public function okOr($err): Result { - return new Err($err, ...$this->pass); + return new Err($err); } public function okOrElse(callable $err): Result { - return new Err($err(...$this->pass), ...$this->pass); + return new Err($err()); } public function iter(): array @@ -109,12 +99,12 @@ public function iter(): array public function and(Option $optb): Option { - return new self(...$this->pass); + return new self(); } public function andThen(callable $op): Option { - return new self(...$this->pass); + return new self(); } public function filter(callable $predicate): Option @@ -129,13 +119,6 @@ public function or(Option $optb): Option public function orElse(callable $op): Option { - return $op(...$this->pass); - } - - public function with(mixed ...$args): Option - { - $this->pass = $args; - - return $this; + return $op(); } } diff --git a/src/Option/Some.php b/src/Option/Some.php index e8057f5..9a482a5 100644 --- a/src/Option/Some.php +++ b/src/Option/Some.php @@ -26,11 +26,6 @@ */ class Some extends Option { - /** - * @var array - */ - private array $pass; - /** * Some constructor. * @@ -38,9 +33,7 @@ class Some extends Option */ public function __construct( private $value, - mixed ...$pass ) { - $this->pass = $pass; } public function isSome(): bool @@ -81,34 +74,34 @@ public function unwrapOrElse(callable $op) public function inspect(callable $f): Option { - $f($this->value, ...$this->pass); + $f($this->value); return $this; } public function map(callable $mapper): Option { - return new self($mapper($this->value, ...$this->pass)); + return new self($mapper($this->value)); } public function mapOr($default, callable $mapper) { - return $mapper($this->value, ...$this->pass); + return $mapper($this->value); } public function mapOrElse(callable $default, callable $mapper) { - return $mapper($this->value, ...$this->pass); + return $mapper($this->value); } public function okOr($err): Result { - return new Ok($this->value, ...$this->pass); + return new Ok($this->value); } public function okOrElse(callable $err): Result { - return new Ok($this->value, ...$this->pass); + return new Ok($this->value); } public function iter(): array @@ -123,12 +116,12 @@ public function and(Option $optb): Option public function andThen(callable $op): Option { - return $op($this->value, ...$this->pass); + return $op($this->value); } public function filter(callable $predicate): Option { - if ($predicate($this->value, ...$this->pass)) { + if ($predicate($this->value)) { return $this; } @@ -144,11 +137,4 @@ public function orElse(callable $op): Option { return $this; } - - public function with(mixed ...$args): Option - { - $this->pass = $args; - - return $this; - } } diff --git a/test/Unit/NoneTest.php b/test/Unit/NoneTest.php index c4f48bb..1747fde 100644 --- a/test/Unit/NoneTest.php +++ b/test/Unit/NoneTest.php @@ -139,19 +139,4 @@ public function testItOrElse(): void self::assertSame($optb, (new None())->orElse(static fn () => $optb)); } - - /** - * @uses \Prewk\Option\Some - */ - public function testItHandlesPass(): void - { - /** @var None */ - $none = new None('bar'); - - self::assertEquals(new Some('bar'), $none->orElse(static fn (string $aString) => new Some($aString))); - self::assertEquals( - new Some('baz'), - $none->with('baz')->orElse(static fn (string $aString) => new Some($aString)) - ); - } } diff --git a/test/Unit/SomeTest.php b/test/Unit/SomeTest.php index 19a3f8a..7211bfd 100644 --- a/test/Unit/SomeTest.php +++ b/test/Unit/SomeTest.php @@ -136,13 +136,4 @@ public function testItDoesntOrElse(): void self::assertSame($some, $some->orElse(static fn () => new Some('bar'))); } - - public function testItHandlesPass(): void - { - /** @var Some */ - $some = new Some('foo', 'bar', 0); - - self::assertEquals(new Some(0), $some->map(static fn (string $value, string $aString, int $anInt) => $anInt)); - self::assertEquals(new Some(1), $some->with(1)->map(static fn (string $value, int $anInt) => $anInt)); - } }