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..da0cccb5cd4 100644 --- a/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php +++ b/rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php @@ -135,6 +135,14 @@ 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 + // 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(); + } + return false; } 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 */