Skip to content

Commit

Permalink
ClassReflection - cache enum cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed May 10, 2024
1 parent 73521c3 commit 39ce042
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/Reflection/ClassReflection.php
Expand Up @@ -82,6 +82,9 @@ class ClassReflection
/** @var ClassConstantReflection[] */
private array $constants = [];

/** @var EnumCaseReflection[]|null */
private ?array $enumCases = null;

/** @var int[]|null */
private ?array $classHierarchyDistances = null;

Expand Down Expand Up @@ -752,6 +755,10 @@ public function getEnumCases(): array
throw new ShouldNotHappenException();
}

if ($this->enumCases !== null) {
return $this->enumCases;
}

$cases = [];
$initializerExprContext = InitializerExprContext::fromClassReflection($this);
foreach ($this->reflection->getCases() as $case) {
Expand All @@ -764,7 +771,7 @@ public function getEnumCases(): array
$cases[$caseName] = new EnumCaseReflection($this, $caseName, $valueType);
}

return $cases;
return $this->enumCases = $cases;
}

public function getEnumCase(string $name): EnumCaseReflection
Expand All @@ -777,6 +784,10 @@ public function getEnumCase(string $name): EnumCaseReflection
throw new ShouldNotHappenException();
}

if ($this->enumCases !== null && array_key_exists($name, $this->enumCases)) {
return $this->enumCases[$name];
}

$case = $this->reflection->getCase($name);
$valueType = null;
if ($case instanceof ReflectionEnumBackedCase) {
Expand Down

1 comment on commit 39ce042

@staabm
Copy link
Contributor

@staabm staabm commented on 39ce042 May 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had something similar locally but reverted it and went with a enum-case cache only in ObjectType with #3062

Please sign in to comment.