Skip to content

Commit 3ab7e36

Browse files
committed
fix: overly strict ArraySpreadInsteadOfArrayMergeRector
1 parent 78fd064 commit 3ab7e36

3 files changed

Lines changed: 34 additions & 32 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodingStyle\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector\FixturePhp81;
4+
5+
class SimpleArrayMerge
6+
{
7+
public function run($iter1, $iter2)
8+
{
9+
$values = array_merge($iter1, $iter2);
10+
}
11+
}
12+
13+
?>
14+
-----
15+
<?php
16+
17+
namespace Rector\Tests\CodingStyle\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector\FixturePhp81;
18+
19+
class SimpleArrayMerge
20+
{
21+
public function run($iter1, $iter2)
22+
{
23+
$values = [...$iter1, ...$iter2];
24+
}
25+
}
26+
27+
?>

rules-tests/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector/FixturePhp81/skip_simple_array_merge.php.inc

Lines changed: 0 additions & 11 deletions
This file was deleted.

rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
use PhpParser\Node\Expr\FuncCall;
1313
use PhpParser\Node\Expr\Ternary;
1414
use PhpParser\Node\Expr\Variable;
15-
use PHPStan\Type\ArrayType;
16-
use PHPStan\Type\Constant\ConstantArrayType;
15+
use PHPStan\Type\Type;
1716
use Rector\NodeTypeResolver\TypeAnalyzer\ArrayTypeAnalyzer;
1817
use Rector\Php\PhpVersionProvider;
1918
use Rector\Rector\AbstractRector;
@@ -117,7 +116,7 @@ private function refactorArray(FuncCall $funcCall): ?Array_
117116
}
118117

119118
$value = $arg->value;
120-
if ($this->shouldSkipArrayForInvalidTypeOrKeys($value)) {
119+
if ($this->shouldSkipArrayForInvalidKeys($value)) {
121120
return null;
122121
}
123122

@@ -134,29 +133,16 @@ private function refactorArray(FuncCall $funcCall): ?Array_
134133
return $array;
135134
}
136135

137-
private function shouldSkipArrayForInvalidTypeOrKeys(Expr $expr): bool
136+
private function shouldSkipArrayForInvalidKeys(Expr $expr): bool
138137
{
139-
// we have no idea what it is → cannot change it
140-
if (! $this->arrayTypeAnalyzer->isArrayType($expr)) {
141-
return true;
142-
}
143-
144-
$arrayStaticType = $this->getType($expr);
145-
if (! $arrayStaticType instanceof ArrayType && ! $arrayStaticType instanceof ConstantArrayType) {
146-
return true;
147-
}
138+
$type = $this->getType($expr);
148139

149-
return ! $this->isArrayKeyTypeAllowed($arrayStaticType);
150-
}
151-
152-
private function isArrayKeyTypeAllowed(ArrayType|ConstantArrayType $arrayType): bool
153-
{
154-
if ($arrayType->getIterableKeyType()->isInteger()->yes()) {
155-
return true;
140+
if ($type->getIterableKeyType()->isInteger()->yes()) {
141+
return false;
156142
}
157143

158144
// php 8.1+ allow mixed key: int, string, and null
159-
return $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_SPREAD_STRING_KEYS);
145+
return ! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_SPREAD_STRING_KEYS);
160146
}
161147

162148
private function resolveValue(Expr $expr): Expr

0 commit comments

Comments
 (0)