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 non-empty-lowercase-string handling with literal non-lowercase strings #10722

Merged
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 47 additions & 5 deletions src/Psalm/Internal/Type/TypeCombiner.php
Original file line number Diff line number Diff line change
Expand Up @@ -1047,12 +1047,19 @@ private static function scrapeStringProperties(
&& strtolower($type->value) === $type->value
) {
// do nothing
} elseif (isset($combination->value_types['string'])
&& $combination->value_types['string'] instanceof TNonFalsyString
&& $type->value
) {
// do nothing
} elseif (isset($combination->value_types['string'])
&& $combination->value_types['string'] instanceof TNonFalsyString
&& $type->value === '0'
) {
$combination->value_types['string'] = new TNonEmptyString();
} elseif (isset($combination->value_types['string'])
&& $combination->value_types['string'] instanceof TNonEmptyString
&& ($combination->value_types['string'] instanceof TNonFalsyString
? $type->value
: $type->value !== ''
)
&& $type->value !== ''
) {
// do nothing
} else {
Expand Down Expand Up @@ -1103,18 +1110,53 @@ private static function scrapeStringProperties(
} else {
$combination->value_types['string'] = $type;
}
} elseif ($type instanceof TNonFalsyString) {
$has_empty_string = false;
$has_falsy_string = false;

foreach ($combination->strings as $string_type) {
if ($string_type->value === '') {
$has_empty_string = true;
$has_falsy_string = true;
break;
}

if ($string_type->value === '0') {
$has_falsy_string = true;
}
}

if ($has_empty_string) {
$combination->value_types['string'] = new TString();
} elseif ($has_falsy_string) {
$combination->value_types['string'] = new TNonEmptyString();
} else {
$combination->value_types['string'] = $type;
}
} elseif ($type instanceof TNonEmptyString) {
$has_empty_string = false;

foreach ($combination->strings as $string_type) {
if (!$string_type->value) {
if ($string_type->value === '') {
$has_empty_string = true;
break;
}
}

$has_non_lowercase_string = false;
if ($type instanceof TNonEmptyLowercaseString) {
foreach ($combination->strings as $string_type) {
if (strtolower($string_type->value) !== $string_type->value) {
$has_non_lowercase_string = true;
break;
}
}
}

if ($has_empty_string) {
$combination->value_types['string'] = new TString();
} elseif ($has_non_lowercase_string && get_class($type) !== TNonEmptyString::class) {
$combination->value_types['string'] = new TNonEmptyString();
} else {
$combination->value_types['string'] = $type;
}
Expand Down
36 changes: 35 additions & 1 deletion tests/TypeCombinationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,40 @@ function takesLiteralString($arg) {}
'$x===' => 'non-falsy-string',
],
],
'loopNonFalsyWithZeroShouldBeNonEmpty' => [
'code' => '<?php
/**
* @psalm-suppress InvalidReturnType
* @return string[]
*/
function getStringArray() {}

$x = array();
foreach (getStringArray() as $id) {
$x[] = "0";
$x[] = "some_" . $id;
}',
'assertions' => [
'$x===' => 'list<non-empty-string>',
],
],
'loopNonLowercaseLiteralWithNonEmptyLowercaseShouldBeNonEmptyAndNotLowercase' => [
'code' => '<?php
/**
* @psalm-suppress InvalidReturnType
* @return int[]
*/
function getIntArray() {}

$x = array();
foreach (getIntArray() as $id) {
$x[] = "TEXT";
$x[] = "some_" . $id;
}',
'assertions' => [
'$x===' => 'list<non-empty-string>',
],
],
];
}

Expand Down Expand Up @@ -900,7 +934,7 @@ public function providerTestValidTypeCombination(): array
],
],
'nonFalsyStringAndFalsyLiteral' => [
'string',
'non-empty-string',
[
'non-falsy-string',
'"0"',
Expand Down
Loading