Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve comparator and comparable interface #106

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading