Skip to content

Commit

Permalink
Faster ObjectType->getEnumCases() type substraction
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Jul 2, 2024
1 parent c6b961d commit 3a11805
Showing 1 changed file with 17 additions and 14 deletions.
31 changes: 17 additions & 14 deletions src/Type/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3a11805

Please sign in to comment.