1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Fix analysis when there’s a break in a loop after a reassignment

This commit is contained in:
Matt Brown 2020-09-30 00:04:07 -04:00
parent 046725bd21
commit 169b2b7023
2 changed files with 39 additions and 1 deletions

View File

@ -451,6 +451,8 @@ class LoopAnalyzer
$type, $type,
$loop_scope->loop_parent_context->vars_in_scope[$var] $loop_scope->loop_parent_context->vars_in_scope[$var]
); );
$loop_scope->loop_parent_context->possibly_assigned_var_ids[$var] = true;
} }
} }
} }

View File

@ -1057,7 +1057,7 @@ class ForeachTest extends \Psalm\Tests\TestCase
} }
}' }'
], ],
'loopCanUpdateOuter' => [ 'loopCanUpdateOuterWithoutBreak' => [
'<?php '<?php
/** /**
* @param array<int> $mappings * @param array<int> $mappings
@ -1069,6 +1069,42 @@ class ForeachTest extends \Psalm\Tests\TestCase
} }
} }
if (is_int($id)) {}
}'
],
'loopCanUpdateOuterWithBreak' => [
'<?php
/**
* @param array<int> $mappings
*/
function foo(string $id, array $mappings) : void {
if ($id === "a") {
foreach ($mappings as $value) {
if (rand(0, 1)) {
$id = $value;
break;
}
}
}
if (is_int($id)) {}
}'
],
'loopCanUpdateOuterWithContinue' => [
'<?php
/**
* @param array<int> $mappings
*/
function foo(string $id, array $mappings) : void {
if ($id === "a") {
foreach ($mappings as $value) {
if (rand(0, 1)) {
$id = $value;
continue;
}
}
}
if (is_int($id)) {} if (is_int($id)) {}
}' }'
], ],