From fd7afc9c400b82657464db12f2b9f76d81c7fd3e Mon Sep 17 00:00:00 2001 From: Matt Brown Date: Wed, 2 Dec 2020 02:15:17 -0500 Subject: [PATCH] Memoize coalesce expression in more scenarios --- .../Expression/BinaryOp/CoalesceAnalyzer.php | 20 +++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/CoalesceAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/CoalesceAnalyzer.php index 4813a9d64..e9eb65945 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/CoalesceAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/BinaryOp/CoalesceAnalyzer.php @@ -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 = '$' . (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(