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

dont combine empty string with numeric-string

Fix https://github.com/vimeo/psalm/issues/6646
This commit is contained in:
kkmuffme 2023-12-07 16:12:03 +01:00
parent 93c7a8fd63
commit 5fccb33938
2 changed files with 35 additions and 0 deletions

View File

@ -1080,6 +1080,9 @@ final class TypeCombiner
if ($has_only_numeric_strings) { if ($has_only_numeric_strings) {
$combination->value_types['string'] = $type; $combination->value_types['string'] = $type;
} elseif (count($combination->strings) === 1 && !$has_only_non_empty_strings) {
$combination->value_types['string'] = $type;
return;
} elseif ($has_only_non_empty_strings) { } elseif ($has_only_non_empty_strings) {
$combination->value_types['string'] = new TNonEmptyString(); $combination->value_types['string'] = new TNonEmptyString();
} else { } else {

View File

@ -88,6 +88,38 @@ class TypeCombinationTest extends TestCase
} }
', ',
], ],
'emptyStringNumericStringDontCombine' => [
'code' => '<?php
/**
* @param numeric-string $arg
* @return void
*/
function takesNumeric($arg) {}
$b = rand(0, 10);
$a = $b < 5 ? "" : (string) $b;
if ($a !== "") {
takesNumeric($a);
}
/** @var ""|numeric-string $c */
if (is_numeric($c)) {
takesNumeric($c);
}',
],
'emptyStringNumericStringDontCombineNegation' => [
'code' => '<?php
/**
* @param ""|"hello" $arg
* @return void
*/
function takesLiteralString($arg) {}
/** @var ""|numeric-string $c */
if (!is_numeric($c)) {
takesLiteralString($c);
}',
],
]; ];
} }