From 3a1180543597882042bca6a7481a24db58de98ed Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Tue, 2 Jul 2024 11:52:45 +0200 Subject: [PATCH] Faster ObjectType->getEnumCases() type substraction --- src/Type/ObjectType.php | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/Type/ObjectType.php b/src/Type/ObjectType.php index 1ef20306ee..322fe1540a 100644 --- a/src/Type/ObjectType.php +++ b/src/Type/ObjectType.php @@ -1229,25 +1229,28 @@ public function getEnumCases(): array $className = $classReflection->getName(); - $cases = []; - foreach ($classReflection->getEnumCases() as $enumCase) { - $cases[] = new EnumCaseObjectType($className, $enumCase->getName(), $classReflection); - } - if ($this->subtractedType !== null) { - $subtracedEnumCases = $this->subtractedType->getEnumCases(); - foreach ($cases as $i => $case) { - $caseName = $case->getEnumCaseName(); - foreach ($subtracedEnumCases as $subtracedCase) { - if ($caseName === $subtracedCase->getEnumCaseName()) { - unset($cases[$i]); - continue 2; - } + $subtracedEnumCaseNames = []; + + foreach ($this->subtractedType->getEnumCases() as $subtractedCase) { + $subtracedEnumCaseNames[$subtractedCase->getEnumCaseName()] = true; + } + + $cases = []; + foreach ($classReflection->getEnumCases() as $enumCase) { + if (array_key_exists($enumCase->getName(), $subtracedEnumCaseNames)) { + continue; } + $cases[] = new EnumCaseObjectType($className, $enumCase->getName(), $classReflection); + } + } else { + $cases = []; + foreach ($classReflection->getEnumCases() as $enumCase) { + $cases[] = new EnumCaseObjectType($className, $enumCase->getName(), $classReflection); } } - return self::$enumCases[$cacheKey] = array_values($cases); + return self::$enumCases[$cacheKey] = $cases; } public function isCallable(): TrinaryLogic