1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Fix #4527 - improve interpolated string types

This commit is contained in:
Matt Brown 2020-11-11 00:38:26 -05:00 committed by Daniil Gentili
parent f83b6bb178
commit f3b6846c70
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 28 additions and 0 deletions

View File

@ -18,7 +18,15 @@ class EncapsulatedStringAnalyzer
) : bool { ) : bool {
$stmt_type = Type::getString(); $stmt_type = Type::getString();
$non_empty = false;
foreach ($stmt->parts as $part) { foreach ($stmt->parts as $part) {
if ($part instanceof PhpParser\Node\Scalar\EncapsedStringPart
&& $part->value
) {
$non_empty = true;
}
if (ExpressionAnalyzer::analyze($statements_analyzer, $part, $context) === false) { if (ExpressionAnalyzer::analyze($statements_analyzer, $part, $context) === false) {
return false; return false;
} }
@ -52,6 +60,12 @@ class EncapsulatedStringAnalyzer
} }
} }
if ($non_empty) {
$new_type = new Type\Union([new Type\Atomic\TNonEmptyString()]);
$new_type->parent_nodes = $stmt_type->parent_nodes;
$stmt_type = $new_type;
}
$statements_analyzer->node_data->setType($stmt, $stmt_type); $statements_analyzer->node_data->setType($stmt, $stmt_type);
return true; return true;

View File

@ -304,6 +304,20 @@ class BinaryOperationTest extends TestCase
if (is_int($s)) {} if (is_int($s)) {}
}' }'
], ],
'interpolatedStringNotEmpty' => [
'<?php
/**
* @psalm-param non-empty-string $i
*/
function func($i): string
{
return $i;
}
function foo(string $a) : void {
func("asdasdasd $a");
}'
],
]; ];
} }