1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-29 20:28:59 +01:00

Memoize coalesce expression in more scenarios

This commit is contained in:
Matt Brown 2020-12-02 02:15:17 -05:00 committed by Daniil Gentili
parent 7cc0f65f11
commit fd7afc9c40
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7

View File

@ -20,10 +20,18 @@ class CoalesceAnalyzer
) : bool {
$left_expr = $stmt->left;
if ($left_expr instanceof PhpParser\Node\Expr\FuncCall
|| $left_expr instanceof PhpParser\Node\Expr\MethodCall
|| $left_expr instanceof PhpParser\Node\Expr\StaticCall
|| $left_expr instanceof PhpParser\Node\Expr\Cast
$root_expr = $left_expr;
while ($root_expr instanceof PhpParser\Node\Expr\ArrayDimFetch
|| $root_expr instanceof PhpParser\Node\Expr\PropertyFetch
) {
$root_expr = $root_expr->var;
}
if ($root_expr instanceof PhpParser\Node\Expr\FuncCall
|| $root_expr instanceof PhpParser\Node\Expr\MethodCall
|| $root_expr instanceof PhpParser\Node\Expr\StaticCall
|| $root_expr instanceof PhpParser\Node\Expr\Cast
) {
$left_var_id = '$<tmp coalesce var>' . (int) $left_expr->getAttribute('startFilePos');
@ -31,6 +39,10 @@ class CoalesceAnalyzer
$condition_type = $statements_analyzer->node_data->getType($left_expr) ?: Type::getMixed();
if ($root_expr !== $left_expr) {
$condition_type->possibly_undefined = true;
}
$context->vars_in_scope[$left_var_id] = $condition_type;
$left_expr = new PhpParser\Node\Expr\Variable(