From 46c39e8d456a44eb3bdcea1a65d5c05bef1ae3e0 Mon Sep 17 00:00:00 2001 From: Ondrej Mirtes Date: Wed, 11 Nov 2020 18:42:08 +0100 Subject: [PATCH] Fix unreachable statement after switch with conditional break --- src/Analyser/NodeScopeResolver.php | 1 + .../DeadCode/UnreachableStatementRuleTest.php | 6 +++++ .../PHPStan/Rules/DeadCode/data/bug-4076.php | 25 +++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 tests/PHPStan/Rules/DeadCode/data/bug-4076.php diff --git a/src/Analyser/NodeScopeResolver.php b/src/Analyser/NodeScopeResolver.php index 663e06460b..9f2e9fe0d9 100644 --- a/src/Analyser/NodeScopeResolver.php +++ b/src/Analyser/NodeScopeResolver.php @@ -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) { diff --git a/tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php b/tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php index 3c85891d12..43270e54a4 100644 --- a/tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php +++ b/tests/PHPStan/Rules/DeadCode/UnreachableStatementRuleTest.php @@ -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'], []); + } + } diff --git a/tests/PHPStan/Rules/DeadCode/data/bug-4076.php b/tests/PHPStan/Rules/DeadCode/data/bug-4076.php new file mode 100644 index 0000000000..be5b929832 --- /dev/null +++ b/tests/PHPStan/Rules/DeadCode/data/bug-4076.php @@ -0,0 +1,25 @@ +