diff --git a/packages/FileSystemRector/src/Rector/AbstractFileSystemRector.php b/packages/FileSystemRector/src/Rector/AbstractFileSystemRector.php index 96d01a6449b0..5f6b26242293 100644 --- a/packages/FileSystemRector/src/Rector/AbstractFileSystemRector.php +++ b/packages/FileSystemRector/src/Rector/AbstractFileSystemRector.php @@ -9,6 +9,7 @@ use Rector\FileSystemRector\Contract\FileSystemRectorInterface; use Rector\NodeTypeResolver\NodeScopeAndMetadataDecorator; use Rector\PhpParser\Parser\Parser; +use Rector\PhpParser\Printer\BetterStandardPrinter; use Rector\PhpParser\Printer\FormatPerservingPrinter; use Rector\Rector\AbstractRectorTrait; use Symplify\PackageBuilder\FileSystem\SmartFileInfo; @@ -61,7 +62,8 @@ public function setAbstractFileSystemRectorDependencies( FormatPerservingPrinter $formatPerservingPrinter, NodeScopeAndMetadataDecorator $nodeScopeAndMetadataDecorator, RemovedAndAddedFilesCollector $removedAndAddedFilesCollector, - Configuration $configuration + Configuration $configuration, + BetterStandardPrinter $betterStandardPrinter ): void { $this->parser = $parser; $this->lexer = $lexer; @@ -69,6 +71,7 @@ public function setAbstractFileSystemRectorDependencies( $this->nodeScopeAndMetadataDecorator = $nodeScopeAndMetadataDecorator; $this->removedAndAddedFilesCollector = $removedAndAddedFilesCollector; $this->configuration = $configuration; + $this->betterStandardPrinter = $betterStandardPrinter; } /** @@ -99,6 +102,16 @@ protected function printNodesToFilePath(array $nodes, string $fileDestination): $this->addFile($fileDestination, $fileContent); } + /** + * @param Node[] $nodes + */ + protected function printNewNodesToFilePath(array $nodes, string $fileDestination): void + { + $fileContent = $this->betterStandardPrinter->prettyPrint($nodes); + + $this->addFile($fileDestination, $fileContent); + } + protected function removeFile(SmartFileInfo $smartFileInfo): void { $this->removedAndAddedFilesCollector->removeFile($smartFileInfo); diff --git a/src/Rector/Psr4/MultipleClassFileToPsr4ClassesRector.php b/src/Rector/Psr4/MultipleClassFileToPsr4ClassesRector.php index f1e48b093821..a27bc253d505 100644 --- a/src/Rector/Psr4/MultipleClassFileToPsr4ClassesRector.php +++ b/src/Rector/Psr4/MultipleClassFileToPsr4ClassesRector.php @@ -66,7 +66,6 @@ final class SecondException extends Exception public function refactor(SmartFileInfo $smartFileInfo): void { $nodes = $this->parseFileInfoToNodes($smartFileInfo); - if ($this->shouldSkip($smartFileInfo, $nodes)) { return; } @@ -204,8 +203,11 @@ private function processNamespaceNodes(SmartFileInfo $smartFileInfo, array $name } // has file changed? - - $this->printNodesToFilePath($newStmtsSet, $fileDestination); + if ($shouldDelete) { + $this->printNewNodesToFilePath($newStmtsSet, $fileDestination); + } else { + $this->printNodesToFilePath($newStmtsSet, $fileDestination); + } } } }