From 3e7e1b302065d05d064e0719e8e29d09a2a98fe9 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Tue, 24 Apr 2018 21:02:07 -0400 Subject: [PATCH] Fix #694 - remove descendent vars when possibly reassigning --- .../Checker/Statements/Block/IfChecker.php | 21 ++++++++++++++----- tests/LoopScopeTest.php | 9 ++++++++ tests/TestCase.php | 10 +++++++-- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/src/Psalm/Checker/Statements/Block/IfChecker.php b/src/Psalm/Checker/Statements/Block/IfChecker.php index b6d8cdb55..3260c6355 100644 --- a/src/Psalm/Checker/Statements/Block/IfChecker.php +++ b/src/Psalm/Checker/Statements/Block/IfChecker.php @@ -460,12 +460,23 @@ class IfChecker } if ($if_scope->possibly_redefined_vars) { - foreach ($if_scope->possibly_redefined_vars as $var => $type) { - if ($context->hasVariable($var) && - !$type->failed_reconciliation && - !isset($if_scope->updated_vars[$var]) + foreach ($if_scope->possibly_redefined_vars as $var_id => $type) { + if ($context->hasVariable($var_id)) { + + } + } + + foreach ($if_scope->possibly_redefined_vars as $var_id => $type) { + if ($context->hasVariable($var_id) + && !$type->failed_reconciliation + && !isset($if_scope->updated_vars[$var_id]) ) { - $context->vars_in_scope[$var] = Type::combineUnionTypes($context->vars_in_scope[$var], $type); + $combined_type = Type::combineUnionTypes( + $context->vars_in_scope[$var_id], + $type + ); + $context->removeDescendents($var_id, $combined_type); + $context->vars_in_scope[$var_id] = $combined_type; } } } diff --git a/tests/LoopScopeTest.php b/tests/LoopScopeTest.php index 29b556d4c..80fd12b03 100644 --- a/tests/LoopScopeTest.php +++ b/tests/LoopScopeTest.php @@ -998,6 +998,15 @@ class LoopScopeTest extends TestCase } }', ], + 'noRedundantConditionAfterArrayAssignment' => [ + ' false]; + while (!$data["a"]) { + if (rand() % 2 > 0) { + $data = ["a" => true]; + } + }', + ], ]; } diff --git a/tests/TestCase.php b/tests/TestCase.php index 952809ee6..27cbfaab1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -22,8 +22,14 @@ class TestCase extends BaseTestCase public static function setUpBeforeClass() { ini_set('memory_limit', '-1'); - define('PSALM_VERSION', '2.0.0'); - define('PHP_PARSER_VERSION', '4.0.0'); + + if (!defined('PSALM_VERSION')) { + define('PSALM_VERSION', '2.0.0'); + } + + if (!defined('PHP_PARSER_VERSION')) { + define('PHP_PARSER_VERSION', '4.0.0'); + } parent::setUpBeforeClass(); self::$src_dir_path = getcwd() . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR;