From 50f90e7ce16f39cd685af584d2a3b910b4fd8bab Mon Sep 17 00:00:00 2001 From: Martin Herndl Date: Tue, 3 May 2022 09:45:32 +0200 Subject: [PATCH] Do not call TypeSpecifier::specifyTypesInCondition with a non-null context in case the original context is null --- src/Analyser/TypeSpecifier.php | 8 ++++---- .../StrictComparisonOfDifferentTypesRuleTest.php | 6 ++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Analyser/TypeSpecifier.php b/src/Analyser/TypeSpecifier.php index 88fbcdba422..6221164a741 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 e10304d5b74..6ee4eb78ac8 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'], []); + } + }