Skip to content

Commit

Permalink
Calculate diff only once per file after refactoring (#3711)
Browse files Browse the repository at this point in the history
* Calculate diff only once per file after refactoring

* fix

* fix

* fix

* ecs
  • Loading branch information
staabm committed Apr 30, 2023
1 parent a39913e commit c3aca95
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
25 changes: 24 additions & 1 deletion packages/ChangesReporting/ValueObjectFactory/FileDiffFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Rector\ChangesReporting\ValueObjectFactory;

use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Core\Console\Formatter\ConsoleDiffer;
use Rector\Core\Differ\DefaultDiffer;
use Rector\Core\FileSystem\FilePathHelper;
Expand All @@ -21,14 +22,36 @@ public function __construct(

public function createFileDiff(File $file, string $oldContent, string $newContent): FileDiff
{
return $this->createFileDiffWithLineChanges(
$file,
$oldContent,
$newContent,
$file->getRectorWithLineChanges()
);
}

/**
* @param RectorWithLineChange[] $rectorsWithLineChanges
*/
public function createFileDiffWithLineChanges(
File $file,
string $oldContent,
string $newContent,
array $rectorsWithLineChanges
): FileDiff {
$relativeFilePath = $this->filePathHelper->relativePath($file->getFilePath());

// always keep the most recent diff
return new FileDiff(
$relativeFilePath,
$this->defaultDiffer->diff($oldContent, $newContent),
$this->consoleDiffer->diff($oldContent, $newContent),
$file->getRectorWithLineChanges()
$rectorsWithLineChanges
);
}

public function createTempFileDiff(File $file): FileDiff
{
return $this->createFileDiffWithLineChanges($file, '', '', $file->getRectorWithLineChanges());
}
}
24 changes: 19 additions & 5 deletions src/Application/FileProcessor/PhpFileProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Nette\Utils\Strings;
use PHPStan\AnalysedCodeException;
use Rector\ChangesReporting\ValueObjectFactory\ErrorFactory;
use Rector\Core\Application\FileDecorator\FileDiffFileDecorator;
use Rector\ChangesReporting\ValueObjectFactory\FileDiffFactory;
use Rector\Core\Application\FileProcessor;
use Rector\Core\Application\FileSystem\RemovedAndAddedFilesCollector;
use Rector\Core\Contract\Console\OutputStyleInterface;
Expand Down Expand Up @@ -38,7 +38,7 @@ public function __construct(
private readonly FileProcessor $fileProcessor,
private readonly RemovedAndAddedFilesCollector $removedAndAddedFilesCollector,
private readonly OutputStyleInterface $rectorOutputStyle,
private readonly FileDiffFileDecorator $fileDiffFileDecorator,
private readonly FileDiffFactory $fileDiffFactory,
private readonly CurrentFileProvider $currentFileProvider,
private readonly PostFileProcessor $postFileProcessor,
private readonly ErrorFactory $errorFactory,
Expand Down Expand Up @@ -66,6 +66,7 @@ public function process(File $file, Configuration $configuration): array
}

// 2. change nodes with Rectors
$rectorWithLineChanges = null;
do {
$file->changeHasChanged(false);
$this->fileProcessor->refactor($file, $configuration);
Expand All @@ -78,8 +79,24 @@ public function process(File $file, Configuration $configuration): array
// 4. print to file or string
// important to detect if file has changed
$this->printFile($file, $configuration);

if ($file->hasChanged()) {
$file->setFileDiff($this->fileDiffFactory->createTempFileDiff($file));
$rectorWithLineChanges = $file->getRectorWithLineChanges();
}
} while ($file->hasChanged());

if ($configuration->shouldShowDiffs() && $rectorWithLineChanges !== null) {
$file->setFileDiff(
$this->fileDiffFactory->createFileDiffWithLineChanges(
$file,
$file->getOriginalFileContent(),
$file->getFileContent(),
$rectorWithLineChanges
)
);
}

// return json here
$fileDiff = $file->getFileDiff();
if (! $fileDiff instanceof FileDiff) {
Expand Down Expand Up @@ -186,9 +203,6 @@ private function printFile(File $file, Configuration $configuration): void
}

$file->changeFileContent($newContent);
if ($configuration->shouldShowDiffs()) {
$this->fileDiffFileDecorator->decorate([$file]);
}
}

private function notifyFile(File $file): void
Expand Down

0 comments on commit c3aca95

Please sign in to comment.