1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 12:24:49 +01:00

Fix #5017 - handle combining literal and non-empty strings

This commit is contained in:
Matt Brown 2021-01-17 12:22:29 -05:00 committed by Daniil Gentili
parent 0107afb2fb
commit 75d1f79a3d
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 36 additions and 0 deletions

View File

@ -931,6 +931,11 @@ class TypeCombiner
&& \strtolower($type->value) === $type->value
) {
// do nothing
} elseif (isset($combination->value_types['string'])
&& $combination->value_types['string'] instanceof Type\Atomic\TNonEmptyString
&& $type->value
) {
// do nothing
} else {
$combination->value_types['string'] = new TString();
}
@ -974,6 +979,23 @@ class TypeCombiner
$combination->value_types['string'] = $type;
}
$combination->strings = null;
} elseif ($type instanceof Type\Atomic\TNonEmptyString) {
$has_empty_string = false;
foreach ($combination->strings as $string_type) {
if (!$string_type->value) {
$has_empty_string = true;
break;
}
}
if ($has_empty_string) {
$combination->value_types['string'] = new TString();
} else {
$combination->value_types['string'] = $type;
}
$combination->strings = null;
} else {
$has_non_literal_class_string = false;

View File

@ -644,6 +644,20 @@ class TypeCombinationTest extends TestCase
'array{0?:int}',
]
],
'combinNonEmptyStringAndLiteral' => [
'non-empty-string',
[
'non-empty-string',
'"foo"',
]
],
'combinLiteralAndNonEmptyString' => [
'non-empty-string',
[
'"foo"',
'non-empty-string'
]
],
];
}