Skip to content

Commit

Permalink
Merge pull request #87 from jacek-foremski/equals_accepts_anything
Browse files Browse the repository at this point in the history
Change "equals()" method to accept anything as a parameter
  • Loading branch information
mnapoli authored Apr 23, 2019
2 parents 6b7d5ee + 7f285a3 commit e1cc10a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
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)
{
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

0 comments on commit e1cc10a

Please sign in to comment.