2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2018-11-08 12:15:00 +00:00
|
|
|
|
2020-08-18 15:57:30 +00:00
|
|
|
namespace Rector\DeadCode\Rector\Expression;
|
2018-11-08 12:15:00 +00:00
|
|
|
|
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
2019-11-04 20:26:08 +00:00
|
|
|
use PhpParser\Node\Stmt\Nop;
|
2020-02-06 21:48:18 +00:00
|
|
|
use Rector\Core\Rector\AbstractRector;
|
2021-02-08 21:00:45 +00:00
|
|
|
use Rector\DeadCode\NodeManipulator\LivingCodeManipulator;
|
2020-02-02 18:15:36 +00:00
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2020-11-16 17:50:38 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2018-11-08 12:15:00 +00:00
|
|
|
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2020-08-18 15:57:30 +00:00
|
|
|
* @see \Rector\DeadCode\Tests\Rector\Expression\RemoveDeadStmtRector\RemoveDeadStmtRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2018-12-25 11:38:07 +00:00
|
|
|
final class RemoveDeadStmtRector extends AbstractRector
|
2018-11-08 12:15:00 +00:00
|
|
|
{
|
2021-02-08 21:00:45 +00:00
|
|
|
/**
|
|
|
|
* @var LivingCodeManipulator
|
|
|
|
*/
|
|
|
|
private $livingCodeManipulator;
|
|
|
|
|
|
|
|
public function __construct(LivingCodeManipulator $livingCodeManipulator)
|
|
|
|
{
|
|
|
|
$this->livingCodeManipulator = $livingCodeManipulator;
|
|
|
|
}
|
|
|
|
|
2020-11-16 17:50:38 +00:00
|
|
|
public function getRuleDefinition(): RuleDefinition
|
2018-11-08 12:15:00 +00:00
|
|
|
{
|
2020-11-16 17:50:38 +00:00
|
|
|
return new RuleDefinition('Removes dead code statements', [
|
2018-11-08 12:15:00 +00:00
|
|
|
new CodeSample(
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2018-11-08 12:15:00 +00:00
|
|
|
$value = 5;
|
|
|
|
$value;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2018-11-08 12:15:00 +00:00
|
|
|
,
|
2020-09-15 08:23:13 +00:00
|
|
|
<<<'CODE_SAMPLE'
|
2018-11-08 12:15:00 +00:00
|
|
|
$value = 5;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2018-11-08 12:15:00 +00:00
|
|
|
),
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-11-08 12:15:00 +00:00
|
|
|
*/
|
|
|
|
public function getNodeTypes(): array
|
|
|
|
{
|
2019-11-04 20:26:08 +00:00
|
|
|
return [Expression::class];
|
2018-11-08 12:15:00 +00:00
|
|
|
}
|
|
|
|
|
2020-02-02 18:15:36 +00:00
|
|
|
/**
|
|
|
|
* @param Expression $node
|
|
|
|
*/
|
2018-11-08 12:15:00 +00:00
|
|
|
public function refactor(Node $node): ?Node
|
|
|
|
{
|
2020-02-09 11:31:31 +00:00
|
|
|
$livingCode = $this->livingCodeManipulator->keepLivingCodeFromExpr($node->expr);
|
2019-11-04 20:26:08 +00:00
|
|
|
if ($livingCode === []) {
|
2020-02-02 18:15:36 +00:00
|
|
|
return $this->removeNodeAndKeepComments($node);
|
2018-11-08 12:15:00 +00:00
|
|
|
}
|
|
|
|
|
2019-11-04 20:26:08 +00:00
|
|
|
$firstExpr = array_shift($livingCode);
|
|
|
|
$node->expr = $firstExpr;
|
|
|
|
|
2021-03-05 10:55:40 +00:00
|
|
|
foreach ($livingCode as $singleLivingCode) {
|
|
|
|
$newNode = new Expression($singleLivingCode);
|
2020-02-02 18:15:36 +00:00
|
|
|
$this->addNodeAfterNode($newNode, $node);
|
2019-11-04 20:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-12-20 12:50:55 +00:00
|
|
|
private function removeNodeAndKeepComments(Expression $expression): ?Node
|
2019-11-04 20:26:08 +00:00
|
|
|
{
|
2021-01-18 21:45:13 +00:00
|
|
|
$phpDocInfo = $this->phpDocInfoFactory->createFromNodeOrEmpty($expression);
|
2020-02-02 18:15:36 +00:00
|
|
|
|
2020-12-20 12:50:55 +00:00
|
|
|
if ($expression->getComments() !== []) {
|
2019-11-04 20:26:08 +00:00
|
|
|
$nop = new Nop();
|
2020-02-02 18:15:36 +00:00
|
|
|
$nop->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);
|
|
|
|
|
|
|
|
$this->phpDocInfoFactory->createFromNode($nop);
|
|
|
|
|
2019-11-04 20:26:08 +00:00
|
|
|
return $nop;
|
2018-11-09 14:02:00 +00:00
|
|
|
}
|
|
|
|
|
2020-12-20 12:50:55 +00:00
|
|
|
$this->removeNode($expression);
|
2018-11-08 12:15:00 +00:00
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|