Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector\FixturePhp81;

class SimpleArrayMerge
{
public function run($iter1, $iter2)
{
$values = array_merge($iter1, $iter2);
}
}

?>
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector\FixturePhp81;

class SimpleArrayMerge
{
public function run($iter1, $iter2)
{
$values = [...$iter1, ...$iter2];
}
}

?>

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
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;
Expand All @@ -29,7 +26,6 @@
final class ArraySpreadInsteadOfArrayMergeRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ArrayTypeAnalyzer $arrayTypeAnalyzer,
private readonly PhpVersionProvider $phpVersionProvider,
) {
}
Expand Down Expand Up @@ -117,7 +113,7 @@ private function refactorArray(FuncCall $funcCall): ?Array_
}

$value = $arg->value;
if ($this->shouldSkipArrayForInvalidTypeOrKeys($value)) {
if ($this->shouldSkipArrayForInvalidKeys($value)) {
return null;
}

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

private function shouldSkipArrayForInvalidTypeOrKeys(Expr $expr): bool
private function shouldSkipArrayForInvalidKeys(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;
}
$type = $this->getType($expr);

return ! $this->isArrayKeyTypeAllowed($arrayStaticType);
}

private function isArrayKeyTypeAllowed(ArrayType|ConstantArrayType $arrayType): bool
{
if ($arrayType->getIterableKeyType()->isInteger()->yes()) {
return true;
if ($type->getIterableKeyType()->isInteger()->yes()) {
return false;
}

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

private function resolveValue(Expr $expr): Expr
Expand Down
Loading