rector/rules/DeadCode/Rector/FunctionLike/RemoveCodeAfterReturnRector.php
Tomas Votruba 96112cb1f0 Updated Rector to commit 2da49992cc
2da49992cc [Downgrade] [PHP 7.2] Make DowngradeParameterTypeWideningRector always downgrade to phpdoc type (#390)
2021-07-05 22:50:18 +00:00

79 lines
2.3 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\Nop;
use PhpParser\Node\Stmt\Return_;
use Rector\Core\Exception\ShouldNotHappenException;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\FunctionLike\RemoveCodeAfterReturnRector\RemoveCodeAfterReturnRectorTest
*/
final class RemoveCodeAfterReturnRector extends \Rector\Core\Rector\AbstractRector
{
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
{
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Remove dead code after return statement', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function run(int $a)
{
return $a;
$a++;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function run(int $a)
{
return $a;
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [\PhpParser\Node\Expr\Closure::class, \PhpParser\Node\Stmt\ClassMethod::class, \PhpParser\Node\Stmt\Function_::class];
}
/**
* @param Closure|ClassMethod|Function_ $node
*/
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
{
if ($node->stmts === null) {
return null;
}
$isDeadAfterReturn = \false;
foreach ($node->stmts as $key => $stmt) {
if ($isDeadAfterReturn) {
if (!isset($node->stmts[$key])) {
throw new \Rector\Core\Exception\ShouldNotHappenException();
}
// keep comment
/** @var int $key */
if ($node->stmts[$key] instanceof \PhpParser\Node\Stmt\Nop) {
continue;
}
$this->nodeRemover->removeStmt($node, $key);
}
if ($stmt instanceof \PhpParser\Node\Stmt\Return_) {
$isDeadAfterReturn = \true;
}
}
return null;
}
}