diff --git a/packages/ChangesReporting/Application/ErrorAndDiffCollector.php b/packages/ChangesReporting/Application/ErrorAndDiffCollector.php index 383511e08c7f..6f5f48ab5148 100644 --- a/packages/ChangesReporting/Application/ErrorAndDiffCollector.php +++ b/packages/ChangesReporting/Application/ErrorAndDiffCollector.php @@ -126,6 +126,7 @@ public function addFileDiff(SmartFileInfo $smartFileInfo, string $newContent, st $this->consoleDiffer->diff($oldContent, $newContent), $rectorChanges ); + $this->fileDiffs[$smartFileInfo->getRealPath()] = $fileDiff; } diff --git a/packages/Testing/PHPUnit/AbstractRectorTestCase.php b/packages/Testing/PHPUnit/AbstractRectorTestCase.php index 0845771f5e29..dc75afaac31e 100644 --- a/packages/Testing/PHPUnit/AbstractRectorTestCase.php +++ b/packages/Testing/PHPUnit/AbstractRectorTestCase.php @@ -238,8 +238,7 @@ private function processFileInfo(SmartFileInfo $originalFileInfo): string // mimic post-rectors return $this->fileProcessor->printToString($originalFileInfo); } elseif (Strings::match($originalFileInfo->getFilename(), StaticNonPhpFileSuffixes::getSuffixRegexPattern())) { - $nonPhpFileChange = $this->nonPhpFileProcessor->process($originalFileInfo); - return $nonPhpFileChange !== null ? $nonPhpFileChange->getNewContent() : ''; + return $this->nonPhpFileProcessor->process($originalFileInfo); } $message = sprintf( diff --git a/rules/Composer/ErrorReporting/ComposerJsonChangeReporter.php b/rules/Composer/ErrorReporting/ComposerJsonChangeReporter.php new file mode 100644 index 000000000000..aa26148c6e94 --- /dev/null +++ b/rules/Composer/ErrorReporting/ComposerJsonChangeReporter.php @@ -0,0 +1,50 @@ +configuration = $configuration; + $this->composerJsonPrinter = $composerJsonPrinter; + } + + public function reportFileContentChange(ComposerJson $composerJson, SmartFileInfo $smartFileInfo): void + { + if ($this->configuration->isDryRun()) { + return; + } + + $this->composerJsonPrinter->print($composerJson, $smartFileInfo); + + $process = new Process(explode(' ', self::COMPOSER_UPDATE), getcwd()); + $process->run(function (string $type, string $message): void { + // $type is always err https://github.com/composer/composer/issues/3795#issuecomment-76401013 + echo $message; + }); + } +} diff --git a/rules/Composer/Processor/ComposerFileProcessor.php b/rules/Composer/Processor/ComposerFileProcessor.php index e8a95516c4bc..fc675f78cfeb 100644 --- a/rules/Composer/Processor/ComposerFileProcessor.php +++ b/rules/Composer/Processor/ComposerFileProcessor.php @@ -5,22 +5,13 @@ namespace Rector\Composer\Processor; use Rector\Composer\Modifier\ComposerModifier; -use Rector\Core\Configuration\Configuration; use Rector\Core\Contract\Processor\FileProcessorInterface; -use Rector\Core\ValueObject\NonPhpFile\NonPhpFileChange; -use Symfony\Component\Process\Process; use Symplify\ComposerJsonManipulator\ComposerJsonFactory; use Symplify\ComposerJsonManipulator\Printer\ComposerJsonPrinter; -use Symplify\ComposerJsonManipulator\ValueObject\ComposerJson; use Symplify\SmartFileSystem\SmartFileInfo; final class ComposerFileProcessor implements FileProcessorInterface { - /** - * @var string - */ - private const COMPOSER_UPDATE = 'composer update'; - /** * @var ComposerJsonFactory */ @@ -36,28 +27,21 @@ final class ComposerFileProcessor implements FileProcessorInterface */ private $composerModifier; - /** - * @var Configuration - */ - private $configuration; - public function __construct( ComposerJsonFactory $composerJsonFactory, ComposerJsonPrinter $composerJsonPrinter, - Configuration $configuration, ComposerModifier $composerModifier ) { $this->composerJsonFactory = $composerJsonFactory; $this->composerJsonPrinter = $composerJsonPrinter; - $this->configuration = $configuration; $this->composerModifier = $composerModifier; } - public function process(SmartFileInfo $smartFileInfo): ?NonPhpFileChange + public function process(SmartFileInfo $smartFileInfo): string { // to avoid modification of file if (! $this->composerModifier->enabled()) { - return null; + return $smartFileInfo->getContents(); } $composerJson = $this->composerJsonFactory->createFromFileInfo($smartFileInfo); @@ -66,15 +50,10 @@ public function process(SmartFileInfo $smartFileInfo): ?NonPhpFileChange // nothing has changed if ($oldComposerJson->getJsonArray() === $composerJson->getJsonArray()) { - return null; + return $smartFileInfo->getContents(); } - $oldContent = $this->composerJsonPrinter->printToString($oldComposerJson); - $newContent = $this->composerJsonPrinter->printToString($composerJson); - - $this->reportFileContentChange($composerJson, $smartFileInfo); - - return new NonPhpFileChange($oldContent, $newContent); + return $this->composerJsonPrinter->printToString($composerJson); } public function supports(SmartFileInfo $smartFileInfo): bool @@ -89,19 +68,4 @@ public function getSupportedFileExtensions(): array { return ['json']; } - - private function reportFileContentChange(ComposerJson $composerJson, SmartFileInfo $smartFileInfo): void - { - if ($this->configuration->isDryRun()) { - return; - } - - $this->composerJsonPrinter->print($composerJson, $smartFileInfo); - - $process = new Process(explode(' ', self::COMPOSER_UPDATE), getcwd()); - $process->run(function (string $type, string $message): void { - // $type is always err https://github.com/composer/composer/issues/3795#issuecomment-76401013 - echo $message; - }); - } } diff --git a/src/Contract/Processor/FileProcessorInterface.php b/src/Contract/Processor/FileProcessorInterface.php index ebe2de490386..43be197a6078 100644 --- a/src/Contract/Processor/FileProcessorInterface.php +++ b/src/Contract/Processor/FileProcessorInterface.php @@ -3,12 +3,11 @@ namespace Rector\Core\Contract\Processor; -use Rector\Core\ValueObject\NonPhpFile\NonPhpFileChange; use Symplify\SmartFileSystem\SmartFileInfo; interface FileProcessorInterface { - public function process(SmartFileInfo $smartFileInfo): ?NonPhpFileChange; + public function process(SmartFileInfo $smartFileInfo): string; public function supports(SmartFileInfo $smartFileInfo): bool; diff --git a/src/NonPhpFile/FileProcessor.php b/src/NonPhpFile/FileProcessor.php index ca0d180d5aba..c41d3df4a1dd 100644 --- a/src/NonPhpFile/FileProcessor.php +++ b/src/NonPhpFile/FileProcessor.php @@ -7,7 +7,6 @@ use Rector\Core\Configuration\Configuration; use Rector\Core\Contract\Processor\FileProcessorInterface; use Rector\Core\FileSystem\FilesFinder; -use Rector\Core\ValueObject\NonPhpFile\NonPhpFileChange; use Symplify\SmartFileSystem\SmartFileInfo; use Symplify\SmartFileSystem\SmartFileSystem; @@ -60,8 +59,8 @@ public function __construct( */ public function runOnPaths(array $paths): void { - $nonPhpFileInfos = $this->collectNonPhpFiles($paths); - $this->runNonPhpFileProcessors($nonPhpFileInfos); + $fileInfos = $this->findFileInfos($paths); + $this->runNonPhpFileProcessors($fileInfos); } /** @@ -75,26 +74,19 @@ private function runNonPhpFileProcessors(array $nonPhpFileInfos): void continue; } - $nonPhpFileChange = $nonPhpFileProcessor->process($nonPhpFileInfo); - if (! $nonPhpFileChange instanceof NonPhpFileChange) { + $oldContent = $nonPhpFileInfo->getContents(); + $newContent = $nonPhpFileProcessor->process($nonPhpFileInfo); + if ($oldContent === $newContent) { continue; } - $this->errorAndDiffCollector->addFileDiff( - $nonPhpFileInfo, - $nonPhpFileChange->getNewContent(), - $nonPhpFileChange->getOldContent() - ); + $this->errorAndDiffCollector->addFileDiff($nonPhpFileInfo, $oldContent, $newContent); if ($this->configuration->isDryRun()) { return; } - $this->smartFileSystem->dumpFile( - $nonPhpFileInfo->getPathname(), - $nonPhpFileChange->getNewContent() - ); - $this->smartFileSystem->chmod($nonPhpFileInfo->getRealPath(), $nonPhpFileInfo->getPerms()); + $this->dumpFileInfo($nonPhpFileInfo, $newContent); } } } @@ -103,7 +95,7 @@ private function runNonPhpFileProcessors(array $nonPhpFileInfos): void * @param string[] $paths * @return SmartFileInfo[] */ - private function collectNonPhpFiles(array $paths): array + private function findFileInfos(array $paths): array { $fileExtensions = $this->resolveSupportedFileExtensions(); @@ -130,4 +122,10 @@ private function resolveSupportedFileExtensions(): array return array_unique($fileExtensions); } + + private function dumpFileInfo(SmartFileInfo $nonPhpFileInfo, string $newContent): void + { + $this->smartFileSystem->dumpFile($nonPhpFileInfo->getPathname(), $newContent); + $this->smartFileSystem->chmod($nonPhpFileInfo->getRealPath(), $nonPhpFileInfo->getPerms()); + } } diff --git a/src/NonPhpFile/NonPhpFileProcessor.php b/src/NonPhpFile/NonPhpFileProcessor.php index 248b4c6bba22..543e65f21194 100644 --- a/src/NonPhpFile/NonPhpFileProcessor.php +++ b/src/NonPhpFile/NonPhpFileProcessor.php @@ -6,7 +6,6 @@ use Rector\Core\Configuration\RenamedClassesDataCollector; use Rector\Core\Contract\Processor\FileProcessorInterface; -use Rector\Core\ValueObject\NonPhpFile\NonPhpFileChange; use Rector\Core\ValueObject\StaticNonPhpFileSuffixes; use Rector\PSR4\Collector\RenamedClassesCollector; use Symplify\SmartFileSystem\SmartFileInfo; @@ -41,7 +40,7 @@ public function __construct( $this->nonPhpFileClassRenamer = $nonPhpFileClassRenamer; } - public function process(SmartFileInfo $smartFileInfo): ?NonPhpFileChange + public function process(SmartFileInfo $smartFileInfo): string { $oldContents = $smartFileInfo->getContents(); @@ -50,14 +49,7 @@ public function process(SmartFileInfo $smartFileInfo): ?NonPhpFileChange $this->renamedClassesCollector->getOldToNewClasses() ); - $newContents = $this->nonPhpFileClassRenamer->renameClasses($oldContents, $classRenames); - - // nothing has changed - if ($oldContents === $newContents) { - return null; - } - - return new NonPhpFileChange($oldContents, $newContents); + return $this->nonPhpFileClassRenamer->renameClasses($oldContents, $classRenames); } public function supports(SmartFileInfo $smartFileInfo): bool diff --git a/tests/NonPhpFile/Source/TextNonPhpFileProcessor.php b/tests/NonPhpFile/Source/TextNonPhpFileProcessor.php index 3651d398dde1..97b8ee71987b 100644 --- a/tests/NonPhpFile/Source/TextNonPhpFileProcessor.php +++ b/tests/NonPhpFile/Source/TextNonPhpFileProcessor.php @@ -1,23 +1,18 @@ getContents(); - $newContent = str_replace('Foo', 'Bar', $oldContent); - - return new NonPhpFileChange($oldContent, $newContent); + return str_replace('Foo', 'Bar', $oldContent); } public function supports(SmartFileInfo $smartFileInfo): bool