diff --git a/rules-tests/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector/Fixture/on_class_constant_not_movable.php.inc b/rules-tests/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector/Fixture/on_class_constant_not_movable.php.inc deleted file mode 100644 index d575b6cc..00000000 --- a/rules-tests/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector/Fixture/on_class_constant_not_movable.php.inc +++ /dev/null @@ -1,41 +0,0 @@ - ------ - diff --git a/rules-tests/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector/Fixture/skip_on_class_constant_not_movable.php.inc b/rules-tests/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector/Fixture/skip_on_class_constant_not_movable.php.inc new file mode 100644 index 00000000..6c2b73fb --- /dev/null +++ b/rules-tests/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector/Fixture/skip_on_class_constant_not_movable.php.inc @@ -0,0 +1,20 @@ + diff --git a/rules-tests/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector/Fixture/skip_property_default_spread.php.inc b/rules-tests/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector/Fixture/skip_property_default_spread.php.inc new file mode 100644 index 00000000..acb0bdb1 --- /dev/null +++ b/rules-tests/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector/Fixture/skip_property_default_spread.php.inc @@ -0,0 +1,16 @@ + diff --git a/rules-tests/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector/Fixture/with_unknown_type_function_item.php.inc b/rules-tests/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector/Fixture/with_unknown_type_function_item.php.inc index af2ef26d..9745c319 100644 --- a/rules-tests/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector/Fixture/with_unknown_type_function_item.php.inc +++ b/rules-tests/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector/Fixture/with_unknown_type_function_item.php.inc @@ -30,7 +30,7 @@ class WithUnknownTypeFunctionItem public function run() { - $fruits = array_merge(['banana', 'orange'], is_array($this->getArrayOrTraversable()) ? $this->getArrayOrTraversable() : iterator_to_array($this->getArrayOrTraversable()), ['watermelon']); + $fruits = array_merge(['banana', 'orange'], is_array($items = $this->getArrayOrTraversable()) ? $items : iterator_to_array($items), ['watermelon']); } } diff --git a/rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php b/rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php index be8c5d4e..af9fbb7d 100644 --- a/rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php +++ b/rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php @@ -21,6 +21,7 @@ use PHPStan\Type\Type; use Rector\DowngradePhp81\NodeAnalyzer\ArraySpreadAnalyzer; use Rector\DowngradePhp81\NodeFactory\ArrayMergeFromArraySpreadFactory; +use Rector\NodeTypeResolver\Node\AttributeKey; use Rector\PhpParser\Node\BetterNodeFinder; use Rector\PHPStan\ScopeFetcher; use Rector\Rector\AbstractRector; @@ -112,12 +113,30 @@ public function refactor(Node $node): ?Node return null; } + // array_merge() is not allowed in a constant expression + if ($this->isInConstantExpr($node)) { + return null; + } + /** @var MutatingScope $scope */ $scope = ScopeFetcher::fetch($node); return $this->arrayMergeFromArraySpreadFactory->createFromArray($node, $scope); } + private function isInConstantExpr(Array_ $array): bool + { + if ($array->getAttribute(AttributeKey::IS_CLASS_CONST_VALUE) === true) { + return true; + } + + if ($array->getAttribute(AttributeKey::IS_DEFAULT_PROPERTY_VALUE) === true) { + return true; + } + + return $array->getAttribute(AttributeKey::IS_PARAM_DEFAULT) === true; + } + private function refactorUnderClassLike(ClassLike $classLike): ?ClassLike { $hasChanged = false; diff --git a/rules/DowngradePhp81/NodeFactory/ArrayMergeFromArraySpreadFactory.php b/rules/DowngradePhp81/NodeFactory/ArrayMergeFromArraySpreadFactory.php index e0e3c6c0..2c33aace 100644 --- a/rules/DowngradePhp81/NodeFactory/ArrayMergeFromArraySpreadFactory.php +++ b/rules/DowngradePhp81/NodeFactory/ArrayMergeFromArraySpreadFactory.php @@ -7,16 +7,25 @@ use PhpParser\Node\Arg; use PhpParser\Node\ArrayItem; use PhpParser\Node\Expr\Array_; +use PhpParser\Node\Expr\Assign; +use PhpParser\Node\Expr\CallLike; use PhpParser\Node\Expr\FuncCall; use PhpParser\Node\Expr\Ternary; +use PhpParser\Node\Expr\Variable; use PhpParser\Node\Name; use PHPStan\Analyser\MutatingScope; use PHPStan\Type\IterableType; use PHPStan\Type\ObjectType; use PHPStan\Type\Type; +use Rector\DowngradePhp\Naming\VariableNaming; final readonly class ArrayMergeFromArraySpreadFactory { + public function __construct( + private VariableNaming $variableNaming + ) { + } + public function createFromArray(Array_ $array, MutatingScope $mutatingScope): FuncCall { $newArrayItems = $this->dissolveArrayItems($array); @@ -108,6 +117,17 @@ private function createArgFromSpreadArrayItem(MutatingScope $mutatingScope, Arra } // Print a ternary, handling either an array or an iterator + if ($arrayItem->value instanceof CallLike) { + // assign the call result first, so the call runs only once + $variable = new Variable($this->variableNaming->createCountedValueName('items', $mutatingScope)); + $isArrayFuncCall = new FuncCall(new Name('is_array'), [ + new Arg(new Assign($variable, $arrayItem->value)), + ]); + $iteratorToArrayFuncCall = new FuncCall(new Name('iterator_to_array'), [new Arg($variable)]); + + return new Arg(new Ternary($isArrayFuncCall, $variable, $iteratorToArrayFuncCall)); + } + $inArrayFuncCall = new FuncCall(new Name('is_array'), [new Arg($arrayItem->value)]); return new Arg(new Ternary($inArrayFuncCall, $arrayItem->value, $iteratorToArrayFuncCall)); }