-
-
Notifications
You must be signed in to change notification settings - Fork 336
/
RemoveUnusedPrivatePropertyRector.php
122 lines (105 loc) · 3.17 KB
/
RemoveUnusedPrivatePropertyRector.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
declare(strict_types=1);
namespace Rector\DeadCode\Rector\Property;
use PhpParser\Node;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Property;
use Rector\Core\Contract\Rector\AllowEmptyConfigurableRectorInterface;
use Rector\Core\NodeManipulator\PropertyManipulator;
use Rector\Core\Rector\AbstractRector;
use Rector\Removing\NodeManipulator\ComplexNodeRemover;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\Property\RemoveUnusedPrivatePropertyRector\RemoveUnusedPrivatePropertyRectorTest
*/
final class RemoveUnusedPrivatePropertyRector extends AbstractRector implements AllowEmptyConfigurableRectorInterface
{
/**
* @api
* @var string
*/
public const REMOVE_ASSIGN_SIDE_EFFECT = 'remove_assign_side_effect';
/**
* Default to true, which apply remove assign even has side effect.
* Set to false will allow to skip when assign has side effect.
*/
private bool $removeAssignSideEffect = true;
public function __construct(
private readonly PropertyManipulator $propertyManipulator,
private readonly ComplexNodeRemover $complexNodeRemover,
) {
}
/**
* @param mixed[] $configuration
*/
public function configure(array $configuration): void
{
$this->removeAssignSideEffect = $configuration[self::REMOVE_ASSIGN_SIDE_EFFECT] ?? (bool) current(
$configuration
);
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Remove unused private properties', [
new ConfiguredCodeSample(
<<<'CODE_SAMPLE'
class SomeClass
{
private $property;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
class SomeClass
{
}
CODE_SAMPLE
,
[
self::REMOVE_ASSIGN_SIDE_EFFECT => true,
]
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
{
$hasRemoved = false;
foreach ($node->getProperties() as $property) {
if ($this->shouldSkipProperty($property)) {
continue;
}
if ($this->propertyManipulator->isPropertyUsedInReadContext($node, $property)) {
continue;
}
// use different variable to avoid re-assign back $hasRemoved to false
// when already asssigned to true
$isRemoved = $this->complexNodeRemover->removePropertyAndUsages(
$node,
$property,
$this->removeAssignSideEffect
);
if ($isRemoved) {
$hasRemoved = true;
}
}
return $hasRemoved ? $node : null;
}
private function shouldSkipProperty(Property $property): bool
{
if (count($property->props) !== 1) {
return true;
}
return ! $property->isPrivate();
}
}