Skip to content

Commit

Permalink
Fix vimeo#4327 - make sure loop always returns
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug authored and danog committed Jan 29, 2021
1 parent c15fabb commit 84126fb
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/Psalm/Internal/Analyzer/ScopeAnalyzer.php
Expand Up @@ -286,8 +286,11 @@ function ($action) {

$control_actions = array_filter(
array_merge($control_actions, $do_actions),
function ($action) {
return $action !== self::ACTION_NONE;
function ($action) use ($break_types) {
return $action !== self::ACTION_NONE
&& ($break_types
|| ($action !== self::ACTION_CONTINUE
&& $action !== self::ACTION_BREAK));
}
);
}
Expand Down
31 changes: 31 additions & 0 deletions tests/SwitchTypeTest.php
Expand Up @@ -999,6 +999,21 @@ function getRows(string $s) : int {
}
}'
],
'loopWithSwitchAlwaysReturns' => [
'<?php
function b(): int {
foreach([1,2] as $i) {
continue;
}
switch (random_int(1, 10)) {
case 1:
return 1;
default:
return 2;
}
}',
],
];
}

Expand Down Expand Up @@ -1270,6 +1285,22 @@ function foo() {
}',
'error_message' => 'ParadoxicalCondition'
],
'loopWithSwitchDoesntReturnFirstCase' => [
'<?php
function b(): int {
switch (random_int(1, 10)) {
case 1:
foreach([1,2] as $i) {
continue;
}
break;
default:
return 2;
}
}',
'error_message' => 'InvalidReturnType'
],
];
}
}

0 comments on commit 84126fb

Please sign in to comment.