-
-
Notifications
You must be signed in to change notification settings - Fork 687
/
RemoveDeadZeroAndOneOperationRector.php
161 lines (159 loc) · 5.11 KB
/
RemoveDeadZeroAndOneOperationRector.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
<?php
declare (strict_types=1);
namespace Rector\DeadCode\Rector\Plus;
use PhpParser\Node;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\AssignOp;
use PhpParser\Node\Expr\AssignOp\Div as AssignDiv;
use PhpParser\Node\Expr\AssignOp\Minus as AssignMinus;
use PhpParser\Node\Expr\AssignOp\Mul as AssignMul;
use PhpParser\Node\Expr\AssignOp\Plus as AssignPlus;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\BinaryOp\Div;
use PhpParser\Node\Expr\BinaryOp\Minus;
use PhpParser\Node\Expr\BinaryOp\Mul;
use PhpParser\Node\Expr\BinaryOp\Plus;
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\UnaryMinus;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector\RemoveDeadZeroAndOneOperationRectorTest
*/
final class RemoveDeadZeroAndOneOperationRector extends AbstractRector
{
/**
* @readonly
* @var \Rector\PhpParser\Node\Value\ValueResolver
*/
private $valueResolver;
public function __construct(ValueResolver $valueResolver)
{
$this->valueResolver = $valueResolver;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Remove operation with 1 and 0, that have no effect on the value', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$value = 5 * 1;
$value = 5 + 0;
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function run()
{
$value = 5;
$value = 5;
}
}
CODE_SAMPLE
)]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes() : array
{
return [Plus::class, Minus::class, Mul::class, Div::class, AssignPlus::class, AssignMinus::class, AssignMul::class, AssignDiv::class];
}
/**
* @param Plus|Minus|Mul|Div|AssignPlus|AssignMinus|AssignMul|AssignDiv $node
*/
public function refactor(Node $node) : ?Node
{
if ($node instanceof AssignOp) {
return $this->processAssignOp($node);
}
// -, +
return $this->processBinaryOp($node);
}
private function processAssignOp(AssignOp $assignOp) : ?Expr
{
// +=, -=
if ($assignOp instanceof AssignPlus || $assignOp instanceof AssignMinus) {
if (!$this->valueResolver->isValue($assignOp->expr, 0)) {
return null;
}
if ($this->nodeTypeResolver->isNumberType($assignOp->var)) {
return $assignOp->var;
}
}
// *, /
if ($assignOp instanceof AssignMul || $assignOp instanceof AssignDiv) {
if (!$this->valueResolver->isValue($assignOp->expr, 1)) {
return null;
}
if ($this->nodeTypeResolver->isNumberType($assignOp->var)) {
return $assignOp->var;
}
}
return null;
}
private function processBinaryOp(Node $node) : ?Expr
{
if ($node instanceof Plus || $node instanceof Minus) {
return $this->processBinaryPlusAndMinus($node);
}
// *, /
if ($node instanceof Mul) {
return $this->processBinaryMulAndDiv($node);
}
if ($node instanceof Div) {
return $this->processBinaryMulAndDiv($node);
}
return null;
}
private function areNumberType(BinaryOp $binaryOp) : bool
{
if (!$this->nodeTypeResolver->isNumberType($binaryOp->left)) {
return \false;
}
return $this->nodeTypeResolver->isNumberType($binaryOp->right);
}
/**
* @param \PhpParser\Node\Expr\BinaryOp\Plus|\PhpParser\Node\Expr\BinaryOp\Minus $binaryOp
*/
private function processBinaryPlusAndMinus($binaryOp) : ?Expr
{
if (!$this->areNumberType($binaryOp)) {
return null;
}
if ($this->valueResolver->isValue($binaryOp->left, 0) && $this->nodeTypeResolver->isNumberType($binaryOp->right)) {
if ($binaryOp instanceof Minus) {
return new UnaryMinus($binaryOp->right);
}
return $binaryOp->right;
}
if (!$this->valueResolver->isValue($binaryOp->right, 0)) {
return null;
}
return $binaryOp->left;
}
/**
* @param \PhpParser\Node\Expr\BinaryOp\Mul|\PhpParser\Node\Expr\BinaryOp\Div $binaryOp
*/
private function processBinaryMulAndDiv($binaryOp) : ?Expr
{
if ($binaryOp->left instanceof ClassConstFetch || $binaryOp->right instanceof ClassConstFetch) {
return null;
}
if (!$this->areNumberType($binaryOp)) {
return null;
}
if ($binaryOp instanceof Mul && $this->valueResolver->isValue($binaryOp->left, 1) && $this->nodeTypeResolver->isNumberType($binaryOp->right)) {
return $binaryOp->right;
}
if (!$this->valueResolver->isValue($binaryOp->right, 1)) {
return null;
}
return $binaryOp->left;
}
}