From 1f0aca00b3194b6643c92d89dcb7659f670db796 Mon Sep 17 00:00:00 2001 From: lhchavez Date: Tue, 27 Aug 2019 20:19:10 -0700 Subject: [PATCH] Use the correct types for AssignmentRef (#2078) This change assigns the type of the rhs expression to the variable that will become a reference, as well as preventing clobbering of the rhs if it is an already-typed variable. Fixes: #2077 --- .../Expression/AssignmentAnalyzer.php | 9 ++++---- tests/AssignmentTest.php | 21 +++++++++++++++++++ tests/Php70Test.php | 2 +- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php index c8fd1d2c2..e47c151b7 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php @@ -855,14 +855,15 @@ class AssignmentAnalyzer PhpParser\Node\Expr\AssignRef $stmt, Context $context ) { - if (self::analyze( + $assignment_type = self::analyze( $statements_analyzer, $stmt->var, $stmt->expr, null, $context, $stmt->getDocComment() - ) === false) { + ); + if ($assignment_type === false) { return false; } @@ -879,11 +880,11 @@ class AssignmentAnalyzer ); if ($lhs_var_id) { - $context->vars_in_scope[$lhs_var_id] = Type::getMixed(); + $context->vars_in_scope[$lhs_var_id] = $assignment_type; $context->hasVariable($lhs_var_id, $statements_analyzer); } - if ($rhs_var_id) { + if ($rhs_var_id && !isset($context->vars_in_scope[$rhs_var_id])) { $context->vars_in_scope[$rhs_var_id] = Type::getMixed(); } } diff --git a/tests/AssignmentTest.php b/tests/AssignmentTest.php index 2ccb08f0e..a38efff7f 100644 --- a/tests/AssignmentTest.php +++ b/tests/AssignmentTest.php @@ -58,6 +58,27 @@ class AssignmentTest extends TestCase /** @var mixed */ $b = $a;', ], + 'referenceAssignmentArray' => [ + ' [ + ' [ + ' [ - '$a' => 'mixed', + '$a' => 'int', ], ], 'spaceship' => [