From 4db65220f886bb6d4d826ae4dddc6f586e50d8c3 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 25 Dec 2024 11:39:10 +0100 Subject: [PATCH] Test loose comparison on constant types --- src/Type/Constant/ConstantArrayType.php | 4 ++++ .../Analyser/nsrt/loose-comparisons-php7.php | 8 ++++++++ .../Analyser/nsrt/loose-comparisons-php8.php | 8 ++++++++ .../PHPStan/Analyser/nsrt/loose-comparisons.php | 16 ++++++++++++++++ 4 files changed, 36 insertions(+) diff --git a/src/Type/Constant/ConstantArrayType.php b/src/Type/Constant/ConstantArrayType.php index 1514781d0f..750a5ecb83 100644 --- a/src/Type/Constant/ConstantArrayType.php +++ b/src/Type/Constant/ConstantArrayType.php @@ -418,6 +418,10 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType { + if ($type->isInteger()->yes()) { + return new ConstantBooleanType(false); + } + if ($this->isIterableAtLeastOnce()->no() && count($type->getConstantScalarValues()) === 1) { // @phpstan-ignore equal.invalid, equal.notAllowed return new ConstantBooleanType($type->getConstantScalarValues()[0] == []); // phpcs:ignore diff --git a/tests/PHPStan/Analyser/nsrt/loose-comparisons-php7.php b/tests/PHPStan/Analyser/nsrt/loose-comparisons-php7.php index 9e00dd0f65..737418a75a 100644 --- a/tests/PHPStan/Analyser/nsrt/loose-comparisons-php7.php +++ b/tests/PHPStan/Analyser/nsrt/loose-comparisons-php7.php @@ -60,5 +60,13 @@ public function sayInt( assertType('bool', $int == $emptyStr); assertType('bool', $int == $phpStr); assertType('bool', $int == 'a'); + + assertType('bool', 5 == $emptyStr); + assertType('bool', 5 == $phpStr); + assertType('bool', 5 == 'a'); + + assertType('bool', $emptyStr == 5); + assertType('bool', $phpStr == 5); + assertType('bool', 'a' == 5); } } diff --git a/tests/PHPStan/Analyser/nsrt/loose-comparisons-php8.php b/tests/PHPStan/Analyser/nsrt/loose-comparisons-php8.php index a3ca84cf64..b66a55e52d 100644 --- a/tests/PHPStan/Analyser/nsrt/loose-comparisons-php8.php +++ b/tests/PHPStan/Analyser/nsrt/loose-comparisons-php8.php @@ -66,6 +66,14 @@ public function sayInt( assertType('false', $intRange == $emptyStr); assertType('false', $intRange == $phpStr); assertType('false', $intRange == 'a'); + + assertType('false', 5 == $emptyStr); + assertType('false', 5 == $phpStr); + assertType('false', 5 == 'a'); + + assertType('false', $emptyStr == 5); + assertType('false', $phpStr == 5); + assertType('false', 'a' == 5); } } diff --git a/tests/PHPStan/Analyser/nsrt/loose-comparisons.php b/tests/PHPStan/Analyser/nsrt/loose-comparisons.php index 16c414170b..98da83057d 100644 --- a/tests/PHPStan/Analyser/nsrt/loose-comparisons.php +++ b/tests/PHPStan/Analyser/nsrt/loose-comparisons.php @@ -651,6 +651,12 @@ public function sayInt( assertType('false', $intRange == $emptyArr); assertType('false', $intRange == $array); + assertType('false', 5 == $emptyArr); + assertType('false', $emptyArr == 5); + assertType('false', 5 == $array); + assertType('false', $array == 5); + assertType('false', [] == 5); + assertType('false', 5 == []); } /** @@ -703,6 +709,7 @@ public function sayConstUnion( * @param lowercase-string $lower * @param array{} $emptyArr * @param non-empty-array $nonEmptyArr + * @param array{abc: string, num?: int, nullable: ?string} $arrShape * @param int<10, 20> $intRange */ public function sayIntersection( @@ -712,6 +719,7 @@ public function sayIntersection( array $emptyArr, array $nonEmptyArr, array $arr, + array $arrShape, int $i, int $intRange, ): void @@ -747,6 +755,14 @@ public function sayIntersection( assertType('false', $nonEmptyArr == $emptyArr); assertType('bool', $arr == $nonEmptyArr); assertType('bool', $nonEmptyArr == $arr); + assertType('false', 5 == $arr); + assertType('false', $arr == 5); + assertType('false', 5 == $emptyArr); + assertType('false', $emptyArr == 5); + assertType('false', 5 == $nonEmptyArr); + assertType('false', $nonEmptyArr == 5); + assertType('false', 5 == $arrShape); + assertType('false', $arrShape == 5); assertType('bool', '' == $lower); if ($lower != '') {