diff --git a/src/Analyser/ExprHandler/Helper/ConditionalExpressionHolderHelper.php b/src/Analyser/ExprHandler/Helper/ConditionalExpressionHolderHelper.php index e8c1008c183..f2a3412c599 100644 --- a/src/Analyser/ExprHandler/Helper/ConditionalExpressionHolderHelper.php +++ b/src/Analyser/ExprHandler/Helper/ConditionalExpressionHolderHelper.php @@ -172,9 +172,15 @@ public function processBooleanConditionalTypes(Scope $scope, SpecifiedTypes $con continue; } + $scopeType = $scope->getType($expr); + $conditionType = TypeCombinator::intersect($scopeType, $type); + if ($scopeType->equals($conditionType)) { + continue; + } + $conditionExpressionTypes[$exprString] = ExpressionTypeHolder::createYes( $expr, - TypeCombinator::intersect($scope->getType($expr), $type), + $conditionType, ); } diff --git a/tests/PHPStan/Analyser/nsrt/bug-14878.php b/tests/PHPStan/Analyser/nsrt/bug-14878.php new file mode 100644 index 00000000000..dab39e4e12a --- /dev/null +++ b/tests/PHPStan/Analyser/nsrt/bug-14878.php @@ -0,0 +1,89 @@ +analyse([__DIR__ . '/data/bug-14807.php'], []); } + public function testBug14878(): void + { + $this->treatPhpDocTypesAsCertain = true; + $this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-14878.php'], []); + } + public function testInTrait(): void { $this->treatPhpDocTypesAsCertain = true; diff --git a/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php b/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php index fc774b404e8..3da1fd10bed 100644 --- a/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php @@ -1244,6 +1244,11 @@ public function testBug14791(): void $this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-14791.php'], []); } + public function testBug14878(): void + { + $this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-14878.php'], []); + } + public function testBug14847(): void { $this->analyse([__DIR__ . '/data/bug-14847.php'], [