-
-
Notifications
You must be signed in to change notification settings - Fork 333
/
ArrayKeysAndInArrayToArrayKeyExistsRector.php
147 lines (120 loc) · 4.17 KB
/
ArrayKeysAndInArrayToArrayKeyExistsRector.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
<?php
declare(strict_types=1);
namespace Rector\CodeQuality\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Assign;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\FunctionLike;
use PhpParser\Node\Name;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see \Rector\Tests\CodeQuality\Rector\FuncCall\ArrayKeysAndInArrayToArrayKeyExistsRector\ArrayKeysAndInArrayToArrayKeyExistsRectorTest
*/
final class ArrayKeysAndInArrayToArrayKeyExistsRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace array_keys() and in_array() to array_key_exists()',
[
new CodeSample(
<<<'CODE_SAMPLE'
function run($packageName, $values)
{
$keys = array_keys($values);
return in_array($packageName, $keys, true);
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
function run($packageName, $values)
{
return array_key_exists($packageName, $values);
}
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [FuncCall::class];
}
/**
* @param FuncCall $node
*/
public function refactor(Node $node): ?Node
{
if (! $this->nodeNameResolver->isName($node, 'in_array')) {
return null;
}
$args = $node->getArgs();
$secondArg = $args[1];
$arrayVariable = $secondArg->value;
$previousAssignArraysKeysFuncCall = $this->findPreviousAssignToArrayKeys($node, $arrayVariable);
if ($previousAssignArraysKeysFuncCall instanceof Assign) {
/** @var FuncCall $arrayKeysFuncCall */
$arrayKeysFuncCall = $previousAssignArraysKeysFuncCall->expr;
$this->removeNode($previousAssignArraysKeysFuncCall);
return $this->createArrayKeyExists($node, $arrayKeysFuncCall);
}
if ($arrayVariable instanceof FuncCall && $this->isName($arrayVariable, 'array_keys')) {
$arrayKeysFuncCallArgs = $arrayVariable->getArgs();
if (count($arrayKeysFuncCallArgs) > 1) {
return null;
}
// unwrap array func call
$secondArg->value = $arrayKeysFuncCallArgs[0]->value;
$node->name = new Name('array_key_exists');
unset($node->args[2]);
return $node;
}
return null;
}
private function createArrayKeyExists(FuncCall $inArrayFuncCall, FuncCall $arrayKeysFuncCall): ?FuncCall
{
if (! isset($inArrayFuncCall->args[0])) {
return null;
}
if (! $inArrayFuncCall->args[0] instanceof Arg) {
return null;
}
if (! isset($arrayKeysFuncCall->args[0])) {
return null;
}
if (! $arrayKeysFuncCall->args[0] instanceof Arg) {
return null;
}
$arguments = [$inArrayFuncCall->args[0], $arrayKeysFuncCall->args[0]];
return new FuncCall(new Name('array_key_exists'), $arguments);
}
private function findPreviousAssignToArrayKeys(FuncCall $funcCall, Expr $expr): null|Node|FunctionLike
{
return $this->betterNodeFinder->findFirstPrevious($funcCall, function (Node $node) use ($expr): bool {
// breaking out of scope
if ($node instanceof FunctionLike) {
return true;
}
if (! $node instanceof Assign) {
return ! (bool) $this->betterNodeFinder->find(
$node,
fn (Node $subNode): bool => $this->nodeComparator->areNodesEqual($expr, $subNode)
);
}
if (! $this->nodeComparator->areNodesEqual($expr, $node->var)) {
return false;
}
if (! $node->expr instanceof FuncCall) {
return false;
}
return $this->nodeNameResolver->isName($node->expr, 'array_keys');
});
}
}