Skip to content

Commit

Permalink
Fix ConstantArray loose comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Dec 25, 2024
1 parent 9e3d1d3 commit 7a3f001
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
11 changes: 8 additions & 3 deletions src/Type/Constant/ConstantArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,14 @@ public function looseCompare(Type $type, PhpVersion $phpVersion): BooleanType
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
if ($this->isIterableAtLeastOnce()->no()) {
if (count($type->getConstantScalarValues()) === 1) {
// @phpstan-ignore equal.invalid, equal.notAllowed
return new ConstantBooleanType($type->getConstantScalarValues()[0] == []); // phpcs:ignore
}
if ($type->isIterableAtLeastOnce()->yes()) {
return new ConstantBooleanType(false);
}
}

return new BooleanType();
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/loose-comparisons.php
Original file line number Diff line number Diff line change
Expand Up @@ -761,7 +761,7 @@ public function sayIntersection(
assertType('false', $nonEmptyArr == $i);
assertType('false', $arr == $intRange);
assertType('false', $nonEmptyArr == $intRange);
assertType('bool', $emptyArr == $nonEmptyArr); // should be false
assertType('false', $emptyArr == $nonEmptyArr);
assertType('false', $nonEmptyArr == $emptyArr);
assertType('bool', $arr == $nonEmptyArr);
assertType('bool', $nonEmptyArr == $arr);
Expand Down

0 comments on commit 7a3f001

Please sign in to comment.