Skip to content

Commit 2c08dfa

Browse files
committed
Limit the depth when resolving closure type for performance reasons
1 parent 7c61b03 commit 2c08dfa

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/Analyser/MutatingScope.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,8 @@ class MutatingScope implements Scope
180180

181181
private ?self $scopeWithPromotedNativeTypes = null;
182182

183+
private static int $resolveClosureTypeDepth = 0;
184+
183185
/**
184186
* @param array<string, ExpressionTypeHolder> $expressionTypes
185187
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
@@ -1322,6 +1324,16 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
13221324
$closureExecutionEnds = [];
13231325
$closureImpurePoints = [];
13241326
$invalidateExpressions = [];
1327+
if (self::$resolveClosureTypeDepth >= 2) {
1328+
return new ClosureType(
1329+
$parameters,
1330+
$this->getFunctionType($node->returnType, false, false),
1331+
$isVariadic,
1332+
);
1333+
}
1334+
1335+
self::$resolveClosureTypeDepth++;
1336+
try {
13251337
$closureStatementResult = $this->nodeScopeResolver->processStmtNodes($node, $node->stmts, $closureScope, static function (Node $node, Scope $scope) use ($closureScope, &$closureReturnStatements, &$closureYieldStatements, &$closureExecutionEnds, &$closureImpurePoints, &$invalidateExpressions): void {
13261338
if ($scope->getAnonymousFunctionReflection() !== $closureScope->getAnonymousFunctionReflection()) {
13271339
return;
@@ -1372,6 +1384,9 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
13721384

13731385
$closureYieldStatements[] = [$node, $scope];
13741386
}, StatementContext::createTopLevel());
1387+
} finally {
1388+
self::$resolveClosureTypeDepth--;
1389+
}
13751390

13761391
$throwPoints = $closureStatementResult->getThrowPoints();
13771392
$impurePoints = array_merge($closureImpurePoints, $closureStatementResult->getImpurePoints());

0 commit comments

Comments
 (0)