Skip to content

Commit 45f01ad

Browse files
authored
Merge pull request #97 from KartaviK/hotfix/enum-inheritance-compatibility
Solution for construct enum from inherited enum with incompatible value
2 parents 46244e7 + 3359da8 commit 45f01ad

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/Enum.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ abstract class Enum implements \JsonSerializable
4141
public function __construct($value)
4242
{
4343
if ($value instanceof static) {
44-
$this->value = $value->getValue();
45-
46-
return;
44+
$value = $value->getValue();
4745
}
4846

4947
if (!$this->isValid($value)) {

tests/EnumTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,15 @@ public function testUnserialize()
320320
$this->assertEquals(EnumFixture::FOO, $value->getValue());
321321
$this->assertTrue(EnumFixture::FOO()->equals($value));
322322
}
323+
324+
/**
325+
* @see https://github.com/myclabs/php-enum/issues/95
326+
*/
327+
public function testEnumValuesInheritance()
328+
{
329+
$this->expectException(\UnexpectedValueException::class);
330+
$this->expectExceptionMessage("Value 'value' is not part of the enum MyCLabs\Tests\Enum\EnumFixture");
331+
$inheritedEnumFixture = InheritedEnumFixture::VALUE();
332+
new EnumFixture($inheritedEnumFixture);
333+
}
323334
}

tests/InheritedEnumFixture.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace MyCLabs\Tests\Enum;
4+
5+
/**
6+
* Class InheritedEnumFixture.
7+
* @package MyCLabs\Tests\Enum
8+
*
9+
* @method static InheritedEnumFixture VALUE()
10+
*/
11+
class InheritedEnumFixture extends EnumFixture
12+
{
13+
const VALUE = 'value';
14+
}

0 commit comments

Comments
 (0)