-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathBinaryOpStandaloneAssignsToDirectRector.php
More file actions
176 lines (144 loc) · 4.7 KB
/
BinaryOpStandaloneAssignsToDirectRector.php
File metadata and controls
176 lines (144 loc) · 4.7 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
<?php
declare(strict_types=1);
namespace Rector\CodingStyle\Rector\ClassMethod;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\BinaryOp;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\Return_;
use Rector\CodingStyle\ValueObject\VariableAndExprAssign;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodingStyle\Rector\ClassMethod\BinaryOpStandaloneAssignsToDirectRector\BinaryOpStandaloneAssignsToDirectRectorTest
*/
final class BinaryOpStandaloneAssignsToDirectRector extends AbstractRector implements MinPhpVersionInterface
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change 2 standalone assigns to variable then binary op to direct binary op', [
new CodeSample(
<<<'CODE_SAMPLE'
function run()
{
$value = 100;
$anotherValue = 200;
return 100 <=> 200;
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
function run()
{
return 100 <=> 200;
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ClassMethod::class, Function_::class, Closure::class];
}
/**
* @param ClassMethod|Function_|Closure $node
*/
public function refactor(Node $node): ?Node
{
if ($node->stmts === null) {
return null;
}
if (count($node->stmts) !== 3) {
return null;
}
$firstStmt = $node->stmts[0];
$secondStmt = $node->stmts[1];
$thirdStmt = $node->stmts[2];
if (! $thirdStmt instanceof Return_) {
return null;
}
$firstVariableAndExprAssign = $this->matchToVariableAssignExpr($firstStmt);
if (! $firstVariableAndExprAssign instanceof VariableAndExprAssign) {
return null;
}
$secondVariableAndExprAssign = $this->matchToVariableAssignExpr($secondStmt);
if (! $secondVariableAndExprAssign instanceof VariableAndExprAssign) {
return null;
}
if (! $thirdStmt->expr instanceof BinaryOp) {
return null;
}
$binaryOp = $thirdStmt->expr;
if (! $this->nodeComparator->areNodesEqual($binaryOp->left, $firstVariableAndExprAssign->getVariable())) {
return null;
}
if (! $this->nodeComparator->areNodesEqual($binaryOp->right, $secondVariableAndExprAssign->getVariable())) {
return null;
}
$resolveParamByRefVariables = $this->resolveParamByRefVariables($node);
if ($this->isNames($binaryOp->left, $resolveParamByRefVariables)) {
return null;
}
if ($this->isNames($binaryOp->right, $resolveParamByRefVariables)) {
return null;
}
$binaryOp->left = $firstVariableAndExprAssign->getExpr();
$binaryOp->right = $secondVariableAndExprAssign->getExpr();
$node->stmts = [$thirdStmt];
return $node;
}
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::VARIADIC_PARAM;
}
/**
* @return string[]
*/
private function resolveParamByRefVariables(ClassMethod|Function_|Closure $node): array
{
$paramByRefVariables = [];
foreach ($node->params as $param) {
if (! $param->var instanceof Variable) {
continue;
}
if (! $param->byRef) {
continue;
}
$paramByRefVariables[] = $this->getName($param);
}
return $paramByRefVariables;
}
private function matchToVariableAssignExpr(Stmt $stmt): ?VariableAndExprAssign
{
if (! $stmt instanceof Expression) {
return null;
}
if (! $stmt->expr instanceof Assign) {
return null;
}
$assign = $stmt->expr;
if (! $assign->var instanceof Variable) {
return null;
}
// skip complex cases
if ($assign->expr instanceof CallLike && ! $assign->expr->isFirstClassCallable() && $assign->expr->getArgs() !== []) {
return null;
}
if ($assign->expr instanceof BinaryOp) {
return null;
}
return new VariableAndExprAssign($assign->var, $assign->expr);
}
}