Skip to content

Commit

Permalink
ensure concat https://psalm.dev/r/323e33ae8a will be a non-falsy-string
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmuffme committed Jan 13, 2024
1 parent 4092618 commit 7f0f0bc
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -258,16 +258,30 @@ public static function analyze(

$has_numeric_and_non_empty = $has_numeric_type && $has_non_empty;

$non_falsy_string = $numeric_type->getBuilder()->addType(new TNonFalsyString())->freeze();
$left_non_falsy = UnionTypeComparator::isContainedBy(
$codebase,
$left_type,
$non_falsy_string,
);

$right_non_falsy = UnionTypeComparator::isContainedBy(
$codebase,
$right_type,
$non_falsy_string,
);

$all_literals = $left_type->allLiterals() && $right_type->allLiterals();

if ($has_non_empty) {
if ($all_literals) {
$result_type = new Union([new TNonEmptyNonspecificLiteralString]);
} elseif ($all_lowercase) {
$result_type = Type::getNonEmptyLowercaseString();
} elseif ($all_non_empty || $has_numeric_and_non_empty || $left_non_falsy || $right_non_falsy) {
$result_type = Type::getNonFalsyString();
} else {
$result_type = $all_non_empty || $has_numeric_and_non_empty ?
Type::getNonFalsyString() : Type::getNonEmptyString();
$result_type = Type::getNonEmptyString();
}
} else {
if ($all_literals) {
Expand Down
9 changes: 9 additions & 0 deletions tests/BinaryOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,15 @@ public function providerValidCodeParse(): iterable
'code' => '<?php
$a = "Hey " . "Jude,";',
],
'concatenationNonFalsyLiteralStringWithString' => [
'code' => '<?php
function foo(): string {}
$a = "Hey " . foo();',
'assertions' => [
'$a===' => 'non-falsy-string',
],
'ignored_issues' => ['InvalidReturnType'],
],
'concatenationWithNumberInWeakMode' => [
'code' => '<?php
$a = "hi" . 5;',
Expand Down

0 comments on commit 7f0f0bc

Please sign in to comment.