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

Update loop with union of existing types

This commit is contained in:
Matthew Brown 2017-11-21 22:13:46 -05:00
parent df06c64b3e
commit fd3de443b2
2 changed files with 27 additions and 1 deletions

View File

@ -240,7 +240,15 @@ class IfChecker
if ($if_scope->redefined_loop_vars && $loop_context) {
foreach ($if_scope->redefined_loop_vars as $var => $type) {
$loop_context->vars_in_scope[$var] = $type;
if (!isset($loop_context->vars_in_scope[$var])) {
$loop_context->vars_in_scope[$var] = $type;
} else {
$loop_context->vars_in_scope[$var] = Type::combineUnionTypes(
$loop_context->vars_in_scope[$var],
$type
);
}
$updated_loop_vars[$var] = true;
}
}

View File

@ -365,6 +365,24 @@ class LoopScopeTest extends TestCase
if (!$a) {}
}',
],
'loopWithIfElseNoParadox' => [
'<?php
$a = [];
$b = rand(0, 10) > 5;
foreach ([1, 2, 3] as $i) {
if (rand(0, 5)) {
$a[] = 5;
continue;
}
if ($b) {
continue; // if this is removed, no failure
} else {} // if else is removed, no failure
}
if ($a) {}',
],
];
}