Skip to content

Commit

Permalink
loose comparisons of int with string shouldn't change change the int …
Browse files Browse the repository at this point in the history
…to empty-mixed
  • Loading branch information
kkmuffme committed Nov 18, 2023
1 parent 69d3ec7 commit 6e5af03
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/Psalm/Internal/Type/SimpleAssertionReconciler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,10 @@ private static function reconcileString(
$string_types[] = $type;
}

$redundant = false;
} elseif ($type instanceof TInt && $assertion instanceof IsLooselyEqual) {
// don't change the type of an int for non-strict comparisons
$string_types[] = $type;
$redundant = false;
} else {
$redundant = false;
Expand Down
3 changes: 1 addition & 2 deletions tests/TypeReconciliation/ConditionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ function foo($length) {
}
}',
'assertions' => [],
'ignored_issues' => ['DocblockTypeContradiction'],
'ignored_issues' => ['DocblockTypeContradiction', 'TypeDoesNotContainType'],
],
'notInstanceof' => [
'code' => '<?php
Expand Down Expand Up @@ -1350,7 +1350,6 @@ function takes_string(string $string) : void {}
function takes_int(int $int) : void {}
if ($int == $string) {
/** @psalm-suppress MixedArgument */
takes_int($int);
}',
],
Expand Down
8 changes: 6 additions & 2 deletions tests/UnusedVariableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2143,15 +2143,19 @@ function foo(array $clips) : void {
*/
function validate($b, string $source) : void {
/**
* @psalm-suppress DocblockTypeContradiction
* @psalm-suppress MixedAssignment
* @psalm-suppress RedundantConditionGivenDocblockType
* @var bool|string $b
*/
if (!is_bool($b)) {
$source = $b;
$b = false;
}
print_r($source);
}',
'assertions' => [
'$b===' => 'bool',
],
],
'implicitSpread' => [
'code' => '<?php
Expand Down

0 comments on commit 6e5af03

Please sign in to comment.