Skip to content

Commit 628d85d

Browse files
committed
fix: overly strict ArraySpreadInsteadOfArrayMergeRector
1 parent 78fd064 commit 628d85d

3 files changed

Lines changed: 33 additions & 34 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: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@
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;
17-
use Rector\NodeTypeResolver\TypeAnalyzer\ArrayTypeAnalyzer;
1815
use Rector\Php\PhpVersionProvider;
1916
use Rector\Rector\AbstractRector;
2017
use Rector\ValueObject\PhpVersionFeature;
@@ -29,7 +26,6 @@
2926
final class ArraySpreadInsteadOfArrayMergeRector extends AbstractRector implements MinPhpVersionInterface
3027
{
3128
public function __construct(
32-
private readonly ArrayTypeAnalyzer $arrayTypeAnalyzer,
3329
private readonly PhpVersionProvider $phpVersionProvider,
3430
) {
3531
}
@@ -117,7 +113,7 @@ private function refactorArray(FuncCall $funcCall): ?Array_
117113
}
118114

119115
$value = $arg->value;
120-
if ($this->shouldSkipArrayForInvalidTypeOrKeys($value)) {
116+
if ($this->shouldSkipArrayForInvalidKeys($value)) {
121117
return null;
122118
}
123119

@@ -134,29 +130,16 @@ private function refactorArray(FuncCall $funcCall): ?Array_
134130
return $array;
135131
}
136132

137-
private function shouldSkipArrayForInvalidTypeOrKeys(Expr $expr): bool
133+
private function shouldSkipArrayForInvalidKeys(Expr $expr): bool
138134
{
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-
}
135+
$type = $this->getType($expr);
148136

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;
137+
if ($type->getIterableKeyType()->isInteger()->yes()) {
138+
return false;
156139
}
157140

158141
// php 8.1+ allow mixed key: int, string, and null
159-
return $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_SPREAD_STRING_KEYS);
142+
return ! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_SPREAD_STRING_KEYS);
160143
}
161144

162145
private function resolveValue(Expr $expr): Expr

0 commit comments

Comments
 (0)