2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\DeadCode\Rector\FunctionLike;
|
2019-03-26 11:17:08 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2023-03-23 23:21:34 +00:00
|
|
|
use PhpParser\Node\Expr;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
2023-06-05 14:01:36 +00:00
|
|
|
use PhpParser\Node\Stmt\Else_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Function_;
|
2023-06-05 14:01:36 +00:00
|
|
|
use PhpParser\Node\Stmt\If_;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\Rector\AbstractRector;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector\RemoveDeadReturnRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveDeadReturnRector extends AbstractRector
|
2019-03-26 11:17:08 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2019-03-26 11:17:08 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove last return in the functions, since does not do anything', [new CodeSample(<<<'CODE_SAMPLE'
|
2019-03-26 11:17:08 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$shallWeDoThis = true;
|
|
|
|
|
|
|
|
if ($shallWeDoThis) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2019-03-26 11:17:08 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
public function run()
|
|
|
|
{
|
|
|
|
$shallWeDoThis = true;
|
|
|
|
|
|
|
|
if ($shallWeDoThis) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
2019-03-26 11:17:08 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2019-03-26 11:17:08 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2019-03-26 11:17:08 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [ClassMethod::class, Function_::class, Closure::class];
|
2019-03-26 11:17:08 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param ClassMethod|Function_|Closure $node
|
2019-03-26 11:17:08 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2019-03-26 11:17:08 +00:00
|
|
|
{
|
2023-06-05 14:01:36 +00:00
|
|
|
if ($node->stmts === [] || $node->stmts === null) {
|
2020-12-24 16:28:56 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-06-05 14:01:36 +00:00
|
|
|
\end($node->stmts);
|
|
|
|
$lastStmtKey = \key($node->stmts);
|
2024-03-08 06:34:02 +00:00
|
|
|
\reset($node->stmts);
|
2023-06-05 14:01:36 +00:00
|
|
|
$lastStmt = $node->stmts[$lastStmtKey];
|
|
|
|
if ($lastStmt instanceof If_) {
|
|
|
|
if (!$this->isBareIfWithOnlyStmtEmptyReturn($lastStmt)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$lastStmt->stmts = [];
|
|
|
|
return $node;
|
2019-03-26 11:17:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
if (!$lastStmt instanceof Return_) {
|
2019-03-26 11:17:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-03-23 23:21:34 +00:00
|
|
|
if ($lastStmt->expr instanceof Expr) {
|
2019-03-26 11:17:08 +00:00
|
|
|
return null;
|
|
|
|
}
|
2023-06-05 14:01:36 +00:00
|
|
|
unset($node->stmts[$lastStmtKey]);
|
2019-03-26 11:17:08 +00:00
|
|
|
return $node;
|
|
|
|
}
|
2023-06-05 14:01:36 +00:00
|
|
|
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;
|
|
|
|
}
|
2019-03-26 11:17:08 +00:00
|
|
|
}
|