diff --git a/src/Reflection/ClassReflection.php b/src/Reflection/ClassReflection.php index 131804e9df..8106faf39d 100644 --- a/src/Reflection/ClassReflection.php +++ b/src/Reflection/ClassReflection.php @@ -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; @@ -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) { @@ -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 @@ -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) {