diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 630734f117..6baba2de9c 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -1611,35 +1611,38 @@ private function resolveType(string $exprString, Expr $node): Type } if ($node instanceof Expr\Ternary) { + $noopCallback = static function (): void { + }; + $condResult = $this->nodeScopeResolver->processExprNode($node->cond, $this, $noopCallback, ExpressionContext::createDeep()); if ($node->if === null) { $conditionType = $this->getType($node->cond); $booleanConditionType = $conditionType->toBoolean(); if ($booleanConditionType->isTrue()->yes()) { - return $this->filterByTruthyValue($node->cond)->getType($node->cond); + return $condResult->getTruthyScope()->getType($node->cond); } if ($booleanConditionType->isFalse()->yes()) { - return $this->filterByFalseyValue($node->cond)->getType($node->else); + return $condResult->getFalseyScope()->getType($node->else); } return TypeCombinator::union( - TypeCombinator::removeFalsey($this->filterByTruthyValue($node->cond)->getType($node->cond)), - $this->filterByFalseyValue($node->cond)->getType($node->else), + TypeCombinator::removeFalsey($condResult->getTruthyScope()->getType($node->cond)), + $condResult->getFalseyScope()->getType($node->else), ); } $booleanConditionType = $this->getType($node->cond)->toBoolean(); if ($booleanConditionType->isTrue()->yes()) { - return $this->filterByTruthyValue($node->cond)->getType($node->if); + return $condResult->getTruthyScope()->getType($node->if); } if ($booleanConditionType->isFalse()->yes()) { - return $this->filterByFalseyValue($node->cond)->getType($node->else); + return $condResult->getFalseyScope()->getType($node->else); } return TypeCombinator::union( - $this->filterByTruthyValue($node->cond)->getType($node->if), - $this->filterByFalseyValue($node->cond)->getType($node->else), + $condResult->getTruthyScope()->getType($node->if), + $condResult->getFalseyScope()->getType($node->else), ); } diff --git a/tests/PHPStan/Rules/Comparison/data/bug-5365.php b/tests/PHPStan/Rules/Comparison/data/bug-5365.php index 4313d81532..54f2263446 100644 --- a/tests/PHPStan/Rules/Comparison/data/bug-5365.php +++ b/tests/PHPStan/Rules/Comparison/data/bug-5365.php @@ -12,3 +12,11 @@ function (): void { $found = (bool)preg_match( $pattern, $subject, $matches ) && isset( $matches['productId'] ); assertType('bool', $found); }; + +function (): void { + $matches = []; + $pattern = '#^C\s+(?\d+)$#i'; + $subject = 'C 1234567890'; + + assertType('bool', preg_match( $pattern, $subject, $matches ) ? isset( $matches['productId'] ) : false); +};