rector/packages/DeadCode/src/Rector/Stmt/RemoveDeadStmtRector.php

64 lines
1.4 KiB
PHP
Raw Normal View History

<?php declare(strict_types=1);
2018-12-25 11:38:07 +00:00
namespace Rector\DeadCode\Rector\Stmt;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
2018-11-08 12:51:29 +00:00
use PhpParser\Node\Expr\Cast;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt\Expression;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\RectorDefinition\CodeSample;
use Rector\RectorDefinition\RectorDefinition;
2019-09-03 09:11:45 +00:00
/**
* @see \Rector\DeadCode\Tests\Rector\Stmt\RemoveDeadStmtRector\RemoveDeadStmtRectorTest
*/
2018-12-25 11:38:07 +00:00
final class RemoveDeadStmtRector extends AbstractRector
{
public function getDefinition(): RectorDefinition
{
2018-12-25 11:38:07 +00:00
return new RectorDefinition('Removes dead code statements', [
new CodeSample(
2019-09-18 06:14:35 +00:00
<<<'PHP'
$value = 5;
$value;
2019-09-18 06:14:35 +00:00
PHP
,
2019-09-18 06:14:35 +00:00
<<<'PHP'
$value = 5;
2019-09-18 06:14:35 +00:00
PHP
),
]);
}
/**
* @return string[]
*/
public function getNodeTypes(): array
{
2018-11-08 12:51:29 +00:00
return [Variable::class, PropertyFetch::class, ArrayDimFetch::class, Cast::class];
}
/**
* @param Variable $node
*/
public function refactor(Node $node): ?Node
{
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
if (! $parentNode instanceof Expression) {
return null;
}
2019-02-17 14:12:47 +00:00
if ($node->getComments() !== []) {
return null;
}
$this->removeNode($node);
return null;
}
}