diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index a26a86a066..48f592e4c5 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -3833,7 +3833,15 @@ public function filterBySpecifiedTypes(SpecifiedTypes $specifiedTypes): self } $conditions[$conditionalExprString][] = $conditionalExpression; - $specifiedExpressions[$conditionalExprString] = $conditionalExpression->getTypeHolder(); + $newTypeHolder = $conditionalExpression->getTypeHolder(); + if (array_key_exists($conditionalExprString, $specifiedExpressions) && $specifiedExpressions[$conditionalExprString]->getCertainty()->yes() && $newTypeHolder->getCertainty()->yes()) { + $specifiedExpressions[$conditionalExprString] = ExpressionTypeHolder::createYes( + $newTypeHolder->getExpr(), + TypeCombinator::intersect($specifiedExpressions[$conditionalExprString]->getType(), $newTypeHolder->getType()), + ); + } else { + $specifiedExpressions[$conditionalExprString] = $newTypeHolder; + } } } } diff --git a/tests/PHPStan/Analyser/nsrt/bug-14211.php b/tests/PHPStan/Analyser/nsrt/bug-14211.php new file mode 100644 index 0000000000..8effe5a25a --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-14211.php @@ -0,0 +1,25 @@ + $data */ +function doSomething(array $data): bool { + + if (!isset($data['x'])) + return false; + + $m = isset($data['y']); + + if ($m) { + assertType('true', $m); + } + assertType('bool', $m); + + if ($m) { + assertType('true', $m); + } + + return true; +}