Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix conditional on non empty literal string #10912

Merged
merged 1 commit into from Apr 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Psalm/Internal/Type/Comparator/ScalarTypeComparator.php
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
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' => [],
],
];
}
}