1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-27 04:45:20 +01:00

Allow weak equality between scalar types

This commit is contained in:
Matt Brown 2018-05-14 10:36:41 -04:00
parent af8705502e
commit 58c68c355e
2 changed files with 20 additions and 1 deletions

View File

@ -663,6 +663,8 @@ class Reconciler
); );
} }
$any_scalar_type_match_found = false;
foreach ($new_type->getTypes() as $new_type_part) { foreach ($new_type->getTypes() as $new_type_part) {
$has_local_match = false; $has_local_match = false;
@ -689,6 +691,10 @@ class Reconciler
break; break;
} }
if ($scalar_type_match_found) {
$any_scalar_type_match_found = true;
}
if ($new_type_part instanceof TCallable && if ($new_type_part instanceof TCallable &&
( (
$existing_var_type_part instanceof TString || $existing_var_type_part instanceof TString ||
@ -712,7 +718,7 @@ class Reconciler
} }
} }
if (!$has_match) { if (!$has_match && (!$is_loose_equality || !$any_scalar_type_match_found)) {
if ($new_var_type === 'null') { if ($new_var_type === 'null') {
if ($existing_var_type->from_docblock) { if ($existing_var_type->from_docblock) {
if (IssueBuffer::accepts( if (IssueBuffer::accepts(

View File

@ -819,6 +819,12 @@ class TypeReconciliationTest extends TestCase
} }
}', }',
], ],
'allowWeakEqualityScalarType' => [
'<?php
function foo(int $i) : void {
if ($i == "5") {}
}',
],
]; ];
} }
@ -948,6 +954,13 @@ class TypeReconciliationTest extends TestCase
if (is_bool($a[0]) && $a[0]) {}', if (is_bool($a[0]) && $a[0]) {}',
'error_message' => 'DocblockTypeContradiction', 'error_message' => 'DocblockTypeContradiction',
], ],
'preventWeakEqualityToObject' => [
'<?php
function foo(int $i, stdClass $s) : void {
if ($i == $s) {}
}',
'error_message' => 'TypeDoesNotContainType',
],
]; ];
} }
} }