add Differ

This commit is contained in:
TomasVotruba 2017-12-19 03:54:19 +01:00
parent 7db6007a20
commit 4145536514
4 changed files with 53 additions and 5 deletions

View File

@ -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);

View File

@ -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(
'<comment> ---------- begin diff ----------</comment>' .
'%s%%s%s' .
'<comment> ----------- end diff -----------</comment>',
PHP_EOL,
PHP_EOL
));
if ($newContent !== $oldContent) {
$diff = $this->unifiedDiffer->diff($oldContent, $newContent);
$this->symfonyStyle->writeln($diffConsoleFormatter->format($diff));
}
} else {
$this->fileProcessor->processFile($fileInfo);
}
}
}
}

View File

@ -25,3 +25,5 @@ services:
factory: ['Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory', 'create']
Symplify\PackageBuilder\Parameter\ParameterProvider: ~
PhpCsFixer\Differ\UnifiedDiffer: ~

14
test-me/SomeOldTest.php Normal file
View File

@ -0,0 +1,14 @@
<?php declare(strict_types=1);
namespace SomeNamespace;
final class SomeOldTest extends \PHPUnit\Framework\TestCase
{
/**
* @expectedException asdfas
*/
public function test()
{
}
}