Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

63 changes: 8 additions & 55 deletions rules/DeadCode/Rector/FunctionLike/RemoveDeadReturnRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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
));
}
}
2 changes: 0 additions & 2 deletions src/Config/Level/DeadCodeLevel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down
Loading