From 709a5c31da8c59d98f5c0575080a3dfe063f689d Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Mon, 18 Feb 2019 12:53:55 -0500 Subject: [PATCH] Allow bitwise or ops to change type Ref #1340 --- .../Expression/AssignmentAnalyzer.php | 33 ++++++++++++++++--- tests/AssignmentTest.php | 9 +++++ tests/EmptyTest.php | 15 +++++++++ 3 files changed, 52 insertions(+), 5 deletions(-) diff --git a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php index f3b1f1be8..18626968d 100644 --- a/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php +++ b/src/Psalm/Internal/Analyzer/Statements/Expression/AssignmentAnalyzer.php @@ -653,11 +653,11 @@ class AssignmentAnalyzer $var_type = isset($stmt->var->inferredType) ? clone $stmt->var->inferredType : null; $expr_type = isset($stmt->expr->inferredType) ? $stmt->expr->inferredType : null; - if ($stmt instanceof PhpParser\Node\Expr\AssignOp\Plus || - $stmt instanceof PhpParser\Node\Expr\AssignOp\Minus || - $stmt instanceof PhpParser\Node\Expr\AssignOp\Mod || - $stmt instanceof PhpParser\Node\Expr\AssignOp\Mul || - $stmt instanceof PhpParser\Node\Expr\AssignOp\Pow + if ($stmt instanceof PhpParser\Node\Expr\AssignOp\Plus + || $stmt instanceof PhpParser\Node\Expr\AssignOp\Minus + || $stmt instanceof PhpParser\Node\Expr\AssignOp\Mod + || $stmt instanceof PhpParser\Node\Expr\AssignOp\Mul + || $stmt instanceof PhpParser\Node\Expr\AssignOp\Pow ) { BinaryOpAnalyzer::analyzeNonDivArithmenticOp( $statements_analyzer, @@ -698,6 +698,29 @@ class AssignmentAnalyzer $result_type ); + if ($result_type && $array_var_id) { + $context->vars_in_scope[$array_var_id] = $result_type; + $stmt->inferredType = clone $context->vars_in_scope[$array_var_id]; + } + } elseif (isset($stmt->var->inferredType) + && isset($stmt->expr->inferredType) + && ($stmt->var->inferredType->hasInt() || $stmt->expr->inferredType->hasInt()) + && ($stmt instanceof PhpParser\Node\Expr\AssignOp\BitwiseOr + || $stmt instanceof PhpParser\Node\Expr\AssignOp\BitwiseXor + || $stmt instanceof PhpParser\Node\Expr\AssignOp\BitwiseAnd + || $stmt instanceof PhpParser\Node\Expr\AssignOp\ShiftLeft + || $stmt instanceof PhpParser\Node\Expr\AssignOp\ShiftRight + ) + ) { + BinaryOpAnalyzer::analyzeNonDivArithmenticOp( + $statements_analyzer, + $stmt->var, + $stmt->expr, + $stmt, + $result_type, + $context + ); + if ($result_type && $array_var_id) { $context->vars_in_scope[$array_var_id] = $result_type; $stmt->inferredType = clone $context->vars_in_scope[$array_var_id]; diff --git a/tests/AssignmentTest.php b/tests/AssignmentTest.php index e5139683c..cf7177373 100644 --- a/tests/AssignmentTest.php +++ b/tests/AssignmentTest.php @@ -36,6 +36,15 @@ class AssignmentTest extends TestCase echo $w; echo $e;', ], + 'bitwiseAssignment' => [ + ' [ + ' [ + '