1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Fix #1399 - allow literal comparison to scalar

This commit is contained in:
Brown 2019-02-27 16:21:23 -05:00
parent 07825c9ff8
commit f8c2b2d81a
2 changed files with 18 additions and 3 deletions

View File

@ -2001,7 +2001,7 @@ class Reconciler
if ($scalar_type === 'int') {
$value = (int) $value;
if ($existing_var_type->hasMixed()) {
if ($existing_var_type->hasMixed() || $existing_var_type->hasScalar()) {
return new Type\Union([new Type\Atomic\TLiteralInt($value)]);
}
@ -2085,7 +2085,7 @@ class Reconciler
}
}
} elseif ($scalar_type === 'string' || $scalar_type === 'class-string') {
if ($existing_var_type->hasMixed()) {
if ($existing_var_type->hasMixed() || $existing_var_type->hasScalar()) {
if ($scalar_type === 'class-string') {
return new Type\Union([new Type\Atomic\TLiteralClassString($value)]);
}
@ -2144,7 +2144,7 @@ class Reconciler
} elseif ($scalar_type === 'float') {
$value = (float) $value;
if ($existing_var_type->hasMixed()) {
if ($existing_var_type->hasMixed() || $existing_var_type->hasScalar()) {
return new Type\Union([new Type\Atomic\TLiteralFloat($value)]);
}

View File

@ -1268,6 +1268,21 @@ class TypeReconciliationTest extends TestCase
return $a;
}',
],
'dontChangeScalar' => [
'<?php
/**
* @param scalar|null $val
*/
function foo($val) : ? bool {
if ("1" === $val || 1 === $val) {
return true;
} elseif ("0" === $val || 0 === $val) {
return false;
}
return null;
}'
],
];
}