From 72190d56ec1943b0f4f5060b8c6a4fa54cddec84 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 12 Sep 2025 12:45:31 +0700 Subject: [PATCH 1/4] [CodingStyle] Skip non-native array type on PHP 7.4 for ArraySpreadInsteadOfArrayMergeRector --- .../skip_doblock_based_type_array_merge.php.inc | 15 +++++++++++++++ .../ArraySpreadInsteadOfArrayMergeRector.php | 5 +++++ 2 files changed, 20 insertions(+) create mode 100644 rules-tests/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector/FixturePhp74/skip_doblock_based_type_array_merge.php.inc diff --git a/rules-tests/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector/FixturePhp74/skip_doblock_based_type_array_merge.php.inc b/rules-tests/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector/FixturePhp74/skip_doblock_based_type_array_merge.php.inc new file mode 100644 index 00000000000..f70373dfaf8 --- /dev/null +++ b/rules-tests/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector/FixturePhp74/skip_doblock_based_type_array_merge.php.inc @@ -0,0 +1,15 @@ + $iter1 + * @param array $iter2 + */ + public function run($iter1, $iter2) + { + $values = array_merge($iter1, $iter2); + } +} diff --git a/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php b/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php index 700baaaee88..fbdbd7224e5 100644 --- a/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php +++ b/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php @@ -135,6 +135,11 @@ private function shouldSkipArrayForInvalidKeys(Expr $expr): bool $type = $this->getType($expr); if ($type->getIterableKeyType()->isInteger()->yes()) { + if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_SPREAD_STRING_KEYS)) { + $nativeType = $this->nodeTypeResolver->getNativeType($expr); + return ! $nativeType->isArray()->yes(); + } + return false; } From 6bdd527dd4eba30f9d9b1d85dc9ae3b31b7969c2 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 12 Sep 2025 12:51:33 +0700 Subject: [PATCH 2/4] [CodingStyle] Skip non-native array type on PHP 7.4 for ArraySpreadInsteadOfArrayMergeRector --- .../FuncCall/ArraySpreadInsteadOfArrayMergeRector.php | 2 +- src/ValueObject/PhpVersionFeature.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php b/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php index fbdbd7224e5..e18ffcbb12c 100644 --- a/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php +++ b/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php @@ -135,7 +135,7 @@ private function shouldSkipArrayForInvalidKeys(Expr $expr): bool $type = $this->getType($expr); if ($type->getIterableKeyType()->isInteger()->yes()) { - if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_SPREAD_STRING_KEYS)) { + if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_ON_ARRAY_MERGE)) { $nativeType = $this->nodeTypeResolver->getNativeType($expr); return ! $nativeType->isArray()->yes(); } diff --git a/src/ValueObject/PhpVersionFeature.php b/src/ValueObject/PhpVersionFeature.php index 0fb9c7daf04..4a9172fa203 100644 --- a/src/ValueObject/PhpVersionFeature.php +++ b/src/ValueObject/PhpVersionFeature.php @@ -653,6 +653,12 @@ final class PhpVersionFeature */ public const MIXED_TYPE = PhpVersion::PHP_80; + /** + * @see https://3v4l.org/OWtO5 + * @var int + */ + public const ARRAY_ON_ARRAY_MERGE = PhpVersion::PHP_80; + /** * @var int */ From 283163f8cbdbebc8be3963273ea09a9d64550dd4 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 12 Sep 2025 12:53:47 +0700 Subject: [PATCH 3/4] [CodingStyle] Skip non-native array type on PHP 7.4 for ArraySpreadInsteadOfArrayMergeRector --- .../Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php | 1 + 1 file changed, 1 insertion(+) diff --git a/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php b/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php index e18ffcbb12c..232cebbaba1 100644 --- a/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php +++ b/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php @@ -135,6 +135,7 @@ private function shouldSkipArrayForInvalidKeys(Expr $expr): bool $type = $this->getType($expr); if ($type->getIterableKeyType()->isInteger()->yes()) { + // when on PHP 8.0+, pass non-array values already error on the first place if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_ON_ARRAY_MERGE)) { $nativeType = $this->nodeTypeResolver->getNativeType($expr); return ! $nativeType->isArray()->yes(); From 8abe314dec1f0adf0938786ee087452d2a9d1427 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Fri, 12 Sep 2025 12:55:07 +0700 Subject: [PATCH 4/4] [CodingStyle] Skip non-native array type on PHP 7.4 for ArraySpreadInsteadOfArrayMergeRector --- .../Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php b/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php index 232cebbaba1..da0cccb5cd4 100644 --- a/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php +++ b/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php @@ -136,6 +136,8 @@ private function shouldSkipArrayForInvalidKeys(Expr $expr): bool if ($type->getIterableKeyType()->isInteger()->yes()) { // when on PHP 8.0+, pass non-array values already error on the first place + // this check avoid unpack non-array values that cause error on php 7.4 as well, + // @see https://3v4l.org/DuYHu#v7.4.33 if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_ON_ARRAY_MERGE)) { $nativeType = $this->nodeTypeResolver->getNativeType($expr); return ! $nativeType->isArray()->yes();