Skip to content

Commit

Permalink
Test loose comparison on constant types
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Dec 25, 2024
1 parent 9f78100 commit 4db6522
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/nsrt/loose-comparisons-php7.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
8 changes: 8 additions & 0 deletions tests/PHPStan/Analyser/nsrt/loose-comparisons-php8.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}
16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/nsrt/loose-comparisons.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 == []);
}

/**
Expand Down Expand Up @@ -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(
Expand All @@ -712,6 +719,7 @@ public function sayIntersection(
array $emptyArr,
array $nonEmptyArr,
array $arr,
array $arrShape,
int $i,
int $intRange,
): void
Expand Down Expand Up @@ -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 != '') {
Expand Down

0 comments on commit 4db6522

Please sign in to comment.