Skip to content

Commit

Permalink
Limit the depth when resolving closure type for performance reasons
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jul 6, 2024
1 parent 7c61b03 commit 2c08dfa
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ class MutatingScope implements Scope

private ?self $scopeWithPromotedNativeTypes = null;

private static int $resolveClosureTypeDepth = 0;

/**
* @param array<string, ExpressionTypeHolder> $expressionTypes
* @param array<string, ConditionalExpressionHolder[]> $conditionalExpressions
Expand Down Expand Up @@ -1322,6 +1324,16 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu
$closureExecutionEnds = [];
$closureImpurePoints = [];
$invalidateExpressions = [];
if (self::$resolveClosureTypeDepth >= 2) {
return new ClosureType(
$parameters,
$this->getFunctionType($node->returnType, false, false),
$isVariadic,
);
}

self::$resolveClosureTypeDepth++;
try {
$closureStatementResult = $this->nodeScopeResolver->processStmtNodes($node, $node->stmts, $closureScope, static function (Node $node, Scope $scope) use ($closureScope, &$closureReturnStatements, &$closureYieldStatements, &$closureExecutionEnds, &$closureImpurePoints, &$invalidateExpressions): void {
if ($scope->getAnonymousFunctionReflection() !== $closureScope->getAnonymousFunctionReflection()) {
return;
Expand Down Expand Up @@ -1372,6 +1384,9 @@ static function (Node $node, Scope $scope) use ($arrowScope, &$arrowFunctionImpu

$closureYieldStatements[] = [$node, $scope];
}, StatementContext::createTopLevel());
} finally {
self::$resolveClosureTypeDepth--;
}

$throwPoints = $closureStatementResult->getThrowPoints();
$impurePoints = array_merge($closureImpurePoints, $closureStatementResult->getImpurePoints());
Expand Down

0 comments on commit 2c08dfa

Please sign in to comment.