diff --git a/rules/Symfony30/Rector/MethodCall/FormTypeInstanceToClassConstRector.php b/rules/Symfony30/Rector/MethodCall/FormTypeInstanceToClassConstRector.php index 893e6992..ba3915a0 100644 --- a/rules/Symfony30/Rector/MethodCall/FormTypeInstanceToClassConstRector.php +++ b/rules/Symfony30/Rector/MethodCall/FormTypeInstanceToClassConstRector.php @@ -107,20 +107,29 @@ public function refactor(Node $node): ?Node } // special case for collections + $hasChangedCollection = false; if ($this->formCollectionAnalyzer->isCollectionType($node)) { - $this->refactorCollectionOptions($node); + $hasChangedCollection = $this->refactorCollectionOptions($node); } - return $this->formInstanceToFormClassConstFetchConverter->processNewInstance($node, 1, 2); + $newMethodCall = $this->formInstanceToFormClassConstFetchConverter->processNewInstance($node, 1, 2); + if ($newMethodCall instanceof MethodCall) { + return $newMethodCall; + } + + // collection options were mutated in place, report the change via the return value + return $hasChangedCollection ? $node : null; } - private function refactorCollectionOptions(MethodCall $methodCall): void + private function refactorCollectionOptions(MethodCall $methodCall): bool { $optionsArray = $this->formOptionsArrayMatcher->match($methodCall); if (! $optionsArray instanceof Array_) { - return; + return false; } + $hasChanged = false; + foreach ($optionsArray->items as $arrayItem) { if (! $arrayItem instanceof ArrayItem) { continue; @@ -145,6 +154,9 @@ private function refactorCollectionOptions(MethodCall $methodCall): void } $arrayItem->value = $this->nodeFactory->createClassConstReference($newClass->toString()); + $hasChanged = true; } + + return $hasChanged; } }