diff --git a/src/Analyser/TypeSpecifier.php b/src/Analyser/TypeSpecifier.php index 83974aa3ae..fb845c658f 100644 --- a/src/Analyser/TypeSpecifier.php +++ b/src/Analyser/TypeSpecifier.php @@ -2605,9 +2605,19 @@ public function resolveEqual(Expr\BinaryOp\Equal $expr, Scope $scope, TypeSpecif $leftTypes = $this->create($expr->left, $leftType, $context, $scope)->setRootExpr($expr); $rightTypes = $this->create($expr->right, $rightType, $context, $scope)->setRootExpr($expr); - return $context->true() + $result = $context->true() ? $leftTypes->unionWith($rightTypes) : $leftTypes->normalize($scope)->intersectWith($rightTypes->normalize($scope)); + + if ( + !$context->null() + && ($leftTypes->getSureTypes() !== [] || $leftTypes->getSureNotTypes() !== []) + && ($rightTypes->getSureTypes() !== [] || $rightTypes->getSureNotTypes() !== []) + ) { + $result = $result->unionWith($this->handleDefaultTruthyOrFalseyContext($context, $expr, $scope)); + } + + return $result; } public function resolveIdentical(Expr\BinaryOp\Identical $expr, Scope $scope, TypeSpecifierContext $context): SpecifiedTypes diff --git a/tests/PHPStan/Analyser/TypeSpecifierTest.php b/tests/PHPStan/Analyser/TypeSpecifierTest.php index aa66c94001..dc066beb99 100644 --- a/tests/PHPStan/Analyser/TypeSpecifierTest.php +++ b/tests/PHPStan/Analyser/TypeSpecifierTest.php @@ -1317,6 +1317,7 @@ public static function dataCondition(): iterable [ '$float' => 'float', '(int) $float' => 'int', + '$float == (int) $float' => '~0|0.0|\'\'|\'0\'|array{}|false|null', ], [], ], diff --git a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php index a05ca24efd..ffed79ee6c 100644 --- a/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php +++ b/tests/PHPStan/Rules/Variables/DefinedVariableRuleTest.php @@ -1514,4 +1514,13 @@ public function testBug14117(): void ]); } + public function testBug7718(): void + { + $this->cliArgumentsVariablesRegistered = true; + $this->polluteScopeWithLoopInitialAssignments = false; + $this->checkMaybeUndefinedVariables = true; + $this->polluteScopeWithAlwaysIterableForeach = true; + $this->analyse([__DIR__ . '/data/bug-7718.php'], []); + } + } diff --git a/tests/PHPStan/Rules/Variables/data/bug-7718.php b/tests/PHPStan/Rules/Variables/data/bug-7718.php new file mode 100644 index 0000000000..13827ebfcb --- /dev/null +++ b/tests/PHPStan/Rules/Variables/data/bug-7718.php @@ -0,0 +1,11 @@ +