-
-
Notifications
You must be signed in to change notification settings - Fork 440
Expand file tree
/
Copy pathArrayFirstLastRector.php
More file actions
185 lines (149 loc) · 5.27 KB
/
ArrayFirstLastRector.php
File metadata and controls
185 lines (149 loc) · 5.27 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
<?php
declare(strict_types=1);
namespace Rector\Php85\Rector\ArrayDimFetch;
use PhpParser\Node;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\Minus;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Scalar\Int_;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PHPStan\ScopeFetcher;
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 https://php.watch/versions/8.5/array_first-array_last
* @see \Rector\Tests\Php85\Rector\ArrayDimFetch\ArrayFirstLastRector\ArrayFirstLastRectorTest
*/
final class ArrayFirstLastRector extends AbstractRector implements MinPhpVersionInterface
{
private const string ARRAY_KEY_FIRST = 'array_key_first';
private const string ARRAY_KEY_LAST = 'array_key_last';
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Make use of array_first() and array_last()',
[
new CodeSample(
<<<'CODE_SAMPLE'
echo $array[array_key_first($array)];
echo $array[array_key_last($array)];
echo array_values($array)[0];
echo array_values($array)[count($array) - 1];
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
echo array_first($array);
echo array_last($array);
echo array_first($array);
echo array_last($array);
CODE_SAMPLE
),
]
);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [ArrayDimFetch::class];
}
/**
* @param ArrayDimFetch $node
*/
public function refactor(Node $node): ?FuncCall
{
if ($node->dim instanceof FuncCall) {
return $this->refactorArrayKeyPattern($node);
}
if ($node->var instanceof FuncCall && ($node->dim instanceof Int_ || $node->dim instanceof Minus)) {
return $this->refactorArrayValuesPattern($node);
}
return null;
}
public function provideMinPhpVersion(): int
{
return PhpVersionFeature::ARRAY_FIRST_LAST;
}
private function refactorArrayKeyPattern(ArrayDimFetch $arrayDimFetch): ?FuncCall
{
if (! $arrayDimFetch->dim instanceof FuncCall) {
return null;
}
if (! $this->isNames($arrayDimFetch->dim, [self::ARRAY_KEY_FIRST, self::ARRAY_KEY_LAST])) {
return null;
}
if ($arrayDimFetch->dim->isFirstClassCallable()) {
return null;
}
if (count($arrayDimFetch->dim->getArgs()) !== 1) {
return null;
}
if (! $this->nodeComparator->areNodesEqual($arrayDimFetch->var, $arrayDimFetch->dim->getArgs()[0]->value)) {
return null;
}
if ($this->shouldSkip($arrayDimFetch, $arrayDimFetch->var)) {
return null;
}
$functionName = $this->isName($arrayDimFetch->dim, self::ARRAY_KEY_FIRST)
? 'array_first'
: 'array_last';
return $this->nodeFactory->createFuncCall($functionName, [$arrayDimFetch->var]);
}
private function refactorArrayValuesPattern(ArrayDimFetch $arrayDimFetch): ?FuncCall
{
if (! $arrayDimFetch->var instanceof FuncCall) {
return null;
}
if (! $this->isName($arrayDimFetch->var, 'array_values')) {
return null;
}
if ($arrayDimFetch->var->isFirstClassCallable()) {
return null;
}
if (count($arrayDimFetch->var->getArgs()) !== 1) {
return null;
}
if ($this->shouldSkip($arrayDimFetch, $arrayDimFetch)) {
return null;
}
$arrayArg = $arrayDimFetch->var->getArgs()[0]
->value;
if ($arrayDimFetch->dim instanceof Int_ && $arrayDimFetch->dim->value === 0) {
return $this->nodeFactory->createFuncCall('array_first', [$arrayArg]);
}
if ($arrayDimFetch->dim instanceof Minus) {
if (! $arrayDimFetch->dim->left instanceof FuncCall) {
return null;
}
if (! $this->isName($arrayDimFetch->dim->left, 'count')) {
return null;
}
if ($arrayDimFetch->dim->left->isFirstClassCallable()) {
return null;
}
if (count($arrayDimFetch->dim->left->getArgs()) !== 1) {
return null;
}
if (! $arrayDimFetch->dim->right instanceof Int_ || $arrayDimFetch->dim->right->value !== 1) {
return null;
}
if (! $this->nodeComparator->areNodesEqual($arrayArg, $arrayDimFetch->dim->left->getArgs()[0]->value)) {
return null;
}
return $this->nodeFactory->createFuncCall('array_last', [$arrayArg]);
}
return null;
}
private function shouldSkip(ArrayDimFetch $arrayDimFetch, Node $scopeNode): bool
{
$scope = ScopeFetcher::fetch($scopeNode);
if ($scope->isInExpressionAssign($arrayDimFetch)) {
return true;
}
return (bool) $arrayDimFetch->getAttribute(AttributeKey::IS_UNSET_VAR);
}
}