1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #980 - allow literal int types to be coerced from mixed

This commit is contained in:
Matthew Brown 2018-09-09 13:01:16 -04:00
parent 2c3244e93b
commit 80cd77832b
2 changed files with 40 additions and 0 deletions

View File

@ -1393,6 +1393,10 @@ class Reconciler
if ($scalar_type === 'int') {
$value = (int) $value;
if ($existing_var_type->isMixed()) {
return new Type\Union([new Type\Atomic\TLiteralInt($value)]);
}
if ($existing_var_type->hasInt()) {
$existing_int_types = $existing_var_type->getLiteralInts();
@ -1428,6 +1432,10 @@ class Reconciler
}
}
} elseif ($scalar_type === 'string' || $scalar_type === 'class-string') {
if ($existing_var_type->isMixed()) {
return new Type\Union([new Type\Atomic\TLiteralString($value)]);
}
if ($existing_var_type->hasString()) {
$existing_string_types = $existing_var_type->getLiteralStrings();
@ -1469,6 +1477,10 @@ class Reconciler
} elseif ($scalar_type === 'float') {
$value = (float) $value;
if ($existing_var_type->isMixed()) {
return new Type\Union([new Type\Atomic\TLiteralFloat($value)]);
}
if ($existing_var_type->hasFloat()) {
$existing_float_types = $existing_var_type->getLiteralFloats();

View File

@ -419,6 +419,34 @@ class ValueTest extends TestCase
do {} while (--$i > 0);
echo $i === 0;',
],
'coerceFromMixed' => [
'<?php
function type(int $b): void {}
/**
* @param mixed $a
*/
function foo($a) : void {
if ($a === 1 || $a === 2) {
type($a);
}
if (in_array($a, [1, 2], true)) {
type($a);
}
}',
],
'coerceFromString' => [
'<?php
/** @param "a"|"b" $b */
function type(string $b): void {}
function foo(string $a) : void {
if ($a === "a" || $a === "b") {
type($a);
}
}',
],
];
}