Skip to content

Commit

Permalink
Restore original Ternary handling
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 28, 2023
1 parent 32cbe96 commit 6e72a32
Showing 1 changed file with 2 additions and 38 deletions.
40 changes: 2 additions & 38 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@
use PHPStan\Node\InFunctionNode;
use PHPStan\Node\InstantiationCallableNode;
use PHPStan\Node\InTraitNode;
use PHPStan\Node\IssetExpr;
use PHPStan\Node\LiteralArrayItem;
use PHPStan\Node\LiteralArrayNode;
use PHPStan\Node\MatchExpressionArm;
Expand Down Expand Up @@ -2782,41 +2781,9 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
)->getScope();
} elseif ($expr instanceof Ternary) {
$ternaryCondResult = $this->processExprNode($expr->cond, $scope, $nodeCallback, $context->enterDeep());
$condScope = $ternaryCondResult->getScope();

$truthySpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($condScope, $expr->cond, TypeSpecifierContext::createTruthy());
$falseySpecifiedTypes = $this->typeSpecifier->specifyTypesInCondition($condScope, $expr->cond, TypeSpecifierContext::createFalsey());
$ifTrueScope = $ternaryCondResult->getTruthyScope()->filterBySpecifiedTypes($truthySpecifiedTypes);
$ifFalseScope = $ternaryCondResult->getFalseyScope()->filterBySpecifiedTypes($falseySpecifiedTypes);

$restoreIssetExprCertainty = static function (MutatingScope $previousScope, MutatingScope $scope, SpecifiedTypes $specifiedTypes): MutatingScope {
foreach ($specifiedTypes->getSureNotTypes() as [$expr, $exprType]) {
if (!$expr instanceof IssetExpr) {
continue;
}

$expr = $expr->getExpr();
if ($scope->hasExpressionType($expr)->no()) {
continue;
}

$previousScopeHasExpressionType = $previousScope->hasExpressionType($expr);
if ($previousScopeHasExpressionType->no()) {
continue;
}

$scope = $scope->specifyExpressionType(
$expr,
$scope->getType($expr),
$scope->getNativeType($expr),
$previousScopeHasExpressionType,
);
}

return $scope;
};

$throwPoints = $ternaryCondResult->getThrowPoints();
$ifTrueScope = $ternaryCondResult->getTruthyScope();
$ifFalseScope = $ternaryCondResult->getFalseyScope();
$ifTrueType = null;
if ($expr->if !== null) {
$ifResult = $this->processExprNode($expr->if, $ifTrueScope, $nodeCallback, $context);
Expand All @@ -2841,9 +2808,6 @@ static function (Node $node, Scope $scope) use ($nodeCallback): void {
}
}

$finalScope = $restoreIssetExprCertainty($scope, $finalScope, $truthySpecifiedTypes);
$finalScope = $restoreIssetExprCertainty($scope, $finalScope, $falseySpecifiedTypes);

return new ExpressionResult(
$finalScope,
$ternaryCondResult->hasYield(),
Expand Down

0 comments on commit 6e72a32

Please sign in to comment.