forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReplaceEachAssignmentWithKeyCurrentRector.php
140 lines (117 loc) · 3.84 KB
/
ReplaceEachAssignmentWithKeyCurrentRector.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
<?php
declare(strict_types=1);
namespace Rector\Php72\Rector\Assign;
use PhpParser\BuilderHelpers;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\List_;
use PhpParser\Node\Stmt\While_;
use Rector\Core\Rector\AbstractRector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\Php72\Rector\Assign\ReplaceEachAssignmentWithKeyCurrentRector\ReplaceEachAssignmentWithKeyCurrentRectorTest
*/
final class ReplaceEachAssignmentWithKeyCurrentRector extends AbstractRector
{
/**
* @var string
*/
private const KEY = 'key';
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Replace each() assign outside loop', [
new CodeSample(
<<<'CODE_SAMPLE'
$array = ['b' => 1, 'a' => 2];
$eachedArray = each($array);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$array = ['b' => 1, 'a' => 2];
$eachedArray[1] = current($array);
$eachedArray['value'] = current($array);
$eachedArray[0] = key($array);
$eachedArray['key'] = key($array);
next($array);
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Assign::class];
}
/**
* @param Assign $node
*/
public function refactor(Node $node): ?Node
{
if ($this->shouldSkip($node)) {
return null;
}
/** @var FuncCall $eachFuncCall */
$eachFuncCall = $node->expr;
$eachedVariable = $eachFuncCall->args[0]->value;
$assignVariable = $node->var;
$newNodes = $this->createNewNodes($assignVariable, $eachedVariable);
$this->addNodesAfterNode($newNodes, $node);
$this->removeNode($node);
return null;
}
private function shouldSkip(Assign $assign): bool
{
if (! $assign->expr instanceof FuncCall) {
return true;
}
if (! $this->nodeNameResolver->isName($assign->expr, 'each')) {
return true;
}
$parentNode = $assign->getAttribute(AttributeKey::PARENT_NODE);
if ($parentNode instanceof While_) {
return true;
}
// skip assign to List
if (! $parentNode instanceof Assign) {
return false;
}
return $parentNode->var instanceof List_;
}
/**
* @return array<int, Assign|FuncCall>
*/
private function createNewNodes(Expr $assignVariable, Expr $eachedVariable): array
{
$newNodes = [];
$newNodes[] = $this->createDimFetchAssignWithFuncCall($assignVariable, $eachedVariable, 1, 'current');
$newNodes[] = $this->createDimFetchAssignWithFuncCall($assignVariable, $eachedVariable, 'value', 'current');
$newNodes[] = $this->createDimFetchAssignWithFuncCall($assignVariable, $eachedVariable, 0, self::KEY);
$newNodes[] = $this->createDimFetchAssignWithFuncCall($assignVariable, $eachedVariable, self::KEY, self::KEY);
$newNodes[] = $this->nodeFactory->createFuncCall('next', [new Arg($eachedVariable)]);
return $newNodes;
}
/**
* @param string|int $dimValue
*/
private function createDimFetchAssignWithFuncCall(
Expr $assignVariable,
Expr $eachedVariable,
$dimValue,
string $functionName
): Assign {
$dim = BuilderHelpers::normalizeValue($dimValue);
$arrayDimFetch = new ArrayDimFetch($assignVariable, $dim);
return new Assign($arrayDimFetch, $this->nodeFactory->createFuncCall(
$functionName,
[new Arg($eachedVariable)]
));
}
}