From 9e408acecf11a6ca39aeeca059dace44f9739c5e Mon Sep 17 00:00:00 2001 From: Vlasta Neubauer Date: Sat, 28 Aug 2021 13:57:08 +0200 Subject: [PATCH] Abstract classes in Enum inheritance hierarchy are skipped --- src/Enum/EnumSetMixin.php | 5 +++++ tests/src/Enum/Enum.phpt | 22 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/src/Enum/EnumSetMixin.php b/src/Enum/EnumSetMixin.php index a9603bef..2aee1694 100644 --- a/src/Enum/EnumSetMixin.php +++ b/src/Enum/EnumSetMixin.php @@ -97,6 +97,11 @@ final protected static function init(string $class): void /** @var ReflectionClass $parent */ $parent = $ref->getParentClass(); $parentClass = $parent->getName(); + if (!in_array($parentClass, [IntEnum::class, StringEnum::class, IntSet::class, StringSet::class], true) && $parent->isAbstract()) { + /** @var ReflectionClass $parent */ + $parent = $parent->getParentClass(); + $parentClass = $parent->getName(); + } $isDescendant = !in_array($parentClass, $rootClasses, true); $parentValues = []; diff --git a/tests/src/Enum/Enum.phpt b/tests/src/Enum/Enum.phpt index 9e2ee987..202569d0 100644 --- a/tests/src/Enum/Enum.phpt +++ b/tests/src/Enum/Enum.phpt @@ -54,6 +54,20 @@ class E extends IntEnum } +// skip abstract +abstract class F extends IntEnum +{ + +} + +class G extends F +{ + + public const ONE = 1; + public const TWO = 2; + +} + $aOne = A::get(1); $aOne2 = A::get(1); $aTwo = A::get(2); @@ -63,12 +77,14 @@ $abOne = AB::get(1); $abcOne = ABC::get(1); $abcTwo = ABC::get(2); $abdOne = ABD::get(1); -$fOne = E::get(1); +$eOne = E::get(1); +$gOne = G::get(1); get: Assert::type($aOne, A::class); Assert::equal($aOne, $aOne2); +Assert::equal($gOne, G::get(1)); getValue: @@ -80,8 +96,8 @@ Assert::same($aOne->getConstantName(), 'ONE'); equals: -Assert::exception(static function () use ($aOne, $fOne): void { - $aOne->equals($fOne); +Assert::exception(static function () use ($aOne, $eOne): void { + $aOne->equals($eOne); }, InvalidTypeException::class); Assert::false($aOne->equals($aTwo)); Assert::true($aOne->equals($aOne2));