-
-
Notifications
You must be signed in to change notification settings - Fork 335
/
SplitListAssignToSeparateLineRector.php
151 lines (126 loc) · 3.84 KB
/
SplitListAssignToSeparateLineRector.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
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\Assign;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayItem;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\List_;
use PhpParser\Node\Stmt\Expression;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://mobile.twitter.com/ivanhoe011/status/1246376872931401728
*
* @see \Rector\Tests\CodeQuality\Rector\Assign\SplitListAssignToSeparateLineRector\SplitListAssignToSeparateLineRectorTest
*/
final class SplitListAssignToSeparateLineRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Splits `[$a, $b] = [5, 10]` scalar assign to standalone lines',
[
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run(): void
{
[$a, $b] = [1, 2];
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run(): void
{
$a = 1;
$b = 2;
}
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Expression::class];
}
/**
* @param Expression $node
* @return Expression[]|null
*/
public function refactor(Node $node): ?array
{
if (! $node->expr instanceof Assign) {
return null;
}
$assign = $node->expr;
if ($this->shouldSkipAssign($assign)) {
return null;
}
/** @var Array_|List_ $leftArray */
$leftArray = $assign->var;
/** @var Array_ $rightArray */
$rightArray = $assign->expr;
return $this->createStandaloneAssignExpressions($leftArray, $rightArray);
}
private function shouldSkipAssign(Assign $assign): bool
{
if (! $assign->var instanceof Array_ && ! $assign->var instanceof List_) {
return true;
}
$assignExpr = $assign->expr;
if (! $assignExpr instanceof Array_) {
return true;
}
if (count($assign->var->items) !== count($assignExpr->items)) {
return true;
}
// is value swap
return $this->isValueSwap($assign->var, $assignExpr);
}
/**
* @return Expression[]
*/
private function createStandaloneAssignExpressions(Array_ | List_ $expr, Array_ $rightArray): array
{
$standaloneAssignExpresssions = [];
foreach ($expr->items as $key => $leftArrayItem) {
if ($leftArrayItem === null) {
continue;
}
$rightArrayItem = $rightArray->items[$key];
if (! $rightArrayItem instanceof ArrayItem) {
continue;
}
$assign = new Assign($leftArrayItem->value, $rightArrayItem);
$standaloneAssignExpresssions[] = new Expression($assign);
}
return $standaloneAssignExpresssions;
}
private function isValueSwap(Array_ | List_ $expr, Array_ $secondArray): bool
{
$firstArrayItemsHash = $this->getArrayItemsHash($expr);
$secondArrayItemsHash = $this->getArrayItemsHash($secondArray);
return $firstArrayItemsHash === $secondArrayItemsHash;
}
private function getArrayItemsHash(Array_ | List_ $node): string
{
$arrayItemsHashes = [];
foreach ($node->items as $arrayItem) {
$arrayItemsHashes[] = $this->nodeComparator->printWithoutComments($arrayItem);
}
sort($arrayItemsHashes);
$arrayItemsHash = implode('', $arrayItemsHashes);
return sha1($arrayItemsHash);
}
}