From 121e0212db869fe0c42307a1485f27054d492041 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Tue, 18 Jan 2022 14:12:07 +0100 Subject: [PATCH] Enums will no longer crash the playground --- src/Reflection/ClassReflection.php | 21 +++++++++++++++++++ .../Php/PhpClassReflectionExtension.php | 12 ++--------- src/Type/ObjectType.php | 9 ++------ src/Type/StaticType.php | 7 +------ 4 files changed, 26 insertions(+), 23 deletions(-) diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index 10e4c9aa7a..b8dcdedc1b 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -544,6 +544,27 @@ public function hasEnumCase(string $name): bool return $this->reflection->hasCase($name); } + /** + * @return array + */ + public function getEnumCases(): array + { + if (!$this->reflection instanceof ReflectionEnum) { + throw new ShouldNotHappenException(); + } + + $cases = []; + foreach ($this->reflection->getCases() as $case) { + $valueType = null; + if ($case instanceof ReflectionEnumBackedCase) { + $valueType = ConstantTypeHelper::getTypeFromValue($case->getBackingValue()); + } + $cases[$case->getName()] = new EnumCaseReflection($this, $case->getName(), $valueType); + } + + return $cases; + } + public function getEnumCase(string $name): EnumCaseReflection { if (!$this->hasEnumCase($name)) { diff --git a/src/Reflection/Php/PhpClassReflectionExtension.php b/src/Reflection/Php/PhpClassReflectionExtension.php index d09c3276d0..bf0054d575 100644 --- a/src/Reflection/Php/PhpClassReflectionExtension.php +++ b/src/Reflection/Php/PhpClassReflectionExtension.php @@ -158,11 +158,7 @@ private function createProperty( || ($declaringClassReflection->isBackedEnum() && $propertyName === 'value') ) { $types = []; - foreach (array_keys($classReflection->getNativeReflection()->getConstants()) as $name) { - if (!$classReflection->hasEnumCase($name)) { - continue; - } - + foreach (array_keys($classReflection->getEnumCases()) as $name) { if ($propertyName === 'name') { $types[] = new ConstantStringType($name); continue; @@ -450,11 +446,7 @@ private function createMethod( && strtolower($methodReflection->getName()) === 'cases' ) { $arrayBuilder = ConstantArrayTypeBuilder::createEmpty(); - foreach (array_keys($classReflection->getNativeReflection()->getConstants()) as $name) { - if (!$classReflection->hasEnumCase($name)) { - continue; - } - + foreach (array_keys($classReflection->getEnumCases()) as $name) { $arrayBuilder->setOffsetValueType(null, new EnumCaseObjectType($classReflection->getName(), $name)); } diff --git a/src/Type/ObjectType.php b/src/Type/ObjectType.php index d81f55af93..97c393a61b 100644 --- a/src/Type/ObjectType.php +++ b/src/Type/ObjectType.php @@ -979,14 +979,9 @@ public function changeSubtractedType(?Type $subtractedType): Type { $classReflection = $this->getClassReflection(); if ($classReflection !== null && $classReflection->isEnum() && $subtractedType !== null) { - $constants = $classReflection->getNativeReflection()->getConstants(); $cases = []; - foreach (array_keys($constants) as $constantName) { - if (!$classReflection->hasEnumCase($constantName)) { - continue; - } - - $cases[$constantName] = new EnumCaseObjectType($classReflection->getName(), $constantName); + foreach (array_keys($classReflection->getEnumCases()) as $name) { + $cases[$name] = new EnumCaseObjectType($classReflection->getName(), $name); } foreach (TypeUtils::flattenTypes($subtractedType) as $subType) { diff --git a/src/Type/StaticType.php b/src/Type/StaticType.php index f94cb14f77..a9d467f69c 100644 --- a/src/Type/StaticType.php +++ b/src/Type/StaticType.php @@ -408,13 +408,8 @@ public function changeSubtractedType(?Type $subtractedType): Type { $classReflection = $this->getClassReflection(); if ($classReflection !== null && $classReflection->isEnum() && $subtractedType !== null) { - $constants = $classReflection->getNativeReflection()->getConstants(); $cases = []; - foreach (array_keys($constants) as $constantName) { - if (!$classReflection->hasEnumCase($constantName)) { - continue; - } - + foreach (array_keys($classReflection->getEnumCases()) as $constantName) { $cases[$constantName] = new EnumCaseObjectType($classReflection->getName(), $constantName); }