Skip to content

Commit

Permalink
Fix unreachable statement after switch with conditional break
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Nov 11, 2020
1 parent 5ad4686 commit 46c39e8
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Analyser/NodeScopeResolver.php
Expand Up @@ -1011,6 +1011,7 @@ private function processStmtNode(
$branchFinalScopeResult = $branchScopeResult->filterOutLoopExitPoints();
$hasYield = $hasYield || $branchFinalScopeResult->hasYield();
foreach ($branchScopeResult->getExitPointsByType(Break_::class) as $breakExitPoint) {
$alwaysTerminating = false;
$finalScope = $breakExitPoint->getScope()->mergeWith($finalScope);
}
foreach ($branchScopeResult->getExitPointsByType(Continue_::class) as $continueExitPoint) {
Expand Down
6 changes: 6 additions & 0 deletions tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php
Expand Up @@ -92,4 +92,10 @@ public function testBug4070Two(): void
$this->analyse([__DIR__ . '/data/bug-4070_2.php'], []);
}

public function testBug4076(): void
{
$this->treatPhpDocTypesAsCertain = true;
$this->analyse([__DIR__ . '/data/bug-4076.php'], []);
}

}
25 changes: 25 additions & 0 deletions tests/PHPStan/Rules/DeadCode/data/bug-4076.php
@@ -0,0 +1,25 @@
<?php

namespace Bug4076;

class Foo
{

function test(int $x, int $y): int
{
switch($x) {
case 0:
return 0;
case 1:
if ($y == 2) {
// continue after the switch
break;
}
default:
return 99;
}

return -1;
}

}

0 comments on commit 46c39e8

Please sign in to comment.