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;