Skip to content

Commit

Permalink
Fix condition on literal-string
Browse files Browse the repository at this point in the history
  • Loading branch information
VincentLanglet committed Apr 28, 2024
1 parent c631102 commit c0a9d04
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Psalm/Internal/Type/Comparator/ScalarTypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,10 @@ public static function isContainedBy(
}

if ($container_type_part instanceof TNonEmptyString
&& get_class($input_type_part) === TString::class
&& (
get_class($input_type_part) === TString::class
|| get_class($input_type_part) === TNonspecificLiteralString::class
)
) {
if ($atomic_comparison_result) {
$atomic_comparison_result->type_coerced = true;
Expand Down
28 changes: 28 additions & 0 deletions tests/Template/ConditionalReturnTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,34 @@ function getSomething(string $string)
],
'ignored_issues' => [],
],
'literalStringIsNotNonEmpty' => [
'code' => '<?php
/**
* @param literal-string $string
* @psalm-return ($string is non-empty-string ? string : int)
*/
function getSomething(string $string)
{
if (!$string) {
return 1;
}
return "";
}
/** @var literal-string $literalString */
$literalString;
$something = getSomething($literalString);
/** @var non-empty-literal-string $nonEmptyliteralString */
$nonEmptyliteralString;
$something2 = getSomething($nonEmptyliteralString);
',
'assertions' => [
'$something' => 'int|string',
'$something2' => 'string',
],
'ignored_issues' => [],
],
];
}
}

0 comments on commit c0a9d04

Please sign in to comment.