From 628d85dfffaa04b51c5f5a88ec8dd6297ae66ad8 Mon Sep 17 00:00:00 2001 From: Caleb White Date: Thu, 11 Sep 2025 16:13:16 -0500 Subject: [PATCH] fix: overly strict ArraySpreadInsteadOfArrayMergeRector --- .../FixturePhp81/simple_array_merge.php.inc | 27 +++++++++++++++++ .../skip_simple_array_merge.php.inc | 11 ------- .../ArraySpreadInsteadOfArrayMergeRector.php | 29 ++++--------------- 3 files changed, 33 insertions(+), 34 deletions(-) create mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector/FixturePhp81/simple_array_merge.php.inc delete mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector/FixturePhp81/skip_simple_array_merge.php.inc diff --git a/rules-tests/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector/FixturePhp81/simple_array_merge.php.inc b/rules-tests/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector/FixturePhp81/simple_array_merge.php.inc new file mode 100644 index 00000000000..82c80916eb9 --- /dev/null +++ b/rules-tests/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector/FixturePhp81/simple_array_merge.php.inc @@ -0,0 +1,27 @@ + +----- + diff --git a/rules-tests/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector/FixturePhp81/skip_simple_array_merge.php.inc b/rules-tests/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector/FixturePhp81/skip_simple_array_merge.php.inc deleted file mode 100644 index fd4c55fef4c..00000000000 --- a/rules-tests/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector/FixturePhp81/skip_simple_array_merge.php.inc +++ /dev/null @@ -1,11 +0,0 @@ -value; - if ($this->shouldSkipArrayForInvalidTypeOrKeys($value)) { + if ($this->shouldSkipArrayForInvalidKeys($value)) { return null; } @@ -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