diff --git a/rules-tests/DeadCode/Rector/Stmt/RemoveUnreachableStatementRector/Fixture/skip_do_while.php.inc b/rules-tests/DeadCode/Rector/Stmt/RemoveUnreachableStatementRector/Fixture/skip_do_while.php.inc new file mode 100644 index 00000000000..31bd5e2cf79 --- /dev/null +++ b/rules-tests/DeadCode/Rector/Stmt/RemoveUnreachableStatementRector/Fixture/skip_do_while.php.inc @@ -0,0 +1,21 @@ + 0); + + return $new; +} + +?> diff --git a/rules/DeadCode/Rector/Stmt/RemoveUnreachableStatementRector.php b/rules/DeadCode/Rector/Stmt/RemoveUnreachableStatementRector.php index 6ac9c7ec63c..12a64811443 100644 --- a/rules/DeadCode/Rector/Stmt/RemoveUnreachableStatementRector.php +++ b/rules/DeadCode/Rector/Stmt/RemoveUnreachableStatementRector.php @@ -11,6 +11,7 @@ use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\ClassMethod; +use PhpParser\Node\Stmt\Do_; use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\If_; @@ -29,6 +30,11 @@ */ final class RemoveUnreachableStatementRector extends AbstractRector { + /** + * @var array> + */ + private const STMTS_WITH_IS_UNREACHABLE = [If_::class, While_::class, Do_::class]; + public function getRuleDefinition(): RuleDefinition { return new RuleDefinition('Remove unreachable statements', [ @@ -77,14 +83,11 @@ public function refactor(Node $node): ?Node // might be PHPStan false positive, better skip $previousStatement = $node->getAttribute(AttributeKey::PREVIOUS_STATEMENT); - if ($previousStatement instanceof If_) { - $node->setAttribute( - AttributeKey::IS_UNREACHABLE, - $previousStatement->getAttribute(AttributeKey::IS_UNREACHABLE) - ); - } - - if ($previousStatement instanceof While_) { + if ($previousStatement instanceof Stmt && in_array( + $previousStatement::class, + self::STMTS_WITH_IS_UNREACHABLE, + true + )) { $node->setAttribute( AttributeKey::IS_UNREACHABLE, $previousStatement->getAttribute(AttributeKey::IS_UNREACHABLE)