2018-11-08 12:15:00 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Rector\CodeQuality\Rector\Stmt;
|
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\ArrayDimFetch;
|
2018-11-08 12:51:29 +00:00
|
|
|
use PhpParser\Node\Expr\Cast;
|
2018-11-08 12:15:00 +00:00
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
use PhpParser\Node\Expr\Variable;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use Rector\NodeTypeResolver\Node\Attribute;
|
|
|
|
use Rector\Rector\AbstractRector;
|
|
|
|
use Rector\RectorDefinition\CodeSample;
|
|
|
|
use Rector\RectorDefinition\RectorDefinition;
|
|
|
|
|
|
|
|
final class DeadCodeRemovingRector extends AbstractRector
|
|
|
|
{
|
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
|
|
|
return new RectorDefinition('Removes dead code that is nowhere run', [
|
|
|
|
new CodeSample(
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
$value = 5;
|
|
|
|
$value;
|
|
|
|
CODE_SAMPLE
|
|
|
|
,
|
|
|
|
<<<'CODE_SAMPLE'
|
|
|
|
$value = 5;
|
|
|
|
CODE_SAMPLE
|
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
2018-11-08 12:51:29 +00:00
|
|
|
return [Variable::class, PropertyFetch::class, ArrayDimFetch::class, Cast::class];
|
2018-11-08 12:15:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Variable $node
|
|
|
|
*/
|
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
|
|
|
$parentNode = $node->getAttribute(Attribute::PARENT_NODE);
|
|
|
|
if (! $parentNode instanceof Expression) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->removeNode($node);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|