add FileDiff object over array

This commit is contained in:
TomasVotruba 2018-02-08 16:51:16 +01:00
parent ae7131819b
commit 6540128bce
5 changed files with 51 additions and 16 deletions

View File

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

View File

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

View File

@ -5,6 +5,7 @@ namespace Rector\Console\Output;
use Rector\Console\ConsoleStyle;
use Rector\Contract\Rector\RectorInterface;
use Rector\Rector\RectorCollector;
use Rector\Reporting\FileDiff;
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;
}
$this->consoleStyle->title(sprintf(
'%d file%s with changes',
count($diffFiles),
count($diffFiles) === 1 ? '' : 's'
count($fileDiffs),
count($fileDiffs) === 1 ? '' : 's'
));
$i = 0;
foreach ($diffFiles as $diffFile) {
$this->consoleStyle->writeln(sprintf('<options=bold>%d) %s</>', ++$i, $diffFile['file']));
foreach ($fileDiffs as $fileDiff) {
$this->consoleStyle->writeln(sprintf('<options=bold>%d) %s</>', ++$i, $fileDiff->getFile()));
$this->consoleStyle->newLine();
$this->consoleStyle->writeln($diffFile['diff']);
$this->consoleStyle->writeln($fileDiff->getDiff());
$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\:
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: ~