From 414553651422d5768f92244981568397fa17f55c Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Tue, 19 Dec 2017 03:54:19 +0100 Subject: [PATCH] add Differ --- src/Application/FileProcessor.php | 2 +- src/Console/Command/ProcessCommand.php | 40 +++++++++++++++++++++++--- src/config/external-services.yml | 2 ++ test-me/SomeOldTest.php | 14 +++++++++ 4 files changed, 53 insertions(+), 5 deletions(-) create mode 100644 test-me/SomeOldTest.php diff --git a/src/Application/FileProcessor.php b/src/Application/FileProcessor.php index db7fbb20669..4e4c618ee9e 100644 --- a/src/Application/FileProcessor.php +++ b/src/Application/FileProcessor.php @@ -54,7 +54,7 @@ final class FileProcessor /** * See https://github.com/nikic/PHP-Parser/issues/344#issuecomment-298162516. */ - private function processFileToString(SplFileInfo $fileInfo): string + public function processFileToString(SplFileInfo $fileInfo): string { [$newStmts, $oldStmts, $oldTokens] = $this->nodeTraverserQueue->processFileInfo($fileInfo); diff --git a/src/Console/Command/ProcessCommand.php b/src/Console/Command/ProcessCommand.php index 5bf50b60664..b826a9ee160 100644 --- a/src/Console/Command/ProcessCommand.php +++ b/src/Console/Command/ProcessCommand.php @@ -2,18 +2,21 @@ namespace Rector\Console\Command; +use PhpCsFixer\Differ\DiffConsoleFormatter; +use PhpCsFixer\Differ\UnifiedDiffer; use Rector\Application\FileProcessor; use Rector\Console\Output\ProcessCommandReporter; use Rector\Exception\NoRectorsLoadedException; use Rector\FileSystem\PhpFilesFinder; use Rector\Naming\CommandNaming; use Rector\Rector\RectorCollector; -use SplFileInfo; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Style\SymfonyStyle; +use Symfony\Component\Finder\SplFileInfo; use Symplify\PackageBuilder\Parameter\ParameterProvider; final class ProcessCommand extends Command @@ -52,6 +55,10 @@ final class ProcessCommand extends Command * @var ParameterProvider */ private $parameterProvider; + /** + * @var UnifiedDiffer + */ + private $unifiedDiffer; public function __construct( FileProcessor $fileProcessor, @@ -59,7 +66,8 @@ final class ProcessCommand extends Command SymfonyStyle $symfonyStyle, PhpFilesFinder $phpFilesFinder, ProcessCommandReporter $processCommandReporter, - ParameterProvider $parameterProvider + ParameterProvider $parameterProvider, + UnifiedDiffer $unifiedDiffer ) { $this->fileProcessor = $fileProcessor; $this->rectorCollector = $rectorCollector; @@ -69,6 +77,7 @@ final class ProcessCommand extends Command parent::__construct(); $this->parameterProvider = $parameterProvider; + $this->unifiedDiffer = $unifiedDiffer; } protected function configure(): void @@ -80,7 +89,7 @@ final class ProcessCommand extends Command InputArgument::REQUIRED | InputArgument::IS_ARRAY, 'Files or directories to be upgraded.' ); - $this->addOption('dry-run'); + $this->addOption('dry-run', null, InputOption::VALUE_NONE, 'See diff of changes, do not save them to files.'); } protected function execute(InputInterface $input, OutputInterface $output): int @@ -89,6 +98,7 @@ final class ProcessCommand extends Command $source = $input->getArgument(self::ARGUMENT_SOURCE_NAME); $this->parameterProvider->changeParameter('source', $source); + $this->parameterProvider->changeParameter('dry-run', $input->getOption('dry-run')); $files = $this->phpFilesFinder->findInDirectoriesAndFiles($source); $this->processCommandReporter->reportLoadedRectors(); @@ -123,7 +133,29 @@ final class ProcessCommand extends Command foreach ($fileInfos as $fileInfo) { $this->symfonyStyle->writeln(sprintf(' - %s', $fileInfo->getRealPath())); - $this->fileProcessor->processFile($fileInfo); + + if ($this->parameterProvider->provideParameter('dry-run')) { + $oldContent = $fileInfo->getContents(); + $newContent = $this->fileProcessor->processFileToString($fileInfo); + + // @todo service? + $diffConsoleFormatter = new DiffConsoleFormatter(true, sprintf( + ' ---------- begin diff ----------' . + '%s%%s%s' . + ' ----------- end diff -----------', + PHP_EOL, + PHP_EOL + )); + + if ($newContent !== $oldContent) { + $diff = $this->unifiedDiffer->diff($oldContent, $newContent); + $this->symfonyStyle->writeln($diffConsoleFormatter->format($diff)); + } + + } else { + $this->fileProcessor->processFile($fileInfo); + } + } } } diff --git a/src/config/external-services.yml b/src/config/external-services.yml index 1bbb600b657..b4b020ade25 100644 --- a/src/config/external-services.yml +++ b/src/config/external-services.yml @@ -25,3 +25,5 @@ services: factory: ['Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory', 'create'] Symplify\PackageBuilder\Parameter\ParameterProvider: ~ + + PhpCsFixer\Differ\UnifiedDiffer: ~ \ No newline at end of file diff --git a/test-me/SomeOldTest.php b/test-me/SomeOldTest.php new file mode 100644 index 00000000000..bac5882ed2e --- /dev/null +++ b/test-me/SomeOldTest.php @@ -0,0 +1,14 @@ +