Skip to content

Commit 121e021

Browse files
committed
Enums will no longer crash the playground
1 parent c001f66 commit 121e021

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

src/Reflection/ClassReflection.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,27 @@ public function hasEnumCase(string $name): bool
544544
return $this->reflection->hasCase($name);
545545
}
546546

547+
/**
548+
* @return array<string, EnumCaseReflection>
549+
*/
550+
public function getEnumCases(): array
551+
{
552+
if (!$this->reflection instanceof ReflectionEnum) {
553+
throw new ShouldNotHappenException();
554+
}
555+
556+
$cases = [];
557+
foreach ($this->reflection->getCases() as $case) {
558+
$valueType = null;
559+
if ($case instanceof ReflectionEnumBackedCase) {
560+
$valueType = ConstantTypeHelper::getTypeFromValue($case->getBackingValue());
561+
}
562+
$cases[$case->getName()] = new EnumCaseReflection($this, $case->getName(), $valueType);
563+
}
564+
565+
return $cases;
566+
}
567+
547568
public function getEnumCase(string $name): EnumCaseReflection
548569
{
549570
if (!$this->hasEnumCase($name)) {

src/Reflection/Php/PhpClassReflectionExtension.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,7 @@ private function createProperty(
158158
|| ($declaringClassReflection->isBackedEnum() && $propertyName === 'value')
159159
) {
160160
$types = [];
161-
foreach (array_keys($classReflection->getNativeReflection()->getConstants()) as $name) {
162-
if (!$classReflection->hasEnumCase($name)) {
163-
continue;
164-
}
165-
161+
foreach (array_keys($classReflection->getEnumCases()) as $name) {
166162
if ($propertyName === 'name') {
167163
$types[] = new ConstantStringType($name);
168164
continue;
@@ -450,11 +446,7 @@ private function createMethod(
450446
&& strtolower($methodReflection->getName()) === 'cases'
451447
) {
452448
$arrayBuilder = ConstantArrayTypeBuilder::createEmpty();
453-
foreach (array_keys($classReflection->getNativeReflection()->getConstants()) as $name) {
454-
if (!$classReflection->hasEnumCase($name)) {
455-
continue;
456-
}
457-
449+
foreach (array_keys($classReflection->getEnumCases()) as $name) {
458450
$arrayBuilder->setOffsetValueType(null, new EnumCaseObjectType($classReflection->getName(), $name));
459451
}
460452

src/Type/ObjectType.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -979,14 +979,9 @@ public function changeSubtractedType(?Type $subtractedType): Type
979979
{
980980
$classReflection = $this->getClassReflection();
981981
if ($classReflection !== null && $classReflection->isEnum() && $subtractedType !== null) {
982-
$constants = $classReflection->getNativeReflection()->getConstants();
983982
$cases = [];
984-
foreach (array_keys($constants) as $constantName) {
985-
if (!$classReflection->hasEnumCase($constantName)) {
986-
continue;
987-
}
988-
989-
$cases[$constantName] = new EnumCaseObjectType($classReflection->getName(), $constantName);
983+
foreach (array_keys($classReflection->getEnumCases()) as $name) {
984+
$cases[$name] = new EnumCaseObjectType($classReflection->getName(), $name);
990985
}
991986

992987
foreach (TypeUtils::flattenTypes($subtractedType) as $subType) {

src/Type/StaticType.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -408,13 +408,8 @@ public function changeSubtractedType(?Type $subtractedType): Type
408408
{
409409
$classReflection = $this->getClassReflection();
410410
if ($classReflection !== null && $classReflection->isEnum() && $subtractedType !== null) {
411-
$constants = $classReflection->getNativeReflection()->getConstants();
412411
$cases = [];
413-
foreach (array_keys($constants) as $constantName) {
414-
if (!$classReflection->hasEnumCase($constantName)) {
415-
continue;
416-
}
417-
412+
foreach (array_keys($classReflection->getEnumCases()) as $constantName) {
418413
$cases[$constantName] = new EnumCaseObjectType($classReflection->getName(), $constantName);
419414
}
420415

0 commit comments

Comments
 (0)