-
-
Notifications
You must be signed in to change notification settings - Fork 699
/
Copy pathArraySpreadInsteadOfArrayMergeRector.php
183 lines (179 loc) · 5.79 KB
/
ArraySpreadInsteadOfArrayMergeRector.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
declare (strict_types=1);
namespace Rector\CodingStyle\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\Ternary;
use PhpParser\Node\Expr\Variable;
use PHPStan\Type\ArrayType;
use PHPStan\Type\Constant\ConstantArrayType;
use Rector\NodeTypeResolver\TypeAnalyzer\ArrayTypeAnalyzer;
use Rector\Php\PhpVersionProvider;
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 \Rector\Tests\CodingStyle\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector\Php74ArraySpreadInsteadOfArrayMergeRectorTest
* @see \Rector\Tests\CodingStyle\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector\Php81ArraySpreadInsteadOfArrayMergeRectorTest
*/
final class ArraySpreadInsteadOfArrayMergeRector extends AbstractRector implements MinPhpVersionInterface
{
/**
* @readonly
*/
private ArrayTypeAnalyzer $arrayTypeAnalyzer;
/**
* @readonly
*/
private PhpVersionProvider $phpVersionProvider;
public function __construct(ArrayTypeAnalyzer $arrayTypeAnalyzer, PhpVersionProvider $phpVersionProvider)
{
$this->arrayTypeAnalyzer = $arrayTypeAnalyzer;
$this->phpVersionProvider = $phpVersionProvider;
}
public function getRuleDefinition() : RuleDefinition
{
return new RuleDefinition('Change array_merge() to spread operator', [new CodeSample(<<<'CODE_SAMPLE'
class SomeClass
{
public function run($iter1, $iter2)
{
$values = array_merge(iterator_to_array($iter1), iterator_to_array($iter2));
// Or to generalize to all iterables
$anotherValues = array_merge(
is_array($iter1) ? $iter1 : iterator_to_array($iter1),
is_array($iter2) ? $iter2 : iterator_to_array($iter2)
);
}
}
CODE_SAMPLE
, <<<'CODE_SAMPLE'
class SomeClass
{
public function run($iter1, $iter2)
{
$values = [...$iter1, ...$iter2];
// Or to generalize to all iterables
$anotherValues = [...$iter1, ...$iter2];
}
}
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->isName($node, 'array_merge')) {
return $this->refactorArray($node);
}
return null;
}
public function provideMinPhpVersion() : int
{
return PhpVersionFeature::ARRAY_SPREAD;
}
private function refactorArray(FuncCall $funcCall) : ?Array_
{
if ($funcCall->isFirstClassCallable()) {
return null;
}
$array = new Array_();
foreach ($funcCall->args as $arg) {
if (!$arg instanceof Arg) {
continue;
}
// cannot handle unpacked arguments
if ($arg->unpack) {
return null;
}
$value = $arg->value;
if ($this->shouldSkipArrayForInvalidTypeOrKeys($value)) {
return null;
}
if ($value instanceof Array_) {
$array->items = \array_merge($array->items, $value->items);
continue;
}
$value = $this->resolveValue($value);
$array->items[] = $this->createUnpackedArrayItem($value);
}
return $array;
}
private function shouldSkipArrayForInvalidTypeOrKeys(Expr $expr) : bool
{
// we have no idea what it is → cannot change it
if (!$this->arrayTypeAnalyzer->isArrayType($expr)) {
return \true;
}
$arrayStaticType = $this->getType($expr);
if (!$arrayStaticType instanceof ArrayType && !$arrayStaticType instanceof ConstantArrayType) {
return \true;
}
return !$this->isArrayKeyTypeAllowed($arrayStaticType);
}
/**
* @param \PHPStan\Type\ArrayType|\PHPStan\Type\Constant\ConstantArrayType $arrayType
*/
private function isArrayKeyTypeAllowed($arrayType) : bool
{
if ($arrayType->getIterableKeyType()->isInteger()->yes()) {
return \true;
}
// php 8.1+ allow mixed key: int, string, and null
return $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_SPREAD_STRING_KEYS);
}
private function resolveValue(Expr $expr) : Expr
{
if ($expr instanceof FuncCall && $this->isIteratorToArrayFuncCall($expr)) {
/** @var Arg $arg */
$arg = $expr->args[0];
/** @var FuncCall $expr */
$expr = $arg->value;
}
if (!$expr instanceof Ternary) {
return $expr;
}
if (!$expr->cond instanceof FuncCall) {
return $expr;
}
if (!$this->isName($expr->cond, 'is_array')) {
return $expr;
}
if ($expr->if instanceof Variable && $this->isIteratorToArrayFuncCall($expr->else)) {
return $expr->if;
}
return $expr;
}
private function createUnpackedArrayItem(Expr $expr) : ArrayItem
{
return new ArrayItem($expr, null, \false, [], \true);
}
private function isIteratorToArrayFuncCall(Expr $expr) : bool
{
if (!$expr instanceof FuncCall) {
return \false;
}
if (!$this->nodeNameResolver->isName($expr, 'iterator_to_array')) {
return \false;
}
if ($expr->isFirstClassCallable()) {
return \false;
}
return isset($expr->getArgs()[0]);
}
}