Skip to content

Commit

Permalink
feat!: remove $pass handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Lctrs committed May 7, 2024
1 parent 8cce003 commit 0be48a7
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 96 deletions.
9 changes: 1 addition & 8 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="5.24.0@462c80e31c34e58cc4f750c656be3927e80e550e">
<file src="src/Option/None.php">
<MixedArgument>
<code><![CDATA[$this->pass]]></code>
<code><![CDATA[$this->pass]]></code>
</MixedArgument>
</file>
</files>
<files psalm-version="5.24.0@462c80e31c34e58cc4f750c656be3927e80e550e"/>
27 changes: 10 additions & 17 deletions src/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ 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);

/**
* 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<T>
*/
abstract public function inspect(callable $f): self;
Expand All @@ -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<U>
*/
abstract public function map(callable $mapper): self;
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -126,7 +126,7 @@ abstract public function okOr($err): Result;
*
* @template E
*
* @param callable(mixed...):E $err
* @param callable():E $err
* @return Result<T, E>
*/
abstract public function okOrElse(callable $err): Result;
Expand Down Expand Up @@ -155,7 +155,7 @@ abstract public function and(self $optb): self;
*
* @template U
*
* @param callable(T=,mixed...):Option<U> $op
* @param callable(T=):Option<U> $op
* @return Option<U>
*/
abstract public function andThen(callable $op): self;
Expand All @@ -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<T>
*/
abstract public function filter(callable $predicate): self;
Expand All @@ -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<T> $op
* @param callable():Option<T> $op
* @return Option<T>
*/
abstract public function orElse(callable $op): self;

/**
* The attached pass-through args will be unpacked into extra args into chained callables
*
* @return Option<T>
*/
abstract public function with(mixed ...$args): self;

/**
* Create a Some<T> if T is something using isset(T), None otherwise
*
Expand Down
33 changes: 8 additions & 25 deletions src/Option/None.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,6 @@
*/
class None extends Option
{
/**
* @var array<array-key, mixed>
*/
private array $pass;

public function __construct(mixed ...$pass)
{
$this->pass = $pass;
}

public function isSome(): bool
{
return false;
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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();
}
}
30 changes: 8 additions & 22 deletions src/Option/Some.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,14 @@
*/
class Some extends Option
{
/**
* @var array<array-key, mixed>
*/
private array $pass;

/**
* Some constructor.
*
* @param T $value
*/
public function __construct(
private $value,
mixed ...$pass
) {
$this->pass = $pass;
}

public function isSome(): bool
Expand Down Expand Up @@ -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
Expand All @@ -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;
}

Expand All @@ -144,11 +137,4 @@ public function orElse(callable $op): Option
{
return $this;
}

public function with(mixed ...$args): Option
{
$this->pass = $args;

return $this;
}
}
15 changes: 0 additions & 15 deletions test/Unit/NoneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> */
$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))
);
}
}
9 changes: 0 additions & 9 deletions test/Unit/SomeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,13 +136,4 @@ public function testItDoesntOrElse(): void

self::assertSame($some, $some->orElse(static fn () => new Some('bar')));
}

public function testItHandlesPass(): void
{
/** @var Some<string> */
$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));
}
}

0 comments on commit 0be48a7

Please sign in to comment.