Skip to content

Commit

Permalink
Merge pull request #10912 from VincentLanglet/conditionalNonEmptyLiteral
Browse files Browse the repository at this point in the history
Fix conditional on non empty literal string
  • Loading branch information
orklah authored Apr 28, 2024
2 parents 2e84721 + 3dea282 commit 81e4b97
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Psalm/Internal/Type/Comparator/ScalarTypeComparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ public static function isContainedBy(
if ($container_type_part instanceof TNonspecificLiteralString
&& ($input_type_part instanceof TLiteralString || $input_type_part instanceof TNonspecificLiteralString)
) {
if ($container_type_part instanceof TNonEmptyNonspecificLiteralString) {
return ($input_type_part instanceof TLiteralString && $input_type_part->value !== '')
|| $input_type_part instanceof TNonEmptyNonspecificLiteralString;
}

return 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 @@ -951,6 +951,34 @@ final class SpecificObject extends stdClass {}
'ignored_issues' => [],
'php_version' => '8.1',
],
'nonEmptyLiteralString' => [
'code' => '<?php
/**
* @param literal-string $string
* @psalm-return ($string is non-empty-literal-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 81e4b97

Please sign in to comment.