From 4b10905b4041f131d96e51aaef9bb967d6c57133 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Mon, 29 Apr 2024 22:22:32 +0200 Subject: [PATCH] Fix literal-string|non-empty-literal-string --- src/Psalm/Internal/Type/TypeCombiner.php | 9 ++++++++- tests/TypeCombinationTest.php | 14 ++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/Psalm/Internal/Type/TypeCombiner.php b/src/Psalm/Internal/Type/TypeCombiner.php index 5e05fea1cff..c37b7eeeb5c 100644 --- a/src/Psalm/Internal/Type/TypeCombiner.php +++ b/src/Psalm/Internal/Type/TypeCombiner.php @@ -1227,9 +1227,16 @@ private static function scrapeStringProperties( ) { $combination->value_types['string'] = new TNonEmptyString(); } elseif (get_class($type) === TNonEmptyNonspecificLiteralString::class - && $combination->value_types['string'] instanceof TNonEmptyString + && ( + $combination->value_types['string'] instanceof TNonEmptyString + || $combination->value_types['string'] instanceof TNonspecificLiteralString + ) ) { // do nothing + } elseif (get_class($type) === TNonspecificLiteralString::class + && get_class($combination->value_types['string']) === TNonEmptyNonspecificLiteralString::class + ) { + $combination->value_types['string'] = $type; } else { $combination->value_types['string'] = new TString(); } diff --git a/tests/TypeCombinationTest.php b/tests/TypeCombinationTest.php index 8cab04fcacd..165ef58b6ea 100644 --- a/tests/TypeCombinationTest.php +++ b/tests/TypeCombinationTest.php @@ -947,6 +947,20 @@ public function providerTestValidTypeCombination(): array 'class-string', ], ], + 'unionNonEmptyLiteralStringAndLiteralString' => [ + 'literal-string', + [ + 'non-empty-literal-string', + 'literal-string', + ], + ], + 'unionLiteralStringAndNonEmptyLiteralString' => [ + 'literal-string', + [ + 'literal-string', + 'non-empty-literal-string', + ], + ], ]; }