Skip to content

Change "equals()" method to accept anything as a parameter #87

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

Merged
merged 1 commit into from
Apr 23, 2019
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
11 changes: 7 additions & 4 deletions src/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,18 @@ public function __toString()
}

/**
* Compares one Enum with another.
* Determines if Enum should be considered equal with the variable passed as a parameter.
* Returns false if an argument is an object of different class or not an object.
*
* This method is final, for more information read https://github.com/myclabs/php-enum/issues/4
*
* @return bool True if Enums are equal, false if not equal
* @return bool
*/
final public function equals(Enum $enum = null)
final public function equals($variable = null)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the = null default kept? $enum->equals() seems like a pretty strange call to make.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would be a BC break.

{
return $enum !== null && $this->getValue() === $enum->getValue() && \get_called_class() === \get_class($enum);
return $variable instanceof self
&& $this->getValue() === $variable->getValue()
&& \get_called_class() === \get_class($variable);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/EnumTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,15 @@ public function testEquals()
$foo = new EnumFixture(EnumFixture::FOO);
$number = new EnumFixture(EnumFixture::NUMBER);
$anotherFoo = new EnumFixture(EnumFixture::FOO);
$objectOfDifferentClass = new \stdClass();
$notAnObject = 'foo';

$this->assertTrue($foo->equals($foo));
$this->assertFalse($foo->equals($number));
$this->assertTrue($foo->equals($anotherFoo));
$this->assertFalse($foo->equals(null));
$this->assertFalse($foo->equals($objectOfDifferentClass));
$this->assertFalse($foo->equals($notAnObject));
}

/**
Expand Down