Skip to content

Commit

Permalink
Merge pull request #10459 from kkmuffme/dont-combine-empty-string-wit…
Browse files Browse the repository at this point in the history
…h-numeric-string

dont combine empty string with numeric-string
  • Loading branch information
orklah committed Dec 9, 2023
2 parents 93c7a8f + 5fccb33 commit 5e221f3
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
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
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 5e221f3

Please sign in to comment.