Skip to content

Commit 184ce76

Browse files
authored
[FileProcessor] Remove unnecessary FileDiffFactory::createTempFileDiff() take 2 (#6528)
1 parent 0bfd08d commit 184ce76

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

src/Application/FileProcessor.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,46 +54,46 @@ public function processFile(File $file, Configuration $configuration): FileProce
5454
$fileHasChanged = false;
5555
$filePath = $file->getFilePath();
5656

57-
// 2. change nodes with Rectors
58-
$rectorWithLineChanges = null;
59-
6057
do {
6158
$file->changeHasChanged(false);
6259

60+
// 1. change nodes with Rector Rules
6361
$newStmts = $this->rectorNodeTraverser->traverse($file->getNewStmts());
6462

65-
// apply post rectors
63+
// 2. apply post rectors
6664
$postNewStmts = $this->postFileProcessor->traverse($newStmts, $file);
6765

68-
// this is needed for new tokens added in "afterTraverse()"
66+
// 3. this is needed for new tokens added in "afterTraverse()"
6967
$file->changeNewStmts($postNewStmts);
7068

71-
// 3. print to file or string
69+
// 4. print to file or string
7270
// important to detect if file has changed
7371
$this->printFile($file, $configuration, $filePath);
7472

75-
$fileHasChangedInCurrentPass = $file->hasChanged();
76-
77-
if ($fileHasChangedInCurrentPass) {
78-
$file->setFileDiff($this->fileDiffFactory->createTempFileDiff($file));
79-
$rectorWithLineChanges = $file->getRectorWithLineChanges();
80-
81-
$fileHasChanged = true;
73+
// no change in current iteration, stop
74+
if (! $file->hasChanged()) {
75+
break;
8276
}
83-
} while ($fileHasChangedInCurrentPass);
77+
78+
$fileHasChanged = true;
79+
} while (true);
8480

8581
// 5. add as cacheable if not changed at all
8682
if (! $fileHasChanged) {
8783
$this->changedFilesDetector->addCachableFile($filePath);
88-
}
84+
} else {
85+
// when changed, set final status changed to true
86+
// to ensure it make sense to verify in next process when needed
87+
$file->changeHasChanged(true);
8988

90-
if ($configuration->shouldShowDiffs() && $rectorWithLineChanges !== null) {
9189
$currentFileDiff = $this->fileDiffFactory->createFileDiffWithLineChanges(
90+
$configuration->shouldShowDiffs(),
9291
$file,
9392
$file->getOriginalFileContent(),
9493
$file->getFileContent(),
95-
$rectorWithLineChanges
94+
$file->getRectorWithLineChanges()
9695
);
96+
9797
$file->setFileDiff($currentFileDiff);
9898
}
9999

src/ChangesReporting/ValueObjectFactory/FileDiffFactory.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public function __construct(
2424
* @param RectorWithLineChange[] $rectorsWithLineChanges
2525
*/
2626
public function createFileDiffWithLineChanges(
27+
bool $shouldShowDiffs,
2728
File $file,
2829
string $oldContent,
2930
string $newContent,
@@ -34,14 +35,9 @@ public function createFileDiffWithLineChanges(
3435
// always keep the most recent diff
3536
return new FileDiff(
3637
$relativeFilePath,
37-
$this->defaultDiffer->diff($oldContent, $newContent),
38-
$this->consoleDiffer->diff($oldContent, $newContent),
38+
$shouldShowDiffs ? $this->defaultDiffer->diff($oldContent, $newContent) : '',
39+
$shouldShowDiffs ? $this->consoleDiffer->diff($oldContent, $newContent) : '',
3940
$rectorsWithLineChanges
4041
);
4142
}
42-
43-
public function createTempFileDiff(File $file): FileDiff
44-
{
45-
return $this->createFileDiffWithLineChanges($file, '', '', $file->getRectorWithLineChanges());
46-
}
4743
}

src/Differ/DefaultDiffer.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ public function __construct()
2222

2323
public function diff(string $old, string $new): string
2424
{
25-
if ($old === $new) {
26-
return '';
27-
}
28-
2925
return $this->differ->diff($old, $new);
3026
}
3127
}

0 commit comments

Comments
 (0)