2018-11-08 12:15:00 +00:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
|
2018-12-25 11:38:07 +00:00
|
|
|
namespace Rector\DeadCode\Rector\Stmt;
|
2018-11-08 12:15:00 +00:00
|
|
|
|
|
|
|
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;
|
2019-04-13 09:20:27 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2018-11-08 12:15:00 +00:00
|
|
|
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
|
2018-11-08 12:15:00 +00:00
|
|
|
{
|
|
|
|
public function getDefinition(): RectorDefinition
|
|
|
|
{
|
2018-12-25 11:38:07 +00:00
|
|
|
return new RectorDefinition('Removes dead code statements', [
|
2018-11-08 12:15:00 +00:00
|
|
|
new CodeSample(
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2018-11-08 12:15:00 +00:00
|
|
|
$value = 5;
|
|
|
|
$value;
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2018-11-08 12:15:00 +00:00
|
|
|
,
|
2019-09-18 06:14:35 +00:00
|
|
|
<<<'PHP'
|
2018-11-08 12:15:00 +00:00
|
|
|
$value = 5;
|
2019-09-18 06:14:35 +00:00
|
|
|
PHP
|
2018-11-08 12:15:00 +00:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
{
|
2019-04-13 09:20:27 +00:00
|
|
|
$parentNode = $node->getAttribute(AttributeKey::PARENT_NODE);
|
2018-11-08 12:15:00 +00:00
|
|
|
if (! $parentNode instanceof Expression) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-02-17 14:12:47 +00:00
|
|
|
if ($node->getComments() !== []) {
|
2018-11-09 14:02:00 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-11-08 12:15:00 +00:00
|
|
|
$this->removeNode($node);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|