forked from rectorphp/rector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayKeyFirstLastRector.php
162 lines (135 loc) · 4.17 KB
/
ArrayKeyFirstLastRector.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
152
153
154
155
156
157
158
159
160
161
162
<?php
declare(strict_types=1);
namespace Rector\Php73\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Name;
use PhpParser\Node\Stmt\Expression;
use PHPStan\Reflection\ReflectionProvider;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://tomasvotruba.com/blog/2018/08/16/whats-new-in-php-73-in-30-seconds-in-diffs/#2-first-and-last-array-key
*
* This needs to removed 1 floor above, because only nodes in arrays can be removed why traversing,
* see https://github.com/nikic/PHP-Parser/issues/389
*
* @see \Rector\Tests\Php73\Rector\FuncCall\ArrayKeyFirstLastRector\ArrayKeyFirstLastRectorTest
*/
final class ArrayKeyFirstLastRector extends AbstractRector
{
/**
* @var string
*/
private const ARRAY_KEY_FIRST = 'array_key_first';
/**
* @var string
*/
private const ARRAY_KEY_LAST = 'array_key_last';
/**
* @var array<string, string>
*/
private const PREVIOUS_TO_NEW_FUNCTIONS = [
'reset' => self::ARRAY_KEY_FIRST,
'end' => self::ARRAY_KEY_LAST,
];
/**
* @var ReflectionProvider
*/
private $reflectionProvider;
public function __construct(ReflectionProvider $reflectionProvider)
{
$this->reflectionProvider = $reflectionProvider;
}
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Make use of array_key_first() and array_key_last()',
[
new CodeSample(
<<<'CODE_SAMPLE'
reset($items);
$firstKey = key($items);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$firstKey = array_key_first($items);
CODE_SAMPLE
),
new CodeSample(
<<<'CODE_SAMPLE'
end($items);
$lastKey = key($items);
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
$lastKey = array_key_last($items);
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->shouldSkip($node)) {
return null;
}
$nextExpression = $this->getNextExpression($node);
if (! $nextExpression instanceof Node) {
return null;
}
$resetOrEndFuncCall = $node;
$keyFuncCall = $this->betterNodeFinder->findFirst($nextExpression, function (Node $node) use (
$resetOrEndFuncCall
): bool {
if (! $node instanceof FuncCall) {
return false;
}
if (! $this->isName($node, 'key')) {
return false;
}
return $this->nodeComparator->areNodesEqual($resetOrEndFuncCall->args[0], $node->args[0]);
});
if (! $keyFuncCall instanceof FuncCall) {
return null;
}
$newName = self::PREVIOUS_TO_NEW_FUNCTIONS[$this->getName($node)];
$keyFuncCall->name = new Name($newName);
$this->removeNode($node);
return $node;
}
private function shouldSkip(FuncCall $funcCall): bool
{
if (! $this->isNames($funcCall, ['reset', 'end'])) {
return true;
}
if ($this->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_KEY_FIRST_LAST)) {
return false;
}
if (! $this->reflectionProvider->hasFunction(new Name(self::ARRAY_KEY_FIRST), null)) {
return true;
}
return ! $this->reflectionProvider->hasFunction(new Name(self::ARRAY_KEY_LAST), null);
}
private function getNextExpression(FuncCall $funcCall): ?Node
{
$currentExpression = $funcCall->getAttribute(AttributeKey::CURRENT_STATEMENT);
if (! $currentExpression instanceof Expression) {
return null;
}
return $currentExpression->getAttribute(AttributeKey::NEXT_NODE);
}
}