2021-07-15 10:36:02 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\DeadCode\Rector\ClassMethod;
|
2021-07-15 10:36:02 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\Closure;
|
|
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
|
|
use PhpParser\Node\Stmt\Function_;
|
|
|
|
use PhpParser\Node\Stmt\Return_;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\NodeNestingScope\ContextAnalyzer;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2021-07-15 10:36:02 +00:00
|
|
|
/**
|
|
|
|
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveLastReturnRector\RemoveLastReturnRectorTest
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveLastReturnRector extends AbstractRector
|
2021-07-15 10:36:02 +00:00
|
|
|
{
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-07-15 10:36:02 +00:00
|
|
|
* @var \Rector\NodeNestingScope\ContextAnalyzer
|
|
|
|
*/
|
|
|
|
private $contextAnalyzer;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(ContextAnalyzer $contextAnalyzer)
|
2021-07-15 10:36:02 +00:00
|
|
|
{
|
|
|
|
$this->contextAnalyzer = $contextAnalyzer;
|
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2021-07-15 10:36:02 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Remove very last `return` that has no meaning', [new CodeSample(<<<'CODE_SAMPLE'
|
2021-07-15 10:36:02 +00:00
|
|
|
function some_function($value)
|
|
|
|
{
|
|
|
|
if ($value === 1000) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($value) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
, <<<'CODE_SAMPLE'
|
|
|
|
function some_function($value)
|
|
|
|
{
|
|
|
|
if ($value === 1000) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($value) {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
CODE_SAMPLE
|
|
|
|
)]);
|
|
|
|
}
|
|
|
|
/**
|
2021-07-18 02:11:18 +00:00
|
|
|
* @return array<class-string<Node>>
|
2021-07-15 10:36:02 +00:00
|
|
|
*/
|
|
|
|
public function getNodeTypes() : array
|
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [ClassMethod::class, Function_::class, Closure::class];
|
2021-07-15 10:36:02 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param ClassMethod|Function_ $node
|
2021-07-15 10:36:02 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node) : ?Node
|
2021-07-15 10:36:02 +00:00
|
|
|
{
|
|
|
|
// last node and last return
|
2022-06-07 08:22:29 +00:00
|
|
|
$lastNode = $this->betterNodeFinder->findLastInstanceOf((array) $node->stmts, Node::class);
|
|
|
|
$lastReturn = $this->betterNodeFinder->findLastInstanceOf((array) $node->stmts, Return_::class);
|
|
|
|
if (!$lastReturn instanceof Return_) {
|
2021-07-15 10:36:02 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($lastNode !== $lastReturn) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if ($this->contextAnalyzer->isInLoop($lastReturn)) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$this->removeNode($lastReturn);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|