Skip to content

Commit a6ca5f7

Browse files
committed
Optimize calculating scalar values from huge unions
1 parent 707cfb2 commit a6ca5f7

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

src/Analyser/MutatingScope.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@
101101
class MutatingScope implements Scope
102102
{
103103

104+
public const CALCULATE_SCALARS_LIMIT = 128;
105+
104106
private const OPERATOR_SIGIL_MAP = [
105107
Node\Expr\AssignOp\Plus::class => '+',
106108
Node\Expr\AssignOp\Minus::class => '-',
@@ -1099,11 +1101,18 @@ private function resolveType(Expr $node): Type
10991101
$leftTypes = TypeUtils::getConstantScalars($this->getType($left));
11001102
$rightTypes = TypeUtils::getConstantScalars($this->getType($right));
11011103

1102-
if (count($leftTypes) > 0 && count($rightTypes) > 0) {
1104+
$leftTypesCount = count($leftTypes);
1105+
$rightTypesCount = count($rightTypes);
1106+
if ($leftTypesCount > 0 && $rightTypesCount > 0) {
11031107
$resultTypes = [];
1108+
$generalize = $leftTypesCount * $rightTypesCount > self::CALCULATE_SCALARS_LIMIT;
11041109
foreach ($leftTypes as $leftType) {
11051110
foreach ($rightTypes as $rightType) {
1106-
$resultTypes[] = $this->calculateFromScalars($node, $leftType, $rightType);
1111+
$resultType = $this->calculateFromScalars($node, $leftType, $rightType);
1112+
if ($generalize) {
1113+
$resultType = TypeUtils::generalizeType($resultType);
1114+
}
1115+
$resultTypes[] = $resultType;
11071116
}
11081117
}
11091118
return TypeCombinator::union(...$resultTypes);

0 commit comments

Comments
 (0)