Skip to content

Commit e1cc10a

Browse files
authored
Merge pull request #87 from jacek-foremski/equals_accepts_anything
Change "equals()" method to accept anything as a parameter
2 parents 6b7d5ee + 7f285a3 commit e1cc10a

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/Enum.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,15 +80,18 @@ public function __toString()
8080
}
8181

8282
/**
83-
* Compares one Enum with another.
83+
* Determines if Enum should be considered equal with the variable passed as a parameter.
84+
* Returns false if an argument is an object of different class or not an object.
8485
*
8586
* This method is final, for more information read https://github.com/myclabs/php-enum/issues/4
8687
*
87-
* @return bool True if Enums are equal, false if not equal
88+
* @return bool
8889
*/
89-
final public function equals(Enum $enum = null)
90+
final public function equals($variable = null)
9091
{
91-
return $enum !== null && $this->getValue() === $enum->getValue() && \get_called_class() === \get_class($enum);
92+
return $variable instanceof self
93+
&& $this->getValue() === $variable->getValue()
94+
&& \get_called_class() === \get_class($variable);
9295
}
9396

9497
/**

tests/EnumTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,11 +224,15 @@ public function testEquals()
224224
$foo = new EnumFixture(EnumFixture::FOO);
225225
$number = new EnumFixture(EnumFixture::NUMBER);
226226
$anotherFoo = new EnumFixture(EnumFixture::FOO);
227+
$objectOfDifferentClass = new \stdClass();
228+
$notAnObject = 'foo';
227229

228230
$this->assertTrue($foo->equals($foo));
229231
$this->assertFalse($foo->equals($number));
230232
$this->assertTrue($foo->equals($anotherFoo));
231233
$this->assertFalse($foo->equals(null));
234+
$this->assertFalse($foo->equals($objectOfDifferentClass));
235+
$this->assertFalse($foo->equals($notAnObject));
232236
}
233237

234238
/**

0 commit comments

Comments
 (0)