Skip to content

Commit 3dea282

Browse files
Fix conditional on non empty literal string
1 parent 7d6c88e commit 3dea282

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

src/Psalm/Internal/Type/Comparator/ScalarTypeComparator.php

+5
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ public static function isContainedBy(
8686
if ($container_type_part instanceof TNonspecificLiteralString
8787
&& ($input_type_part instanceof TLiteralString || $input_type_part instanceof TNonspecificLiteralString)
8888
) {
89+
if ($container_type_part instanceof TNonEmptyNonspecificLiteralString) {
90+
return ($input_type_part instanceof TLiteralString && $input_type_part->value !== '')
91+
|| $input_type_part instanceof TNonEmptyNonspecificLiteralString;
92+
}
93+
8994
return true;
9095
}
9196

tests/Template/ConditionalReturnTypeTest.php

+28
Original file line numberDiff line numberDiff line change
@@ -951,6 +951,34 @@ final class SpecificObject extends stdClass {}
951951
'ignored_issues' => [],
952952
'php_version' => '8.1',
953953
],
954+
'nonEmptyLiteralString' => [
955+
'code' => '<?php
956+
/**
957+
* @param literal-string $string
958+
* @psalm-return ($string is non-empty-literal-string ? string : int)
959+
*/
960+
function getSomething(string $string)
961+
{
962+
if (!$string) {
963+
return 1;
964+
}
965+
966+
return "";
967+
}
968+
969+
/** @var literal-string $literalString */
970+
$literalString;
971+
$something = getSomething($literalString);
972+
/** @var non-empty-literal-string $nonEmptyliteralString */
973+
$nonEmptyliteralString;
974+
$something2 = getSomething($nonEmptyliteralString);
975+
',
976+
'assertions' => [
977+
'$something' => 'int|string',
978+
'$something2' => 'string',
979+
],
980+
'ignored_issues' => [],
981+
],
954982
];
955983
}
956984
}

0 commit comments

Comments
 (0)