1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Update context types after switch statement without default case

This commit is contained in:
Matthew Brown 2017-03-13 21:15:46 -04:00
parent b1eeb11dca
commit 50b629e88e
3 changed files with 18 additions and 20 deletions

View File

@ -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);

View File

@ -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]
);
}
}
}

View File

@ -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<int, int|string>', (string) $context->vars_in_scope['$out']);
$this->assertEquals('array<int, string|int>', (string) $context->vars_in_scope['$out']);
}
/**