diff --git a/rules/dead-code/src/Rector/Return_/RemoveDeadConditionAboveReturnRector.php b/rules/dead-code/src/Rector/Return_/RemoveDeadConditionAboveReturnRector.php new file mode 100644 index 000000000000..399750eac431 --- /dev/null +++ b/rules/dead-code/src/Rector/Return_/RemoveDeadConditionAboveReturnRector.php @@ -0,0 +1,100 @@ +getAttribute(AttributeKey::PREVIOUS_NODE); + if (! $previousNode instanceof If_) { + return null; + } + + if ($previousNode->elseifs !== []) { + return null; + } + + if ($previousNode->else instanceof Else_) { + return null; + } + + $countStmt = count($previousNode->stmts); + if ($countStmt === 0) { + $this->removeNode($previousNode); + return $node; + } + + if ($countStmt > 1) { + return null; + } + + $stmt = $previousNode->stmts[0]; + if (! $stmt instanceof Return_) { + return null; + } + + if (! $this->areNodesEqual($stmt, $node)) { + return null; + } + + $this->removeNode($previousNode); + return $node; + } +} diff --git a/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/different_cond.php.inc b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/different_cond.php.inc new file mode 100644 index 000000000000..5005009d7fc4 --- /dev/null +++ b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/different_cond.php.inc @@ -0,0 +1,31 @@ + +----- + diff --git a/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/fixture.php.inc b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/fixture.php.inc new file mode 100644 index 000000000000..fac38c93eb34 --- /dev/null +++ b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/fixture.php.inc @@ -0,0 +1,31 @@ + +----- + diff --git a/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/prev_empty_stmt.php.inc b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/prev_empty_stmt.php.inc new file mode 100644 index 000000000000..acb664563974 --- /dev/null +++ b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/prev_empty_stmt.php.inc @@ -0,0 +1,31 @@ + +----- + \ No newline at end of file diff --git a/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_different_return.php.inc b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_different_return.php.inc new file mode 100644 index 000000000000..bc542104a9b5 --- /dev/null +++ b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_different_return.php.inc @@ -0,0 +1,17 @@ + diff --git a/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_has_else.php.inc b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_has_else.php.inc new file mode 100644 index 000000000000..1cf963e4025d --- /dev/null +++ b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_has_else.php.inc @@ -0,0 +1,19 @@ + diff --git a/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_has_elseif.php.inc b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_has_elseif.php.inc new file mode 100644 index 000000000000..520b2f9d25c7 --- /dev/null +++ b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_has_elseif.php.inc @@ -0,0 +1,19 @@ + diff --git a/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_has_multi_stmt.php.inc b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_has_multi_stmt.php.inc new file mode 100644 index 000000000000..a47cb0bc1a2e --- /dev/null +++ b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_has_multi_stmt.php.inc @@ -0,0 +1,18 @@ + diff --git a/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_not_if.php.inc b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_not_if.php.inc new file mode 100644 index 000000000000..e3b4acf674b5 --- /dev/null +++ b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_not_if.php.inc @@ -0,0 +1,17 @@ + diff --git a/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_not_return.php.inc b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_not_return.php.inc new file mode 100644 index 000000000000..d744794f5153 --- /dev/null +++ b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/Fixture/skip_prev_not_return.php.inc @@ -0,0 +1,17 @@ + diff --git a/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/RemoveDeadConditionAboveReturnRectorTest.php b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/RemoveDeadConditionAboveReturnRectorTest.php new file mode 100644 index 000000000000..5cb0fed13d9c --- /dev/null +++ b/rules/dead-code/tests/Rector/Return_/RemoveDeadConditionAboveReturnRector/RemoveDeadConditionAboveReturnRectorTest.php @@ -0,0 +1,31 @@ +doTestFileInfo($fileInfo); + } + + public function provideData(): Iterator + { + return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); + } + + protected function getRectorClass(): string + { + return RemoveDeadConditionAboveReturnRector::class; + } +}