1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix #814 - remove literal types when removing base key

This commit is contained in:
Matt Brown 2018-06-12 11:19:35 -04:00
parent 3891b08fc2
commit 45a94af015
2 changed files with 51 additions and 0 deletions

View File

@ -399,6 +399,21 @@ class Union
$this->id = null;
return true;
} elseif ($type_string === 'string' && $this->literal_string_types) {
foreach ($this->literal_string_types as $literal_key => $_) {
unset($this->types[$literal_key]);
}
$this->literal_string_types = [];
} elseif ($type_string === 'int' && $this->literal_int_types) {
foreach ($this->literal_int_types as $literal_key => $_) {
unset($this->types[$literal_key]);
}
$this->literal_int_types = [];
} elseif ($type_string === 'float' && $this->literal_float_types) {
foreach ($this->literal_float_types as $literal_key => $_) {
unset($this->types[$literal_key]);
}
$this->literal_float_types = [];
}
return false;

View File

@ -369,6 +369,42 @@ class ValueTest extends TestCase
}
}',
],
'removeLiteralStringForNotIsString' => [
'<?php
function takesInt(int $i) : void {}
$f = ["a", "b", "c"];
$f[rand(0, 2)] = 5;
$i = rand(0, 2);
if (isset($f[$i]) && !is_string($f[$i])) {
takesInt($f[$i]);
}'
],
'removeLiteralIntForNotIsInt' => [
'<?php
function takesString(string $i) : void {}
$f = [0, 1, 2];
$f[rand(0, 2)] = "hello";
$i = rand(0, 2);
if (isset($f[$i]) && !is_int($f[$i])) {
takesString($f[$i]);
}'
],
'removeLiteralFloatForNotIsFloat' => [
'<?php
function takesString(string $i) : void {}
$f = [1.1, 1.2, 1.3];
$f[rand(0, 2)] = "hello";
$i = rand(0, 2);
if (isset($f[$i]) && !is_float($f[$i])) {
takesString($f[$i]);
}'
],
];
}