Merge pull request #314 from rectorphp/file-diff

add FileDiff object over array
This commit is contained in:
Gabriel Caruso 2018-02-08 14:14:26 -02:00 committed by GitHub
commit d08b5a0ba9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 18 deletions

View File

@ -64,8 +64,8 @@ vendor/bin/rector
### Extra Autoloading ### Extra Autoloading
Rector relies on project and autoloading of its classes. To specify own autoload file, use `--autoload-file` option: Rector relies on project and autoloading of its classes. To specify own autoload file, use `--autoload-file` option:
```bash ```bash
vendor/bin/rector process ../project --autoload-file ../project/vendor/autoload.php vendor/bin/rector process ../project --autoload-file ../project/vendor/autoload.php
``` ```

View File

@ -25,6 +25,7 @@ checkers:
- 'Rector\DeprecationExtractor\Deprecation\*' - 'Rector\DeprecationExtractor\Deprecation\*'
- 'Rector\BetterReflection\SourceLocator\Located\LocatedSource' - 'Rector\BetterReflection\SourceLocator\Located\LocatedSource'
- 'phpDocumentor\Reflection\Types\*' - 'phpDocumentor\Reflection\Types\*'
- 'Rector\Reporting\FileDiff'
Symplify\CodingStandard\Fixer\Naming\PropertyNameMatchingTypeFixer: Symplify\CodingStandard\Fixer\Naming\PropertyNameMatchingTypeFixer:
extra_skipped_classes: extra_skipped_classes:

View File

@ -13,6 +13,7 @@ use Rector\Exception\NoRectorsLoadedException;
use Rector\FileSystem\PhpFilesFinder; use Rector\FileSystem\PhpFilesFinder;
use Rector\Naming\CommandNaming; use Rector\Naming\CommandNaming;
use Rector\Rector\RectorCollector; use Rector\Rector\RectorCollector;
use Rector\Reporting\FileDiff;
use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputInterface;
@ -65,9 +66,9 @@ final class ProcessCommand extends Command
private $changedFiles = []; private $changedFiles = [];
/** /**
* @var string[][] * @var FileDiff[]
*/ */
private $diffFiles = []; private $fileDiffs = [];
/** /**
* @var AdditionalAutoloader * @var AdditionalAutoloader
@ -136,7 +137,7 @@ final class ProcessCommand extends Command
$this->processFiles($files); $this->processFiles($files);
$this->processCommandReporter->reportDiffFiles($this->diffFiles); $this->processCommandReporter->reportFileDiffs($this->fileDiffs);
$this->processCommandReporter->reportChangedFiles($this->changedFiles); $this->processCommandReporter->reportChangedFiles($this->changedFiles);
$this->consoleStyle->success('Rector is done!'); $this->consoleStyle->success('Rector is done!');
@ -189,10 +190,10 @@ final class ProcessCommand extends Command
if ($this->parameterProvider->provideParameter(Option::OPTION_DRY_RUN)) { if ($this->parameterProvider->provideParameter(Option::OPTION_DRY_RUN)) {
$newContent = $this->fileProcessor->processFileToString($fileInfo); $newContent = $this->fileProcessor->processFileToString($fileInfo);
if ($newContent !== $oldContent) { if ($newContent !== $oldContent) {
$this->diffFiles[] = [ $this->fileDiffs[] = new FileDiff(
'file' => $fileInfo->getPathname(), $fileInfo->getPathname(),
'diff' => $this->differAndFormatter->diffAndFormat($oldContent, $newContent), $this->differAndFormatter->diffAndFormat($oldContent, $newContent)
]; );
} }
} else { } else {
$newContent = $this->fileProcessor->processFile($fileInfo); $newContent = $this->fileProcessor->processFile($fileInfo);

View File

@ -5,6 +5,7 @@ namespace Rector\Console\Output;
use Rector\Console\ConsoleStyle; use Rector\Console\ConsoleStyle;
use Rector\Contract\Rector\RectorInterface; use Rector\Contract\Rector\RectorInterface;
use Rector\Rector\RectorCollector; use Rector\Rector\RectorCollector;
use Rector\Reporting\FileDiff;
final class ProcessCommandReporter final class ProcessCommandReporter
{ {
@ -57,25 +58,25 @@ final class ProcessCommandReporter
} }
/** /**
* @param string[][] $diffFiles * @param FileDiff[] $fileDiffs
*/ */
public function reportDiffFiles(array $diffFiles): void public function reportFileDiffs(array $fileDiffs): void
{ {
if (count($diffFiles) <= 0) { if (count($fileDiffs) <= 0) {
return; return;
} }
$this->consoleStyle->title(sprintf( $this->consoleStyle->title(sprintf(
'%d file%s with changes', '%d file%s with changes',
count($diffFiles), count($fileDiffs),
count($diffFiles) === 1 ? '' : 's' count($fileDiffs) === 1 ? '' : 's'
)); ));
$i = 0; $i = 0;
foreach ($diffFiles as $diffFile) { foreach ($fileDiffs as $fileDiff) {
$this->consoleStyle->writeln(sprintf('<options=bold>%d) %s</>', ++$i, $diffFile['file'])); $this->consoleStyle->writeln(sprintf('<options=bold>%d) %s</>', ++$i, $fileDiff->getFile()));
$this->consoleStyle->newLine(); $this->consoleStyle->newLine();
$this->consoleStyle->writeln($diffFile['diff']); $this->consoleStyle->writeln($fileDiff->getDiff());
$this->consoleStyle->newLine(); $this->consoleStyle->newLine();
} }
} }

View File

@ -0,0 +1,32 @@
<?php declare(strict_types=1);
namespace Rector\Reporting;
final class FileDiff
{
/**
* @var string
*/
private $diff;
/**
* @var string
*/
private $file;
public function __construct(string $file, string $diff)
{
$this->file = $file;
$this->diff = $diff;
}
public function getDiff(): string
{
return $this->diff;
}
public function getFile(): string
{
return $this->file;
}
}

View File

@ -5,7 +5,7 @@ services:
Rector\: Rector\:
resource: '../' resource: '../'
exclude: '../{Node/Attribute.php,Rector/Contrib/**/*Rector.php,Rector/Dynamic,Rector/MagicDisclosure,Testing}' exclude: '../{Node/Attribute.php,Rector/Contrib/**/*Rector.php,Rector/Dynamic,Rector/MagicDisclosure,Reporting/FileDiff.php,Testing}'
Rector\Rector\Contrib\Symfony\Form\Helper\FormTypeStringToTypeProvider: ~ Rector\Rector\Contrib\Symfony\Form\Helper\FormTypeStringToTypeProvider: ~