Skip to content

Commit

Permalink
Support 'literal-string' with Encapsed Strings
Browse files Browse the repository at this point in the history
  • Loading branch information
craigfrancis committed Nov 16, 2021
1 parent 084cb40 commit 602ce24
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
22 changes: 18 additions & 4 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -1459,13 +1459,27 @@ private function resolveType(Expr $node): Type
continue;
}

$isNonEmpty = false;
$isLiteralString = true;
foreach ($parts as $partType) {
if ($partType->isNonEmptyString()->yes()) {
return new IntersectionType([
new StringType(),
new AccessoryNonEmptyStringType(),
]);
$isNonEmpty = true;
}
if (!$partType->isLiteralString()->yes()) {
$isLiteralString = false;
}
}

$accessoryTypes = [];
if ($isNonEmpty === true) {
$accessoryTypes[] = new AccessoryNonEmptyStringType();
}
if ($isLiteralString === true) {
$accessoryTypes[] = new AccessoryLiteralStringType();
}
if (count($accessoryTypes) > 0) {
$accessoryTypes[] = new StringType();
return new IntersectionType($accessoryTypes);
}

return new StringType();
Expand Down
2 changes: 2 additions & 0 deletions tests/PHPStan/Analyser/data/literal-string.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ public function doFoo($literalString, string $string)
assertType('literal-string', '' . $literalString);
assertType('literal-string&non-empty-string', $literalString . 'foo');
assertType('literal-string&non-empty-string', 'foo' . $literalString);
assertType('literal-string&non-empty-string', "foo ${literalString}");
assertType('literal-string&non-empty-string', "${literalString} foo");
assertType('string', $string . '');
assertType('string', '' . $string);
assertType('string', $literalString . $string);
Expand Down

0 comments on commit 602ce24

Please sign in to comment.