Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent overly greedy $scope->getType() calls in NodeScopeResolver #2104

Merged
merged 1 commit into from Dec 13, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 9 additions & 5 deletions src/Analyser/NodeScopeResolver.php
Expand Up @@ -1622,8 +1622,9 @@ private function ensureShallowNonNullability(MutatingScope $scope, Scope $origin
$exprTypeWithoutNull = TypeCombinator::removeNull($exprType);
if ($exprType->equals($exprTypeWithoutNull)) {
$originalExprType = $originalScope->getType($exprToSpecify);
$originalNativeType = $originalScope->getNativeType($exprToSpecify);
if (!$originalExprType->equals($exprTypeWithoutNull)) {
$originalNativeType = $originalScope->getNativeType($exprToSpecify);

return new EnsuredNonNullabilityResult($scope, [
new EnsuredNonNullabilityResultExpression($exprToSpecify, $originalExprType, $originalNativeType),
]);
Expand Down Expand Up @@ -2643,14 +2644,17 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
$elseResult = $this->processExprNode($expr->else, $ifFalseScope, $nodeCallback, $context);
$throwPoints = array_merge($throwPoints, $elseResult->getThrowPoints());
$ifFalseScope = $elseResult->getScope();
$ifFalseType = $ifFalseScope->getType($expr->else);

if ($ifTrueType instanceof NeverType && $ifTrueType->isExplicit()) {
$finalScope = $ifFalseScope;
} elseif ($ifFalseType instanceof NeverType && $ifFalseType->isExplicit()) {
$finalScope = $ifTrueScope;
} else {
$finalScope = $ifTrueScope->mergeWith($ifFalseScope);
$ifFalseType = $ifFalseScope->getType($expr->else);

if ($ifFalseType instanceof NeverType && $ifFalseType->isExplicit()) {
$finalScope = $ifTrueScope;
} else {
$finalScope = $ifTrueScope->mergeWith($ifFalseScope);
}
}

return new ExpressionResult(
Expand Down