From 50b629e88e9d424a0d6cb10cb68ac9f55eaae1cd Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Mon, 13 Mar 2017 21:15:46 -0400 Subject: [PATCH] Update context types after switch statement without default case --- .../Statements/Block/SwitchChecker.php | 4 +++ src/Psalm/Type.php | 32 ++++++++----------- tests/ArrayAssignmentTest.php | 2 +- 3 files changed, 18 insertions(+), 20 deletions(-) diff --git a/src/Psalm/Checker/Statements/Block/SwitchChecker.php b/src/Psalm/Checker/Statements/Block/SwitchChecker.php index 3e712fe08..d1ed07ca3 100644 --- a/src/Psalm/Checker/Statements/Block/SwitchChecker.php +++ b/src/Psalm/Checker/Statements/Block/SwitchChecker.php @@ -206,6 +206,10 @@ class SwitchChecker if ($redefined_vars) { $context->vars_in_scope = array_merge($context->vars_in_scope, $redefined_vars); } + } elseif ($redefined_vars) { + foreach ($redefined_vars as $var_id => $type) { + $context->vars_in_scope[$var_id] = Type::combineUnionTypes($type, $context->vars_in_scope[$var_id]); + } } $context->vars_possibly_in_scope = array_merge($context->vars_possibly_in_scope, $new_vars_possibly_in_scope); diff --git a/src/Psalm/Type.php b/src/Psalm/Type.php index 15281adc7..c0dd8546b 100644 --- a/src/Psalm/Type.php +++ b/src/Psalm/Type.php @@ -409,30 +409,24 @@ abstract class Type foreach ($redefined_union_type->types as $redefined_atomic_type) { foreach ($context->vars_in_scope[$var_name]->types as $context_type) { if ($context_type instanceof Type\Atomic\TArray && - $redefined_atomic_type instanceof Type\Atomic\TArray && - $context_type->value === $redefined_atomic_type->value + $redefined_atomic_type instanceof Type\Atomic\TArray ) { - // index of last param - $i = count($context_type->type_params) - 1; - - if ($context_type->type_params[$i]->isEmpty()) { - $context_type->type_params[$i] = $redefined_atomic_type->type_params[$i]; + if ($context_type->type_params[1]->isEmpty()) { + $context_type->type_params[1] = $redefined_atomic_type->type_params[1]; } else { - $context_type->type_params[$i] = Type::combineUnionTypes( - $redefined_atomic_type->type_params[$i], - $context_type->type_params[$i] + $context_type->type_params[1] = Type::combineUnionTypes( + $redefined_atomic_type->type_params[1], + $context_type->type_params[1] ); } - if ($i) { - if ($context_type->type_params[0]->isEmpty()) { - $context_type->type_params[0] = $redefined_atomic_type->type_params[0]; - } else { - $context_type->type_params[0] = Type::combineUnionTypes( - $redefined_atomic_type->type_params[0], - $context_type->type_params[0] - ); - } + if ($context_type->type_params[0]->isEmpty()) { + $context_type->type_params[0] = $redefined_atomic_type->type_params[0]; + } else { + $context_type->type_params[0] = Type::combineUnionTypes( + $redefined_atomic_type->type_params[0], + $context_type->type_params[0] + ); } } } diff --git a/tests/ArrayAssignmentTest.php b/tests/ArrayAssignmentTest.php index 150eb07f1..0a83a6b4d 100644 --- a/tests/ArrayAssignmentTest.php +++ b/tests/ArrayAssignmentTest.php @@ -169,7 +169,7 @@ class ArrayAssignmentTest extends PHPUnit_Framework_TestCase $file_checker = new FileChecker('somefile.php', $this->project_checker, $stmts); $context = new Context(); $file_checker->visitAndAnalyzeMethods($context); - $this->assertEquals('array', (string) $context->vars_in_scope['$out']); + $this->assertEquals('array', (string) $context->vars_in_scope['$out']); } /**