Skip to content

Commit a6245e1

Browse files
authored
[CodingStyle] Skip non-native array type on PHP 7.4 for ArraySpreadInsteadOfArrayMergeRector (#7248)
* [CodingStyle] Skip non-native array type on PHP 7.4 for ArraySpreadInsteadOfArrayMergeRector * [CodingStyle] Skip non-native array type on PHP 7.4 for ArraySpreadInsteadOfArrayMergeRector * [CodingStyle] Skip non-native array type on PHP 7.4 for ArraySpreadInsteadOfArrayMergeRector * [CodingStyle] Skip non-native array type on PHP 7.4 for ArraySpreadInsteadOfArrayMergeRector
1 parent 15eb8c2 commit a6245e1

3 files changed

Lines changed: 29 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodingStyle\Rector\FuncCall\ArraySpreadInsteadOfArrayMergeRector\FixturePhp74;
4+
5+
class SkipDocblockBasedTypeArrayMerge
6+
{
7+
/**
8+
* @param array<int, string> $iter1
9+
* @param array<int, string> $iter2
10+
*/
11+
public function run($iter1, $iter2)
12+
{
13+
$values = array_merge($iter1, $iter2);
14+
}
15+
}

rules/CodingStyle/Rector/FuncCall/ArraySpreadInsteadOfArrayMergeRector.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,14 @@ private function shouldSkipArrayForInvalidKeys(Expr $expr): bool
135135
$type = $this->getType($expr);
136136

137137
if ($type->getIterableKeyType()->isInteger()->yes()) {
138+
// when on PHP 8.0+, pass non-array values already error on the first place
139+
// this check avoid unpack non-array values that cause error on php 7.4 as well,
140+
// @see https://3v4l.org/DuYHu#v7.4.33
141+
if (! $this->phpVersionProvider->isAtLeastPhpVersion(PhpVersionFeature::ARRAY_ON_ARRAY_MERGE)) {
142+
$nativeType = $this->nodeTypeResolver->getNativeType($expr);
143+
return ! $nativeType->isArray()->yes();
144+
}
145+
138146
return false;
139147
}
140148

src/ValueObject/PhpVersionFeature.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -653,6 +653,12 @@ final class PhpVersionFeature
653653
*/
654654
public const MIXED_TYPE = PhpVersion::PHP_80;
655655

656+
/**
657+
* @see https://3v4l.org/OWtO5
658+
* @var int
659+
*/
660+
public const ARRAY_ON_ARRAY_MERGE = PhpVersion::PHP_80;
661+
656662
/**
657663
* @var int
658664
*/

0 commit comments

Comments
 (0)