diff --git a/rules-tests/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector/Fixture/fixture.php.inc b/rules-tests/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector/Fixture/fixture.php.inc deleted file mode 100644 index 3cd0ea7dd31..00000000000 --- a/rules-tests/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector/Fixture/fixture.php.inc +++ /dev/null @@ -1,57 +0,0 @@ - ------ - diff --git a/rules-tests/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector/Fixture/if_in_class_method.php.inc b/rules-tests/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector/Fixture/if_in_class_method.php.inc deleted file mode 100644 index 504996207b3..00000000000 --- a/rules-tests/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector/Fixture/if_in_class_method.php.inc +++ /dev/null @@ -1,36 +0,0 @@ - ------ - diff --git a/rules-tests/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector/Fixture/keep.php.inc b/rules-tests/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector/Fixture/keep.php.inc deleted file mode 100644 index af30f359645..00000000000 --- a/rules-tests/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector/Fixture/keep.php.inc +++ /dev/null @@ -1,31 +0,0 @@ -doTestFile($filePath); - } - - public static function provideData(): Iterator - { - return self::yieldFilesFromDirectory(__DIR__ . '/Fixture'); - } - - public function provideConfigFilePath(): string - { - return __DIR__ . '/config/configured_rule.php'; - } -} diff --git a/rules-tests/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector/config/configured_rule.php b/rules-tests/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector/config/configured_rule.php deleted file mode 100644 index 74f1e40ed59..00000000000 --- a/rules-tests/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector/config/configured_rule.php +++ /dev/null @@ -1,9 +0,0 @@ -withRules([RemoveDeadReturnRector::class]); diff --git a/rules/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector.php b/rules/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector.php index 4fc26e9e1f3..f11d38fe30e 100644 --- a/rules/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector.php +++ b/rules/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector.php @@ -5,21 +5,19 @@ namespace Rector\DeadCode\Rector\FunctionLike; use PhpParser\Node; -use PhpParser\Node\Expr; use PhpParser\Node\Expr\Closure; use PhpParser\Node\Stmt\ClassMethod; -use PhpParser\Node\Stmt\Else_; use PhpParser\Node\Stmt\Function_; -use PhpParser\Node\Stmt\If_; -use PhpParser\Node\Stmt\Return_; +use Rector\Configuration\Deprecation\Contract\DeprecatedInterface; +use Rector\Exception\ShouldNotHappenException; use Rector\Rector\AbstractRector; use Rector\RuleDoc\CodeSample\CodeSample; use Rector\RuleDoc\RuleDefinition; /** - * @see \Rector\Tests\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector\RemoveDeadReturnRectorTest + * @deprecated This rule is deprecated in favor of RemoveAlwaysTrueIfConditionRector. */ -final class RemoveDeadReturnRector extends AbstractRector +final class RemoveDeadReturnRector extends AbstractRector implements DeprecatedInterface { public function getRuleDefinition(): RuleDefinition { @@ -74,54 +72,9 @@ public function getNodeTypes(): array */ public function refactor(Node $node): ?Node { - if ($node->stmts === [] || $node->stmts === null) { - return null; - } - - $lastStmtKey = array_key_last($node->stmts); - - $lastStmt = $node->stmts[$lastStmtKey]; - - if ($lastStmt instanceof If_) { - if (! $this->isBareIfWithOnlyStmtEmptyReturn($lastStmt)) { - return null; - } - - $lastStmt->stmts = []; - return $node; - } - - if (! $lastStmt instanceof Return_) { - return null; - } - - if ($lastStmt->expr instanceof Expr) { - return null; - } - - unset($node->stmts[$lastStmtKey]); - return $node; - } - - private function isBareIfWithOnlyStmtEmptyReturn(If_ $if): bool - { - if ($if->else instanceof Else_) { - return false; - } - - if ($if->elseifs !== []) { - return false; - } - - if (count($if->stmts) !== 1) { - return false; - } - - $onlyStmt = $if->stmts[0]; - if (! $onlyStmt instanceof Return_) { - return false; - } - - return ! $onlyStmt->expr instanceof Expr; + throw new ShouldNotHappenException(sprintf( + '"%s" rule is deprecated in favor of RemoveAlwaysTrueIfConditionRector', + self::class + )); } } diff --git a/src/Config/Level/DeadCodeLevel.php b/src/Config/Level/DeadCodeLevel.php index 7c1350fd99a..5f8db19ffd3 100644 --- a/src/Config/Level/DeadCodeLevel.php +++ b/src/Config/Level/DeadCodeLevel.php @@ -49,7 +49,6 @@ use Rector\DeadCode\Rector\Foreach_\RemoveUnusedForeachKeyRector; use Rector\DeadCode\Rector\FuncCall\RemoveFilterVarOnExactTypeRector; use Rector\DeadCode\Rector\FunctionLike\NarrowWideUnionReturnTypeRector; -use Rector\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector; use Rector\DeadCode\Rector\If_\ReduceAlwaysFalseIfOrRector; use Rector\DeadCode\Rector\If_\RemoveAlwaysTrueIfConditionRector; use Rector\DeadCode\Rector\If_\RemoveDeadIfBlockRector; @@ -184,7 +183,6 @@ final class DeadCodeLevel // this could break framework magic autowiring in some cases RemoveUnusedConstructorParamRector::class, RemoveEmptyClassMethodRector::class, - RemoveDeadReturnRector::class, RemoveArgumentFromDefaultParentCallRector::class, RemoveNullArgOnNullDefaultParamRector::class,