2019-10-13 05:59:52 +00:00
|
|
|
<?php
|
|
|
|
|
2021-05-09 20:15:43 +00:00
|
|
|
declare (strict_types=1);
|
2022-06-06 17:12:56 +00:00
|
|
|
namespace Rector\DeadCode\Rector\Expression;
|
2018-11-08 12:15:00 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
|
|
|
use PhpParser\Node\Expr\PropertyFetch;
|
|
|
|
use PhpParser\Node\Expr\StaticPropertyFetch;
|
|
|
|
use PhpParser\Node\Stmt\Expression;
|
|
|
|
use PhpParser\Node\Stmt\Nop;
|
|
|
|
use PHPStan\Reflection\Php\PhpPropertyReflection;
|
|
|
|
use Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer;
|
|
|
|
use Rector\Core\Rector\AbstractRector;
|
|
|
|
use Rector\Core\Reflection\ReflectionResolver;
|
|
|
|
use Rector\DeadCode\NodeManipulator\LivingCodeManipulator;
|
|
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
2022-06-07 09:18:30 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
2019-09-03 09:11:45 +00:00
|
|
|
/**
|
2021-03-12 22:20:25 +00:00
|
|
|
* @see \Rector\Tests\DeadCode\Rector\Expression\RemoveDeadStmtRector\RemoveDeadStmtRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
final class RemoveDeadStmtRector extends AbstractRector
|
2018-11-08 12:15:00 +00:00
|
|
|
{
|
2021-02-08 21:00:45 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-05-10 23:39:21 +00:00
|
|
|
* @var \Rector\DeadCode\NodeManipulator\LivingCodeManipulator
|
2021-02-08 21:00:45 +00:00
|
|
|
*/
|
|
|
|
private $livingCodeManipulator;
|
2021-08-07 11:42:34 +00:00
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-08-07 11:42:34 +00:00
|
|
|
* @var \Rector\Core\NodeAnalyzer\PropertyFetchAnalyzer
|
|
|
|
*/
|
|
|
|
private $propertyFetchAnalyzer;
|
|
|
|
/**
|
2021-12-04 12:47:17 +00:00
|
|
|
* @readonly
|
2021-08-07 11:42:34 +00:00
|
|
|
* @var \Rector\Core\Reflection\ReflectionResolver
|
|
|
|
*/
|
|
|
|
private $reflectionResolver;
|
2022-06-07 08:22:29 +00:00
|
|
|
public function __construct(LivingCodeManipulator $livingCodeManipulator, PropertyFetchAnalyzer $propertyFetchAnalyzer, ReflectionResolver $reflectionResolver)
|
2021-02-08 21:00:45 +00:00
|
|
|
{
|
|
|
|
$this->livingCodeManipulator = $livingCodeManipulator;
|
2021-08-07 11:42:34 +00:00
|
|
|
$this->propertyFetchAnalyzer = $propertyFetchAnalyzer;
|
|
|
|
$this->reflectionResolver = $reflectionResolver;
|
2021-02-08 21:00:45 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-11-08 12:15:00 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return new RuleDefinition('Removes dead code statements', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-11-08 12:15:00 +00:00
|
|
|
$value = 5;
|
|
|
|
$value;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-11-08 12:15:00 +00:00
|
|
|
$value = 5;
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
)]);
|
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
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-11-08 12:15:00 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Expression::class];
|
2018-11-08 12:15:00 +00:00
|
|
|
}
|
2020-02-02 18:15:36 +00:00
|
|
|
/**
|
2021-12-10 10:22:23 +00:00
|
|
|
* @param Expression $node
|
2021-05-06 08:23:37 +00:00
|
|
|
* @return Node[]|Node|null
|
2020-02-02 18:15:36 +00:00
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
public function refactor(Node $node)
|
2018-11-08 12:15:00 +00:00
|
|
|
{
|
2021-08-07 11:42:34 +00:00
|
|
|
if ($this->hasGetMagic($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
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
|
|
|
}
|
2021-05-17 23:45:44 +00:00
|
|
|
if ($livingCode === [$node->expr]) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-07-03 12:14:45 +00:00
|
|
|
$node->expr = \array_shift($livingCode);
|
2021-05-06 08:23:37 +00:00
|
|
|
$newNodes = [];
|
2021-03-05 10:55:40 +00:00
|
|
|
foreach ($livingCode as $singleLivingCode) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$newNodes[] = new Expression($singleLivingCode);
|
2019-11-04 20:26:08 +00:00
|
|
|
}
|
2021-05-06 08:23:37 +00:00
|
|
|
$newNodes[] = $node;
|
|
|
|
return $newNodes;
|
2019-11-04 20:26:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function hasGetMagic(Expression $expression) : bool
|
2021-08-07 11:42:34 +00:00
|
|
|
{
|
|
|
|
if (!$this->propertyFetchAnalyzer->isPropertyFetch($expression->expr)) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
/** @var PropertyFetch|StaticPropertyFetch $propertyFetch */
|
|
|
|
$propertyFetch = $expression->expr;
|
|
|
|
$phpPropertyReflection = $this->reflectionResolver->resolvePropertyReflectionFromPropertyFetch($propertyFetch);
|
|
|
|
/**
|
|
|
|
* property not found assume has class has __get method
|
|
|
|
* that can call non-defined property, that can have some special handling, eg: throw on special case
|
|
|
|
*/
|
2022-06-07 08:22:29 +00:00
|
|
|
return !$phpPropertyReflection instanceof PhpPropertyReflection;
|
2021-08-07 11:42:34 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +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-12-20 12:50:55 +00:00
|
|
|
if ($expression->getComments() !== []) {
|
2022-06-07 08:22:29 +00:00
|
|
|
$nop = new Nop();
|
|
|
|
$nop->setAttribute(AttributeKey::PHP_DOC_INFO, $phpDocInfo);
|
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;
|
|
|
|
}
|
|
|
|
}
|