mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-27 13:13:31 +00:00
aa2cb1b997
0eba231b07
[PHP 8.1] Move ConstantListClassToEnumRector to PHP 8.1 (#2444)
81 lines
2.1 KiB
PHP
81 lines
2.1 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\DeadCode\Rector\FunctionLike;
|
|
|
|
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 Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @see \Rector\Tests\DeadCode\Rector\FunctionLike\RemoveDeadReturnRector\RemoveDeadReturnRectorTest
|
|
*/
|
|
final class RemoveDeadReturnRector extends \Rector\Core\Rector\AbstractRector
|
|
{
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Remove last return in the functions, since does not do anything', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
public function run()
|
|
{
|
|
$shallWeDoThis = true;
|
|
|
|
if ($shallWeDoThis) {
|
|
return;
|
|
}
|
|
|
|
return;
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
public function run()
|
|
{
|
|
$shallWeDoThis = true;
|
|
|
|
if ($shallWeDoThis) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Stmt\ClassMethod::class, \PhpParser\Node\Stmt\Function_::class, \PhpParser\Node\Expr\Closure::class];
|
|
}
|
|
/**
|
|
* @param ClassMethod|Function_|Closure $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
if ($node->stmts === []) {
|
|
return null;
|
|
}
|
|
if ($node->stmts === null) {
|
|
return null;
|
|
}
|
|
$stmtValues = \array_values($node->stmts);
|
|
$lastStmt = \end($stmtValues);
|
|
if (!$lastStmt instanceof \PhpParser\Node\Stmt\Return_) {
|
|
return null;
|
|
}
|
|
if ($lastStmt->expr !== null) {
|
|
return null;
|
|
}
|
|
$this->removeNode($lastStmt);
|
|
return $node;
|
|
}
|
|
}
|