diff --git a/src/Analyser/TypeSpecifier.php b/src/Analyser/TypeSpecifier.php index 88fbcdba42..6221164a74 100644 --- a/src/Analyser/TypeSpecifier.php +++ b/src/Analyser/TypeSpecifier.php @@ -175,7 +175,7 @@ public function specifyTypesInCondition( $exprNode = $expressions[0]; /** @var ConstantScalarType $constantType */ $constantType = $expressions[1]; - if ($constantType->getValue() === false) { + if (!$context->null() && $constantType->getValue() === false) { $types = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr); return $types->unionWith($this->specifyTypesInCondition( $scope, @@ -185,7 +185,7 @@ public function specifyTypesInCondition( )); } - if ($constantType->getValue() === true) { + if (!$context->null() && $constantType->getValue() === true) { $types = $this->create($exprNode, $constantType, $context, false, $scope, $rootExpr); return $types->unionWith($this->specifyTypesInCondition( $scope, @@ -339,7 +339,7 @@ public function specifyTypesInCondition( $exprNode = $expressions[0]; /** @var ConstantScalarType $constantType */ $constantType = $expressions[1]; - if ($constantType->getValue() === false || $constantType->getValue() === null) { + if (!$context->null() && ($constantType->getValue() === false || $constantType->getValue() === null)) { return $this->specifyTypesInCondition( $scope, $exprNode, @@ -348,7 +348,7 @@ public function specifyTypesInCondition( ); } - if ($constantType->getValue() === true) { + if (!$context->null() && $constantType->getValue() === true) { return $this->specifyTypesInCondition( $scope, $exprNode, diff --git a/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php b/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php index e10304d5b7..6ee4eb78ac 100644 --- a/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php +++ b/tests/PHPStan/Rules/Comparison/StrictComparisonOfDifferentTypesRuleTest.php @@ -528,4 +528,10 @@ public function testBug6939(): void ]); } + public function testBug7166(): void + { + $this->checkAlwaysTrueStrictComparison = true; + $this->analyse([__DIR__ . '/data/bug-7166.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Comparison/data/bug-7166.php b/tests/PHPStan/Rules/Comparison/data/bug-7166.php new file mode 100644 index 0000000000..707eba5423 --- /dev/null +++ b/tests/PHPStan/Rules/Comparison/data/bug-7166.php @@ -0,0 +1,16 @@ +