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:
parent
f83b6bb178
commit
f3b6846c70
@ -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;
|
||||||
|
@ -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");
|
||||||
|
}'
|
||||||
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user