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\Property;
|
2018-12-25 16:00:06 +00:00
|
|
|
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node;
|
2023-06-06 11:55:37 +00:00
|
|
|
use PhpParser\Node\Expr\Assign;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Class_;
|
2023-06-06 11:55:37 +00:00
|
|
|
use PhpParser\Node\Stmt\Expression;
|
2022-06-06 17:12:56 +00:00
|
|
|
use PhpParser\Node\Stmt\Property;
|
2024-02-12 14:51:52 +00:00
|
|
|
use PhpParser\Node\Stmt\Return_;
|
2023-06-06 11:55:37 +00:00
|
|
|
use PhpParser\Node\Stmt\TraitUse;
|
|
|
|
use PhpParser\NodeTraverser;
|
2023-05-14 10:01:33 +00:00
|
|
|
use PHPStan\Analyser\Scope;
|
2023-06-06 11:55:37 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDoc\DoctrineAnnotationTagValueNode;
|
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
|
2023-09-20 12:41:26 +00:00
|
|
|
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
|
2023-06-06 11:55:37 +00:00
|
|
|
use Rector\DeadCode\NodeAnalyzer\PropertyWriteonlyAnalyzer;
|
2024-01-02 02:40:38 +00:00
|
|
|
use Rector\PhpParser\NodeFinder\PropertyFetchFinder;
|
|
|
|
use Rector\Rector\AbstractScopeAwareRector;
|
2023-06-06 11:55:37 +00:00
|
|
|
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
|
2022-06-07 09:18:30 +00:00
|
|
|
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\Property\RemoveUnusedPrivatePropertyRector\RemoveUnusedPrivatePropertyRectorTest
|
2019-09-03 09:11:45 +00:00
|
|
|
*/
|
2023-06-06 11:55:37 +00:00
|
|
|
final class RemoveUnusedPrivatePropertyRector extends AbstractScopeAwareRector
|
2018-12-25 16:00:06 +00:00
|
|
|
{
|
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2024-01-02 02:40:38 +00:00
|
|
|
* @var \Rector\PhpParser\NodeFinder\PropertyFetchFinder
|
2018-12-25 16:00:06 +00:00
|
|
|
*/
|
2023-06-06 11:55:37 +00:00
|
|
|
private $propertyFetchFinder;
|
2021-02-08 21:00:45 +00:00
|
|
|
/**
|
2023-06-11 23:01:39 +00:00
|
|
|
* @readonly
|
2023-06-06 11:55:37 +00:00
|
|
|
* @var \Rector\DeadCode\NodeAnalyzer\PropertyWriteonlyAnalyzer
|
2022-05-04 22:24:14 +00:00
|
|
|
*/
|
2023-06-06 11:55:37 +00:00
|
|
|
private $propertyWriteonlyAnalyzer;
|
2023-09-20 12:41:26 +00:00
|
|
|
/**
|
|
|
|
* @readonly
|
|
|
|
* @var \Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory
|
|
|
|
*/
|
|
|
|
private $phpDocInfoFactory;
|
|
|
|
public function __construct(PropertyFetchFinder $propertyFetchFinder, PropertyWriteonlyAnalyzer $propertyWriteonlyAnalyzer, PhpDocInfoFactory $phpDocInfoFactory)
|
2022-03-20 19:48:16 +00:00
|
|
|
{
|
2023-06-06 11:55:37 +00:00
|
|
|
$this->propertyFetchFinder = $propertyFetchFinder;
|
|
|
|
$this->propertyWriteonlyAnalyzer = $propertyWriteonlyAnalyzer;
|
2023-09-20 12:41:26 +00:00
|
|
|
$this->phpDocInfoFactory = $phpDocInfoFactory;
|
2022-03-20 19:48:16 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
public function getRuleDefinition() : RuleDefinition
|
2018-12-25 16:00:06 +00:00
|
|
|
{
|
2023-06-06 11:55:37 +00:00
|
|
|
return new RuleDefinition('Remove unused private properties', [new CodeSample(<<<'CODE_SAMPLE'
|
2018-12-25 16:00:06 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
private $property;
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2021-05-09 20:15:43 +00:00
|
|
|
, <<<'CODE_SAMPLE'
|
2018-12-25 16:00:06 +00:00
|
|
|
class SomeClass
|
|
|
|
{
|
|
|
|
}
|
2020-09-15 08:23:13 +00:00
|
|
|
CODE_SAMPLE
|
2023-06-06 11:55:37 +00:00
|
|
|
)]);
|
2018-12-25 16:00:06 +00:00
|
|
|
}
|
|
|
|
/**
|
2021-02-27 00:06:15 +00:00
|
|
|
* @return array<class-string<Node>>
|
2018-12-25 16:00:06 +00:00
|
|
|
*/
|
2021-05-09 20:15:43 +00:00
|
|
|
public function getNodeTypes() : array
|
2018-12-25 16:00:06 +00:00
|
|
|
{
|
2022-06-07 08:22:29 +00:00
|
|
|
return [Class_::class];
|
2018-12-25 16:00:06 +00:00
|
|
|
}
|
|
|
|
/**
|
2022-05-04 22:24:14 +00:00
|
|
|
* @param Class_ $node
|
2018-12-25 16:00:06 +00:00
|
|
|
*/
|
2023-05-14 10:01:33 +00:00
|
|
|
public function refactorWithScope(Node $node, Scope $scope) : ?Node
|
2018-12-25 16:00:06 +00:00
|
|
|
{
|
2023-06-06 11:55:37 +00:00
|
|
|
if ($this->shouldSkipClass($node)) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-05-30 13:05:48 +00:00
|
|
|
$hasChanged = \false;
|
2023-05-30 14:11:53 +00:00
|
|
|
foreach ($node->stmts as $key => $stmt) {
|
|
|
|
if (!$stmt instanceof Property) {
|
2023-04-01 19:30:08 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-05-30 14:11:53 +00:00
|
|
|
if ($this->shouldSkipProperty($stmt)) {
|
2022-05-04 22:24:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-06-06 11:55:37 +00:00
|
|
|
if (!$this->shouldRemoveProperty($node, $stmt)) {
|
2022-05-04 22:24:14 +00:00
|
|
|
continue;
|
|
|
|
}
|
2023-06-06 11:55:37 +00:00
|
|
|
// remove property
|
|
|
|
unset($node->stmts[$key]);
|
|
|
|
$propertyName = $this->getName($stmt);
|
|
|
|
$this->removePropertyAssigns($node, $propertyName);
|
|
|
|
$hasChanged = \true;
|
2018-12-25 16:00:06 +00:00
|
|
|
}
|
2023-06-06 11:55:37 +00:00
|
|
|
if ($hasChanged) {
|
|
|
|
return $node;
|
|
|
|
}
|
|
|
|
return null;
|
2023-04-01 19:30:08 +00:00
|
|
|
}
|
2022-06-07 08:22:29 +00:00
|
|
|
private function shouldSkipProperty(Property $property) : bool
|
2019-08-29 19:22:52 +00:00
|
|
|
{
|
2023-06-06 11:55:37 +00:00
|
|
|
// has some attribute logic
|
|
|
|
if ($property->attrGroups !== []) {
|
|
|
|
return \true;
|
|
|
|
}
|
2021-05-09 20:15:43 +00:00
|
|
|
if (\count($property->props) !== 1) {
|
|
|
|
return \true;
|
2020-04-29 11:42:11 +00:00
|
|
|
}
|
2023-06-06 11:55:37 +00:00
|
|
|
if (!$property->isPrivate()) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
// has some possible magic
|
|
|
|
if ($property->isStatic()) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
$propertyPhpDocInfo = $this->phpDocInfoFactory->createFromNode($property);
|
|
|
|
if (!$propertyPhpDocInfo instanceof PhpDocInfo) {
|
|
|
|
return \false;
|
|
|
|
}
|
|
|
|
// skip as might contain important metadata
|
|
|
|
return $propertyPhpDocInfo->hasByType(DoctrineAnnotationTagValueNode::class);
|
|
|
|
}
|
|
|
|
private function shouldRemoveProperty(Class_ $class, Property $property) : bool
|
|
|
|
{
|
|
|
|
$propertyName = $this->getName($property);
|
|
|
|
$propertyFetches = $this->propertyFetchFinder->findLocalPropertyFetchesByName($class, $propertyName);
|
|
|
|
if ($propertyFetches === []) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
return $this->propertyWriteonlyAnalyzer->arePropertyFetchesExclusivelyBeingAssignedTo($propertyFetches);
|
|
|
|
}
|
|
|
|
private function shouldSkipClass(Class_ $class) : bool
|
|
|
|
{
|
|
|
|
foreach ($class->stmts as $stmt) {
|
|
|
|
// unclear what property can be used there
|
|
|
|
if ($stmt instanceof TraitUse) {
|
|
|
|
return \true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $this->propertyWriteonlyAnalyzer->hasClassDynamicPropertyNames($class);
|
|
|
|
}
|
|
|
|
private function removePropertyAssigns(Class_ $class, string $propertyName) : void
|
|
|
|
{
|
2024-02-12 14:51:52 +00:00
|
|
|
$this->traverseNodesWithCallable($class, function (Node $node) use($class, $propertyName) {
|
|
|
|
if (!$node instanceof Expression && !$node instanceof Return_) {
|
2023-06-06 11:55:37 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
if (!$node->expr instanceof Assign) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
$assign = $node->expr;
|
2023-06-27 06:35:24 +00:00
|
|
|
if (!$this->propertyFetchFinder->isLocalPropertyFetchByName($assign->var, $class, $propertyName)) {
|
2023-06-06 11:55:37 +00:00
|
|
|
return null;
|
|
|
|
}
|
2024-02-12 14:51:52 +00:00
|
|
|
if ($node instanceof Expression) {
|
|
|
|
return NodeTraverser::REMOVE_NODE;
|
|
|
|
}
|
|
|
|
$node->expr = $node->expr->expr;
|
|
|
|
return $node;
|
2023-06-06 11:55:37 +00:00
|
|
|
});
|
2019-10-30 09:49:07 +00:00
|
|
|
}
|
2018-12-25 16:00:06 +00:00
|
|
|
}
|