mirror of
https://github.com/rectorphp/rector.git
synced 2024-06-04 02:10:51 +00:00
4be28bf7eb
37b06023b6
[DeadCode] Fix removal of variable-used promoted property (#593)
118 lines
4.0 KiB
PHP
118 lines
4.0 KiB
PHP
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace Rector\DeadCode\Rector\ClassMethod;
|
|
|
|
use PhpParser\Node;
|
|
use PhpParser\Node\Expr\Variable;
|
|
use PhpParser\Node\Stmt\Class_;
|
|
use PhpParser\Node\Stmt\ClassMethod;
|
|
use Rector\Core\NodeManipulator\PropertyManipulator;
|
|
use Rector\Core\PhpParser\NodeFinder\PropertyFetchFinder;
|
|
use Rector\Core\Rector\AbstractRector;
|
|
use Rector\Core\ValueObject\MethodName;
|
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
|
use Rector\NodeTypeResolver\Node\AttributeKey;
|
|
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
|
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
|
|
/**
|
|
* @see \Rector\Tests\DeadCode\Rector\ClassMethod\RemoveUnusedPromotedPropertyRector\RemoveUnusedPromotedPropertyRectorTest
|
|
*/
|
|
final class RemoveUnusedPromotedPropertyRector extends \Rector\Core\Rector\AbstractRector implements \Rector\VersionBonding\Contract\MinPhpVersionInterface
|
|
{
|
|
/**
|
|
* @var \Rector\Core\PhpParser\NodeFinder\PropertyFetchFinder
|
|
*/
|
|
private $propertyFetchFinder;
|
|
/**
|
|
* @var \Rector\Core\NodeManipulator\PropertyManipulator
|
|
*/
|
|
private $propertyManipulator;
|
|
public function __construct(\Rector\Core\PhpParser\NodeFinder\PropertyFetchFinder $propertyFetchFinder, \Rector\Core\NodeManipulator\PropertyManipulator $propertyManipulator)
|
|
{
|
|
$this->propertyFetchFinder = $propertyFetchFinder;
|
|
$this->propertyManipulator = $propertyManipulator;
|
|
}
|
|
public function getRuleDefinition() : \Symplify\RuleDocGenerator\ValueObject\RuleDefinition
|
|
{
|
|
return new \Symplify\RuleDocGenerator\ValueObject\RuleDefinition('Remove unused promoted property', [new \Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample(<<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
public function __construct(
|
|
private $someUnusedDependency,
|
|
private $usedDependency
|
|
) {
|
|
}
|
|
|
|
public function getUsedDependency()
|
|
{
|
|
return $this->usedDependency;
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
, <<<'CODE_SAMPLE'
|
|
class SomeClass
|
|
{
|
|
public function __construct(
|
|
private $usedDependency
|
|
) {
|
|
}
|
|
|
|
public function getUsedDependency()
|
|
{
|
|
return $this->usedDependency;
|
|
}
|
|
}
|
|
CODE_SAMPLE
|
|
)]);
|
|
}
|
|
/**
|
|
* @return array<class-string<Node>>
|
|
*/
|
|
public function getNodeTypes() : array
|
|
{
|
|
return [\PhpParser\Node\Stmt\ClassMethod::class];
|
|
}
|
|
/**
|
|
* @param ClassMethod $node
|
|
*/
|
|
public function refactor(\PhpParser\Node $node) : ?\PhpParser\Node
|
|
{
|
|
if (!$this->isName($node, \Rector\Core\ValueObject\MethodName::CONSTRUCT)) {
|
|
return null;
|
|
}
|
|
$class = $node->getAttribute(\Rector\NodeTypeResolver\Node\AttributeKey::CLASS_NODE);
|
|
if (!$class instanceof \PhpParser\Node\Stmt\Class_) {
|
|
return null;
|
|
}
|
|
foreach ($node->getParams() as $param) {
|
|
// only private local scope; removing public property might be dangerous
|
|
if ($param->flags !== \PhpParser\Node\Stmt\Class_::MODIFIER_PRIVATE) {
|
|
continue;
|
|
}
|
|
if ($this->propertyManipulator->isPropertyUsedInReadContext($param)) {
|
|
continue;
|
|
}
|
|
$paramName = $this->getName($param);
|
|
$propertyFetches = $this->propertyFetchFinder->findLocalPropertyFetchesByName($class, $paramName);
|
|
if ($propertyFetches !== []) {
|
|
continue;
|
|
}
|
|
// is variable used? only remove property, keep param
|
|
$variable = $this->betterNodeFinder->findVariableOfName((array) $node->stmts, $paramName);
|
|
if ($variable instanceof \PhpParser\Node\Expr\Variable) {
|
|
$param->flags = 0;
|
|
continue;
|
|
}
|
|
// remove param
|
|
$this->removeNode($param);
|
|
}
|
|
return $node;
|
|
}
|
|
public function provideMinPhpVersion() : int
|
|
{
|
|
return \Rector\Core\ValueObject\PhpVersionFeature::PROPERTY_PROMOTION;
|
|
}
|
|
}
|