diff --git a/rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php b/rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php index 88c2adf0..a6b552f5 100644 --- a/rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php +++ b/rules/DowngradePhp74/Rector/Array_/DowngradeArraySpreadRector.php @@ -123,12 +123,19 @@ private function refactorUnderClassConst(ClassConst $classConst): ?ClassConst $hasChanged = false; - foreach ($arrays as $array) { - $refactorArrayConstValue = $this->refactorArrayConstValue($array); + $this->traverseNodesWithCallable($classConst->consts, function (Node $subNode) use (&$hasChanged): ?Node { + if (! $subNode instanceof Array_) { + return null; + } + + $refactorArrayConstValue = $this->refactorArrayConstValue($subNode); if ($refactorArrayConstValue instanceof Array_) { $hasChanged = true; + return $refactorArrayConstValue; } - } + + return null; + }); if ($hasChanged) { return $classConst; @@ -166,7 +173,11 @@ private function refactorArrayConstValue(Array_ $array): ?Array_ { $hasChanged = false; - foreach ($array->items as $key => $item) { + $newArray = new Array_(); + $newArray->setAttributes($array->getAttributes()); + + foreach ($array->items as $item) { + $newArray->items[] = $item; $type = $this->resolveItemType($item); if (! $type instanceof FullyQualifiedObjectType) { continue; @@ -188,8 +199,8 @@ private function refactorArrayConstValue(Array_ $array): ?Array_ $const = $constant->consts[0]; if ($const->name->toString() === $name->toString() && $const->value instanceof Array_) { - unset($array->items[$key]); - array_splice($array->items, $key, 0, $const->value->items); + array_pop($newArray->items); + $newArray->items = array_merge($newArray->items, $const->value->items); $hasChanged = true; } @@ -197,7 +208,7 @@ private function refactorArrayConstValue(Array_ $array): ?Array_ } if ($hasChanged) { - return $array; + return $newArray; } return null; diff --git a/tests/Issues/IssueDowngradeArraySpread/Fixture/merge_const.php.inc b/tests/Issues/IssueDowngradeArraySpread/Fixture/merge_const.php.inc new file mode 100644 index 00000000..b5e317dc --- /dev/null +++ b/tests/Issues/IssueDowngradeArraySpread/Fixture/merge_const.php.inc @@ -0,0 +1,86 @@ + +----- +