diff --git a/src/Reflection/InitializerExprTypeResolver.php b/src/Reflection/InitializerExprTypeResolver.php index 5c93ad6593..90266acd15 100644 --- a/src/Reflection/InitializerExprTypeResolver.php +++ b/src/Reflection/InitializerExprTypeResolver.php @@ -1253,6 +1253,15 @@ public function resolveEqualType(Type $leftType, Type $rightType): BooleanType return $this->resolveIdenticalType($leftType, $rightType); } + if ($leftType instanceof ConstantArrayType && $leftType->isEmpty() && $rightType instanceof ConstantScalarType) { + // @phpstan-ignore-next-line + return new ConstantBooleanType($rightType->getValue() == []); // phpcs:ignore + } + if ($rightType instanceof ConstantArrayType && $rightType->isEmpty() && $leftType instanceof ConstantScalarType) { + // @phpstan-ignore-next-line + return new ConstantBooleanType($leftType->getValue() == []); // phpcs:ignore + } + if ($leftType instanceof ConstantScalarType && $rightType instanceof ConstantScalarType) { // @phpstan-ignore-next-line return new ConstantBooleanType($leftType->getValue() == $rightType->getValue()); // phpcs:ignore diff --git a/tests/PHPStan/Analyser/NodeScopeResolverTest.php b/tests/PHPStan/Analyser/NodeScopeResolverTest.php index 9d5dabc986..638d860295 100644 --- a/tests/PHPStan/Analyser/NodeScopeResolverTest.php +++ b/tests/PHPStan/Analyser/NodeScopeResolverTest.php @@ -983,6 +983,14 @@ public function dataFileAsserts(): iterable yield from $this->gatherAssertTypes(__DIR__ . '/data/composer-non-empty-array-after-unset.php'); yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Arrays/data/bug-6000.php'); yield from $this->gatherAssertTypes(__DIR__ . '/data/prestashop-breakdowns-empty-array.php'); + + if (PHP_VERSION_ID < 80000) { + yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-comparisons-php7.php'); + } + if (PHP_VERSION_ID >= 80000) { + yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-comparisons-php8.php'); + } + yield from $this->gatherAssertTypes(__DIR__ . '/data/loose-comparisons.php'); } /** diff --git a/tests/PHPStan/Analyser/data/loose-comparisons-php7.php b/tests/PHPStan/Analyser/data/loose-comparisons-php7.php new file mode 100644 index 0000000000..13b1d288e2 --- /dev/null +++ b/tests/PHPStan/Analyser/data/loose-comparisons-php7.php @@ -0,0 +1,49 @@ +