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

Fix issue when two continue/break statements write same variable

This commit is contained in:
Matthew Brown 2021-05-13 09:39:07 -04:00
parent f6e4fb1263
commit 4873f53f69
3 changed files with 23 additions and 15 deletions

View File

@ -39,14 +39,7 @@ class BreakAnalyzer
$loop_scope->possibly_redefined_loop_parent_vars = $redefined_vars;
} else {
foreach ($redefined_vars as $var => $type) {
if ($type->hasMixed()) {
if (isset($loop_scope->possibly_redefined_loop_parent_vars[$var])) {
$type->parent_nodes
+= $loop_scope->possibly_redefined_loop_parent_vars[$var]->parent_nodes;
}
$loop_scope->possibly_redefined_loop_parent_vars[$var] = $type;
} elseif (isset($loop_scope->possibly_redefined_loop_parent_vars[$var])) {
if (isset($loop_scope->possibly_redefined_loop_parent_vars[$var])) {
$loop_scope->possibly_redefined_loop_parent_vars[$var] = Type::combineUnionTypes(
$type,
$loop_scope->possibly_redefined_loop_parent_vars[$var]

View File

@ -75,13 +75,7 @@ class ContinueAnalyzer
}
foreach ($redefined_vars as $var => $type) {
if ($type->hasMixed()) {
if (isset($loop_scope->possibly_redefined_loop_vars[$var])) {
$type->parent_nodes += $loop_scope->possibly_redefined_loop_vars[$var]->parent_nodes;
}
$loop_scope->possibly_redefined_loop_vars[$var] = $type;
} elseif (isset($loop_scope->possibly_redefined_loop_vars[$var])) {
if (isset($loop_scope->possibly_redefined_loop_vars[$var])) {
$loop_scope->possibly_redefined_loop_vars[$var] = Type::combineUnionTypes(
$type,
$loop_scope->possibly_redefined_loop_vars[$var]

View File

@ -623,6 +623,27 @@ class WhileTest extends \Psalm\Tests\TestCase
return $state;
}'
],
'continueShouldAddToContext' => [
'<?php
function foo() : void {
$link = null;
while (rand(0, 1)) {
if (rand(0, 1)) {
$link = "a";
continue;
}
if (rand(0, 1)) {
if ($link === null) {
return;
}
continue;
}
}
}'
],
];
}