From a3f3b0e7d27a118aa1adbc65e0cadd343ce0194e Mon Sep 17 00:00:00 2001 From: addshore Date: Tue, 16 Feb 2021 16:03:52 +0000 Subject: [PATCH] [DeadCode] Fix RemoveUnreachableStatementRector issue with global function declarations (#5575) --- .../Rector/Stmt/RemoveUnreachableStatementRector.php | 5 +++++ .../Fixture/keep_function_after_return.php.inc | 11 +++++++++++ 2 files changed, 16 insertions(+) create mode 100644 rules/dead-code/tests/Rector/Stmt/RemoveUnreachableStatementRector/Fixture/keep_function_after_return.php.inc diff --git a/rules/dead-code/src/Rector/Stmt/RemoveUnreachableStatementRector.php b/rules/dead-code/src/Rector/Stmt/RemoveUnreachableStatementRector.php index aed4ef60411..5b9d04ea5c5 100644 --- a/rules/dead-code/src/Rector/Stmt/RemoveUnreachableStatementRector.php +++ b/rules/dead-code/src/Rector/Stmt/RemoveUnreachableStatementRector.php @@ -12,6 +12,7 @@ use PhpParser\Node\Stmt; use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Else_; +use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Namespace_; use PhpParser\Node\Stmt\Nop; @@ -174,6 +175,10 @@ CODE_SAMPLE return true; } + if ($stmt instanceof Function_) { + return true; + } + if ($stmt instanceof Namespace_) { return true; } diff --git a/rules/dead-code/tests/Rector/Stmt/RemoveUnreachableStatementRector/Fixture/keep_function_after_return.php.inc b/rules/dead-code/tests/Rector/Stmt/RemoveUnreachableStatementRector/Fixture/keep_function_after_return.php.inc new file mode 100644 index 00000000000..1b3174c6d84 --- /dev/null +++ b/rules/dead-code/tests/Rector/Stmt/RemoveUnreachableStatementRector/Fixture/keep_function_after_return.php.inc @@ -0,0 +1,11 @@ +