Skip to content

Commit

Permalink
Fix non-empty encapsed string check.
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrolGenhald committed Jun 25, 2022
1 parent a804e45 commit 3aea098
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,12 @@
use Psalm\Type\Atomic\TFalse;
use Psalm\Type\Atomic\TFloat;
use Psalm\Type\Atomic\TInt;
use Psalm\Type\Atomic\TLiteralFloat;
use Psalm\Type\Atomic\TLiteralInt;
use Psalm\Type\Atomic\TLiteralString;
use Psalm\Type\Atomic\TLowercaseString;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Atomic\TNonEmptyNonspecificLiteralString;
use Psalm\Type\Atomic\TNonEmptyString;
use Psalm\Type\Atomic\TNonspecificLiteralInt;
use Psalm\Type\Atomic\TNonspecificLiteralString;
use Psalm\Type\Atomic\TNull;
use Psalm\Type\Atomic\TString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Psalm\Type\Atomic\TNonEmptyNonspecificLiteralString;
use Psalm\Type\Atomic\TNonEmptyString;
use Psalm\Type\Atomic\TNonspecificLiteralInt;
use Psalm\Type\Atomic\TNonspecificLiteralString;
use Psalm\Type\Union;

use function in_array;
Expand Down Expand Up @@ -55,20 +56,23 @@ public static function analyze(
$all_literals = false;
} elseif (!$non_empty) {
// Check if all literals are nonempty
$non_empty = true;
foreach ($casted_part_type->getAtomicTypes() as $atomic_literal) {
$non_empty = $atomic_literal instanceof TLiteralInt
|| $atomic_literal instanceof TNonspecificLiteralInt
|| $atomic_literal instanceof TLiteralFloat
|| $atomic_literal instanceof TNonEmptyNonspecificLiteralString
|| ($atomic_literal instanceof TLiteralString && $atomic_literal->value !== "")
;
if (!$atomic_literal instanceof TLiteralInt
&& !$atomic_literal instanceof TNonspecificLiteralInt
&& !$atomic_literal instanceof TLiteralFloat
&& !$atomic_literal instanceof TNonEmptyNonspecificLiteralString
&& !($atomic_literal instanceof TLiteralString && $atomic_literal->value !== "")
) {
$non_empty = false;
break;
}
}
}

if ($literal_string !== null) {
if ($casted_part_type->isSingleLiteral()) {
$literal_string .= $casted_part_type->getSingleLiteral()->value;
if (!$non_empty && $literal_string !== "") {}
} else {
$literal_string = null;
}
Expand Down Expand Up @@ -121,7 +125,11 @@ public static function analyze(
} else {
$new_type = new Union([new TNonEmptyString()]);
}

} elseif ($all_literals) {
$new_type = new Union([new TNonspecificLiteralString()]);
}
if (isset($new_type)) {
assert($new_type instanceof Union);
$new_type->parent_nodes = $stmt_type->parent_nodes;
$stmt_type = $new_type;
}
Expand Down
10 changes: 10 additions & 0 deletions tests/BinaryOperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,16 @@ function foo(string $s1): string {
',
'assertions' => ['$concatenated===' => 'non-empty-literal-string'],
],
'encapsedPossiblyEmptyLiteralString' => [
'<?php
/** @var "foo"|"" */
$foo = "";
/** @var "bar"|"" */
$bar = "";
$interpolated = "{$foo}{$bar}";
',
'assertions' => ['$interpolated===' => 'literal-string'],
],
'literalIntConcatCreatesLiteral' => [
'<?php
/**
Expand Down

0 comments on commit 3aea098

Please sign in to comment.