1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Correctly interpret continue 2 in nested loops

This commit is contained in:
Matt Brown 2021-05-17 08:26:14 -05:00
parent 4c4d574bed
commit 9635ca620c
2 changed files with 30 additions and 10 deletions

View File

@ -150,28 +150,32 @@ class ScopeAnalyzer
}
if ($stmt instanceof PhpParser\Node\Stmt\Continue_) {
if ($break_types
&& (!$stmt->num || !$stmt->num instanceof PhpParser\Node\Scalar\LNumber || $stmt->num->value < 2)
) {
if (end($break_types) === 'switch') {
$count = !$stmt->num
? 1
: ($stmt->num instanceof PhpParser\Node\Scalar\LNumber ? $stmt->num->value : null);
if ($break_types && $count !== null && count($break_types) >= $count) {
if ($break_types[count($break_types) - $count] === 'switch') {
return array_merge($control_actions, [self::ACTION_LEAVE_SWITCH]);
}
return $control_actions;
return array_values($control_actions);
}
return array_values(array_unique(array_merge($control_actions, [self::ACTION_CONTINUE])));
}
if ($stmt instanceof PhpParser\Node\Stmt\Break_) {
if ($break_types
&& (!$stmt->num || !$stmt->num instanceof PhpParser\Node\Scalar\LNumber || $stmt->num->value < 2)
) {
if (end($break_types) === 'switch') {
$count = !$stmt->num
? 1
: ($stmt->num instanceof PhpParser\Node\Scalar\LNumber ? $stmt->num->value : null);
if ($break_types && $count !== null && count($break_types) >= $count) {
if ($break_types[count($break_types) - $count] === 'switch') {
return array_merge($control_actions, [self::ACTION_LEAVE_SWITCH]);
}
return $control_actions;
return array_values($control_actions);
}
return array_values(array_unique(array_merge($control_actions, [self::ACTION_BREAK])));

View File

@ -644,6 +644,22 @@ class WhileTest extends \Psalm\Tests\TestCase
}
}'
],
'continue2Returns' => [
'<?php
function foo(): array {
while (rand(0, 1)) {
while (rand(0, 1)) {
if (rand(0, 1)) {
continue 2;
}
return [];
}
}
return [];
}'
],
];
}