1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +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 committed by Daniil Gentili
parent a4b246406c
commit 5351a07cea
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 39 additions and 1 deletions

View File

@ -451,6 +451,8 @@ class LoopAnalyzer
$type,
$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
/**
* @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)) {}
}'
],