Skip to content

Commit

Permalink
Encapsed non-literal non-empty/falsy strings always return generic st…
Browse files Browse the repository at this point in the history
…ring

Fix #10944
  • Loading branch information
kkmuffme committed May 5, 2024
1 parent 0e09a19 commit 979cfc8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public static function analyze(

if (!$casted_part_type->allLiterals()) {
$all_literals = false;
} elseif (!$non_falsy) {
}

if (!$non_falsy) {
// Check if all literals are nonempty
$possibly_non_empty = true;
$non_falsy = true;
Expand All @@ -98,9 +100,16 @@ public static function analyze(
&& !$atomic_literal instanceof TNonEmptyNonspecificLiteralString
&& !($atomic_literal instanceof TLiteralString && $atomic_literal->value !== "")
) {
$possibly_non_empty = false;
$non_falsy = false;
break;
if (!$atomic_literal instanceof TNonFalsyString) {
$non_falsy = false;
}

if (!$atomic_literal instanceof TNonEmptyString) {
$possibly_non_empty = false;
$non_falsy = false;

break;
}
}
}

Expand Down
36 changes: 36 additions & 0 deletions tests/BinaryOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,42 @@ function foo(string $s1): string {
$interpolated = "hello {$foo} bar";',
'assertions' => ['$interpolated===' => "non-falsy-string"],
],
'encapsedStringWithoutLiterals1' => [
'code' => '<?php
/**
* @var non-falsy-string $a
* @var non-falsy-string $b
*/
$interpolated = "{$a}{$b}";',
'assertions' => ['$interpolated===' => "non-falsy-string"],
],
'encapsedStringWithoutLiterals2' => [
'code' => '<?php
/**
* @var string $a
* @var non-empty-string $b
*/
$interpolated = "{$a}{$b}";',
'assertions' => ['$interpolated===' => "non-empty-string"],
],
'encapsedStringWithoutLiterals3' => [
'code' => '<?php
/**
* @var non-empty-string $a
* @var non-falsy-string $b
*/
$interpolated = "{$a}{$b}";',
'assertions' => ['$interpolated===' => "non-falsy-string"],
],
'encapsedStringWithoutLiterals4' => [
'code' => '<?php
/**
* @var string $a
* @var non-falsy-string $b
*/
$interpolated = "{$a}{$b}";',
'assertions' => ['$interpolated===' => "non-falsy-string"],
],
'encapsedStringIsInferredAsLiteral' => [
'code' => '<?php
$int = 1;
Expand Down

0 comments on commit 979cfc8

Please sign in to comment.