Skip to content

Commit faba805

Browse files
committed
MutatingScope - process ternary cond before getting type of the whole expression
1 parent 989dd6f commit faba805

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/Analyser/MutatingScope.php

+11-8
Original file line numberDiff line numberDiff line change
@@ -1611,35 +1611,38 @@ private function resolveType(string $exprString, Expr $node): Type
16111611
}
16121612

16131613
if ($node instanceof Expr\Ternary) {
1614+
$noopCallback = static function (): void {
1615+
};
1616+
$condResult = $this->nodeScopeResolver->processExprNode($node->cond, $this, $noopCallback, ExpressionContext::createDeep());
16141617
if ($node->if === null) {
16151618
$conditionType = $this->getType($node->cond);
16161619
$booleanConditionType = $conditionType->toBoolean();
16171620
if ($booleanConditionType->isTrue()->yes()) {
1618-
return $this->filterByTruthyValue($node->cond)->getType($node->cond);
1621+
return $condResult->getTruthyScope()->getType($node->cond);
16191622
}
16201623

16211624
if ($booleanConditionType->isFalse()->yes()) {
1622-
return $this->filterByFalseyValue($node->cond)->getType($node->else);
1625+
return $condResult->getFalseyScope()->getType($node->else);
16231626
}
16241627

16251628
return TypeCombinator::union(
1626-
TypeCombinator::removeFalsey($this->filterByTruthyValue($node->cond)->getType($node->cond)),
1627-
$this->filterByFalseyValue($node->cond)->getType($node->else),
1629+
TypeCombinator::removeFalsey($condResult->getTruthyScope()->getType($node->cond)),
1630+
$condResult->getFalseyScope()->getType($node->else),
16281631
);
16291632
}
16301633

16311634
$booleanConditionType = $this->getType($node->cond)->toBoolean();
16321635
if ($booleanConditionType->isTrue()->yes()) {
1633-
return $this->filterByTruthyValue($node->cond)->getType($node->if);
1636+
return $condResult->getTruthyScope()->getType($node->if);
16341637
}
16351638

16361639
if ($booleanConditionType->isFalse()->yes()) {
1637-
return $this->filterByFalseyValue($node->cond)->getType($node->else);
1640+
return $condResult->getFalseyScope()->getType($node->else);
16381641
}
16391642

16401643
return TypeCombinator::union(
1641-
$this->filterByTruthyValue($node->cond)->getType($node->if),
1642-
$this->filterByFalseyValue($node->cond)->getType($node->else),
1644+
$condResult->getTruthyScope()->getType($node->if),
1645+
$condResult->getFalseyScope()->getType($node->else),
16431646
);
16441647
}
16451648

tests/PHPStan/Rules/Comparison/data/bug-5365.php

+8
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@ function (): void {
1212
$found = (bool)preg_match( $pattern, $subject, $matches ) && isset( $matches['productId'] );
1313
assertType('bool', $found);
1414
};
15+
16+
function (): void {
17+
$matches = [];
18+
$pattern = '#^C\s+(?<productId>\d+)$#i';
19+
$subject = 'C 1234567890';
20+
21+
assertType('bool', preg_match( $pattern, $subject, $matches ) ? isset( $matches['productId'] ) : false);
22+
};

0 commit comments

Comments
 (0)