Repository navigation
Expand file tree
/
Copy pathRemoveDeadZeroAndOneOperationRector.php
More file actions
287 lines (239 loc) · 8.08 KB
/
Copy pathRemoveDeadZeroAndOneOperationRector.php
File metadata and controls
287 lines (239 loc) · 8.08 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<?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 PhpParser\Node\Scalar\Int_;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\Rector\AbstractRector;
use Rector\RuleDoc\CodeSample\CodeSample;
use Rector\RuleDoc\RuleDefinition;
use Rector\ValueObject\Application\File;
/**
* @see \Rector\Tests\DeadCode\Rector\Plus\RemoveDeadZeroAndOneOperationRector\RemoveDeadZeroAndOneOperationRectorTest
*/
final class RemoveDeadZeroAndOneOperationRector extends AbstractRector
{
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);
}
// -, +
$result = $this->processBinaryOp($node);
if (!$result instanceof Expr) {
return null;
}
// the chosen operand can be a same-class operation that still carries a dead 0/1 at its own
// top level (e.g. 0 + 5 + 0 returns 0 + 5); such a returned node is skip-marked for this rule,
// so reduce its chain here to finish in a single pass
while ($result instanceof Plus || $result instanceof Minus || $result instanceof Mul || $result instanceof Div) {
$reduced = $this->processBinaryOp($result);
if (!$reduced instanceof Expr || $reduced === $result) {
break;
}
$result = $reduced;
}
return $result;
}
private function processAssignOp(AssignOp $assignOp): ?Expr
{
// +=, -=
if ($assignOp instanceof AssignPlus || $assignOp instanceof AssignMinus) {
if (! $this->isLiteralZero($assignOp->expr)) {
return null;
}
// on float, += 0 normalizes negative zero (-0.0) to 0.0, so it is not dead
if ($assignOp instanceof AssignPlus && ! $this->nodeTypeResolver->getNativeType($assignOp->var)
->isInteger()
->yes()) {
return null;
}
if ($this->nodeTypeResolver->isNumberType($assignOp->var)) {
return $assignOp->var;
}
}
// *, /
if ($assignOp instanceof AssignMul || $assignOp instanceof AssignDiv) {
if (! $this->isLiteralOne($assignOp->expr)) {
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);
}
private function processBinaryPlusAndMinus(Plus|Minus $binaryOp): ?Expr
{
if (! $this->isLiteralZero($binaryOp->left) && ! $this->isLiteralZero($binaryOp->right)) {
return null;
}
if (! $this->areNumberType($binaryOp)) {
return null;
}
if ($this->isLiteralZero($binaryOp->left)) {
// on float, 0 + $x normalizes negative zero (-0.0) to 0.0 and 0 - $x flips its sign,
// so the operation is not dead
if (! $this->nodeTypeResolver->getNativeType($binaryOp->right)
->isInteger()
->yes()) {
return null;
}
if ($binaryOp instanceof Minus) {
return new UnaryMinus($binaryOp->right);
}
return $binaryOp->right;
}
if (! $this->isLiteralZero($binaryOp->right)) {
return null;
}
// on float, $x + 0 normalizes negative zero (-0.0) to 0.0, so it is not dead
if ($binaryOp instanceof Plus && ! $this->nodeTypeResolver->getNativeType($binaryOp->left)
->isInteger()
->yes()) {
return null;
}
return $binaryOp->left;
}
private function isMulParenthesized(File $file, Mul $mul): bool
{
if (! $mul->right instanceof BinaryOp) {
return false;
}
$oldTokens = $file->getOldTokens();
$endTokenPost = $mul->getEndTokenPos();
if (isset($oldTokens[$endTokenPost]) && (string) $oldTokens[$endTokenPost] === ')') {
$startTokenPos = $mul->right->getStartTokenPos();
$previousEndTokenPost = $mul->left->getEndTokenPos();
while ($startTokenPos > $previousEndTokenPost) {
--$startTokenPos;
if (! isset($oldTokens[$startTokenPos])) {
return false;
}
// handle space before open parentheses
if (trim((string) $oldTokens[$startTokenPos]) === '') {
continue;
}
return (string) $oldTokens[$startTokenPos] === '(';
}
return false;
}
return false;
}
private function processBinaryMulAndDiv(Mul|Div $binaryOp): ?Expr
{
if ($binaryOp->left instanceof ClassConstFetch || $binaryOp->right instanceof ClassConstFetch) {
return null;
}
if (! $this->isLiteralOne($binaryOp->left) && ! $this->isLiteralOne($binaryOp->right)) {
return null;
}
if (! $this->areNumberType($binaryOp)) {
return null;
}
if ($binaryOp instanceof Mul && $this->isLiteralOne($binaryOp->left) && $this->nodeTypeResolver->isNumberType(
$binaryOp->right
)) {
if ($this->isMulParenthesized($this->getFile(), $binaryOp)) {
$binaryOp->right->setAttribute(AttributeKey::WRAPPED_IN_PARENTHESES, true);
}
return $binaryOp->right;
}
if (! $this->isLiteralOne($binaryOp->right)) {
return null;
}
return $binaryOp->left;
}
private function isLiteralOne(Expr $expr): bool
{
return $expr instanceof Int_ && $expr->value === 1;
}
private function isLiteralZero(Expr $expr): bool
{
return $expr instanceof Int_ && $expr->value === 0;
}
}