Skip to content

Commit 4db6522

Browse files
committed
Test loose comparison on constant types
1 parent 9f78100 commit 4db6522

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

src/Type/Constant/ConstantArrayType.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,10 @@ public function isSuperTypeOf(Type $type): IsSuperTypeOfResult
418418

419419
public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
420420
{
421+
if ($type->isInteger()->yes()) {
422+
return new ConstantBooleanType(false);
423+
}
424+
421425
if ($this->isIterableAtLeastOnce()->no() && count($type->getConstantScalarValues()) === 1) {
422426
// @phpstan-ignore equal.invalid, equal.notAllowed
423427
return new ConstantBooleanType($type->getConstantScalarValues()[0] == []); // phpcs:ignore

tests/PHPStan/Analyser/nsrt/loose-comparisons-php7.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,5 +60,13 @@ public function sayInt(
6060
assertType('bool', $int == $emptyStr);
6161
assertType('bool', $int == $phpStr);
6262
assertType('bool', $int == 'a');
63+
64+
assertType('bool', 5 == $emptyStr);
65+
assertType('bool', 5 == $phpStr);
66+
assertType('bool', 5 == 'a');
67+
68+
assertType('bool', $emptyStr == 5);
69+
assertType('bool', $phpStr == 5);
70+
assertType('bool', 'a' == 5);
6371
}
6472
}

tests/PHPStan/Analyser/nsrt/loose-comparisons-php8.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ public function sayInt(
6666
assertType('false', $intRange == $emptyStr);
6767
assertType('false', $intRange == $phpStr);
6868
assertType('false', $intRange == 'a');
69+
70+
assertType('false', 5 == $emptyStr);
71+
assertType('false', 5 == $phpStr);
72+
assertType('false', 5 == 'a');
73+
74+
assertType('false', $emptyStr == 5);
75+
assertType('false', $phpStr == 5);
76+
assertType('false', 'a' == 5);
6977
}
7078

7179
}

tests/PHPStan/Analyser/nsrt/loose-comparisons.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,12 @@ public function sayInt(
651651
assertType('false', $intRange == $emptyArr);
652652
assertType('false', $intRange == $array);
653653

654+
assertType('false', 5 == $emptyArr);
655+
assertType('false', $emptyArr == 5);
656+
assertType('false', 5 == $array);
657+
assertType('false', $array == 5);
658+
assertType('false', [] == 5);
659+
assertType('false', 5 == []);
654660
}
655661

656662
/**
@@ -703,6 +709,7 @@ public function sayConstUnion(
703709
* @param lowercase-string $lower
704710
* @param array{} $emptyArr
705711
* @param non-empty-array $nonEmptyArr
712+
* @param array{abc: string, num?: int, nullable: ?string} $arrShape
706713
* @param int<10, 20> $intRange
707714
*/
708715
public function sayIntersection(
@@ -712,6 +719,7 @@ public function sayIntersection(
712719
array $emptyArr,
713720
array $nonEmptyArr,
714721
array $arr,
722+
array $arrShape,
715723
int $i,
716724
int $intRange,
717725
): void
@@ -747,6 +755,14 @@ public function sayIntersection(
747755
assertType('false', $nonEmptyArr == $emptyArr);
748756
assertType('bool', $arr == $nonEmptyArr);
749757
assertType('bool', $nonEmptyArr == $arr);
758+
assertType('false', 5 == $arr);
759+
assertType('false', $arr == 5);
760+
assertType('false', 5 == $emptyArr);
761+
assertType('false', $emptyArr == 5);
762+
assertType('false', 5 == $nonEmptyArr);
763+
assertType('false', $nonEmptyArr == 5);
764+
assertType('false', 5 == $arrShape);
765+
assertType('false', $arrShape == 5);
750766

751767
assertType('bool', '' == $lower);
752768
if ($lower != '') {

0 commit comments

Comments
 (0)