Skip to content

Commit

Permalink
dont combine empty string with numeric-string
Browse files Browse the repository at this point in the history
Fix #6646
  • Loading branch information
kkmuffme committed Dec 7, 2023
1 parent 93c7a8f commit 5fccb33
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/Psalm/Internal/Type/TypeCombiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,9 @@ private static function scrapeStringProperties(

if ($has_only_numeric_strings) {
$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) {
$combination->value_types['string'] = new TNonEmptyString();
} else {
Expand Down
32 changes: 32 additions & 0 deletions tests/TypeCombinationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,38 @@ function expectsTraversableOrArray($_a): void
}
',
],
'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);
}',
],
];
}

Expand Down

0 comments on commit 5fccb33

Please sign in to comment.