Skip to content

Commit

Permalink
Remove cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Dec 30, 2024
1 parent 32f2267 commit db7fa8b
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/Nexus/Option/None.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function unwrapOrElse(\Closure $default): mixed

public function map(\Closure $predicate): self
{
return clone $this;
return $this;
}

/**
Expand Down Expand Up @@ -90,17 +90,17 @@ public function mapOrElse(\Closure $default, \Closure $predicate): mixed

public function and(Option $other): self
{
return clone $this;
return $this;
}

public function andThen(\Closure $predicate): self
{
return clone $this;
return $this;
}

public function filter(\Closure $predicate): self
{
return clone $this;
return $this;
}

public function or(Option $other): Option
Expand All @@ -122,7 +122,7 @@ public function orElse(\Closure $other): Option
*/
public function xor(Option $other): Option
{
return $other->isSome() ? $other : clone $this;
return $other->isSome() ? $other : $this;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Nexus/Option/Some.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function andThen(\Closure $predicate): Option

public function filter(\Closure $predicate): Option
{
return $predicate($this->value) ? clone $this : new None();
return $predicate($this->value) ? $this : new None();
}

/**
Expand All @@ -118,7 +118,7 @@ public function filter(\Closure $predicate): Option
*/
public function or(Option $other): self
{
return clone $this;
return $this;
}

/**
Expand All @@ -130,7 +130,7 @@ public function or(Option $other): self
*/
public function orElse(\Closure $other): self
{
return clone $this;
return $this;
}

/**
Expand All @@ -142,7 +142,7 @@ public function orElse(\Closure $other): self
*/
public function xor(Option $other): Option
{
return $other->isSome() ? new None() : clone $this;
return $other->isSome() ? new None() : $this;
}

/**
Expand Down
18 changes: 9 additions & 9 deletions tests/Option/OptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public function testOptionMap(): void
$none = new None();
$newNone = $none->map($predicate);
self::assertTrue($newNone->isNone());
self::assertNotSame($newNone, $none);
self::assertSame($newNone, $none);
}

public function testOptionMapOr(): void
Expand All @@ -113,7 +113,7 @@ public function testOptionAnd(): void
self::assertTrue((new Some(2))->and($none)->isNone());
self::assertTrue($none->and(new Some('foo'))->isNone());
self::assertTrue($none->and($none)->isNone());
self::assertNotSame($none, $none->and($none));
self::assertSame($none, $none->and($none));

$option = (new Some(2))->and(new Some('foo'));
self::assertInstanceOf(Some::class, $option);
Expand All @@ -130,7 +130,7 @@ public function testOptionAndThen(): void

$none = new None();
self::assertTrue($none->andThen($squareThenToString)->isNone());
self::assertNotSame($none, $none->andThen($squareThenToString));
self::assertSame($none, $none->andThen($squareThenToString));
}

public function testOptionFilter(): void
Expand All @@ -139,13 +139,13 @@ public function testOptionFilter(): void

$none = new None();
self::assertTrue($none->filter($isEven)->isNone());
self::assertNotSame($none, $none->filter($isEven));
self::assertSame($none, $none->filter($isEven));

self::assertFalse((new Some(3))->filter($isEven)->isSome());

$some = new Some(4);
self::assertTrue($some->filter($isEven)->isSome());
self::assertNotSame($some, $some->filter($isEven));
self::assertSame($some, $some->filter($isEven));
}

public function testOptionOr(): void
Expand All @@ -156,7 +156,7 @@ public function testOptionOr(): void

self::assertTrue($some02->or($none)->isSome());
self::assertSame(2, $some02->or($none)->unwrap());
self::assertNotSame($some02, $some02->or($none));
self::assertSame($some02, $some02->or($none));

self::assertTrue($none->or($some100)->isSome());
self::assertSame(100, $none->or($some100)->unwrap());
Expand All @@ -175,7 +175,7 @@ public function testOptionOrElse(): void
$some = new Some('barbarians');
$option = $some->orElse($vikings);
self::assertTrue($option->isSome());
self::assertNotSame($some, $option);
self::assertSame($some, $option);
self::assertSame('barbarians', $option->unwrap());

$option = (new None())->orElse($vikings);
Expand All @@ -192,14 +192,14 @@ public function testOptionXor(): void

self::assertInstanceOf(Some::class, $some->xor($none));
self::assertSame(2, $some->xor($none)->unwrap());
self::assertNotSame($some, $some->xor($none));
self::assertSame($some, $some->xor($none));

self::assertInstanceOf(Some::class, $none->xor($some));
self::assertSame(2, $none->xor($some)->unwrap());

self::assertInstanceOf(None::class, $some->xor($some));
self::assertInstanceOf(None::class, $none->xor($none));
self::assertNotSame($none, $none->xor($none));
self::assertSame($none, $none->xor($none));
}

public function testOptionIteration(): void
Expand Down

0 comments on commit db7fa8b

Please sign in to comment.