Skip to content

Commit

Permalink
improve comparator and comparable interface
Browse files Browse the repository at this point in the history
  • Loading branch information
akondas committed May 29, 2024
1 parent 7a6c345 commit b01cfc2
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/Collection/Traversable.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ public function findFirst(): Option
return $this->isEmpty() ? Option::none() : Option::some($this->head());
}

public function equals($object): bool
public function equals(mixed $object): bool
{
if ($object === $this) {
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/Control/Option/None.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function get()
throw new NoSuchElementException('No value present');
}

public function equals($object): bool
public function equals(mixed $object): bool
{
return $object instanceof self;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Control/TryTo/Failure.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public function isEmpty(): bool
return true;
}

public function equals($object): bool
public function equals(mixed $object): bool
{
return $this->cause == $object;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Value.php
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,10 @@ public function getOrElseTry(callable $supplier)

/**
* Similar to "==" operator, but also checks congruence of structures and equality of contained values.
*
* @param mixed $object
*/
public function equals($object): bool
public function equals(mixed $other): bool
{
return Comparator::equals($this->get(), $object);
return Comparator::equals($this->get(), $other);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Value/Comparable.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

interface Comparable
{
public function equals(self $other): bool;
public function equals(mixed $other): bool;
}
12 changes: 1 addition & 11 deletions src/Value/Comparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@

namespace Munus\Value;

use Munus\Tuple;

final class Comparator
{
/**
* @param mixed $a
* @param mixed $b
*/
public static function equals($a, $b): bool
public static function equals(mixed $a, mixed $b): bool
{
if ($a instanceof Comparable) {
return $a->equals($b);
Expand All @@ -22,10 +16,6 @@ public static function equals($a, $b): bool
return $b->equals($a);
}

if ($a instanceof Tuple && $b instanceof Tuple) {
return $a->equals($b);
}

if (is_object($a) && is_object($b)) {
return $a == $b;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/Stub/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function __construct(public string $id, public string $name)
{
}

public function equals(Comparable $other): bool
public function equals(mixed $other): bool
{
return self::class === $other::class && $this->name === $other->name;
}
Expand Down

0 comments on commit b01cfc2

Please sign in to comment.