Skip to content

Commit b70eb13

Browse files
committed
[perf][expr] drop do while from FileProcessor
1 parent ec71117 commit b70eb13

2 files changed

Lines changed: 62 additions & 52 deletions

File tree

‎src/Application/FileProcessor.php‎

Lines changed: 54 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -56,65 +56,74 @@ public function processFile(File $file, Configuration $configuration): FileProce
5656
return new FileProcessResult([$parsingSystemError], null, false);
5757
}
5858

59-
$fileHasChanged = false;
59+
// $fileHasChanged = false;
6060
$filePath = $file->getFilePath();
6161

62-
do {
63-
$file->changeHasChanged(false);
62+
// do {
63+
// $file->changeHasChanged(false);
6464

65-
// every rule and post rector records a line change when it mutates the AST
66-
$changeCountBeforePass = count($file->getRectorWithLineChanges());
65+
// every rule and post rector records a line change when it mutates the AST
66+
// $changeCountBeforePass = count($file->getRectorWithLineChanges());
6767

68-
// 1. change nodes with Rector Rules
69-
$newStmts = $this->rectorNodeTraverser->traverse($file->getNewStmts(), $file);
68+
// 1. change nodes with Rector Rules
69+
$newStmts = $this->rectorNodeTraverser->traverse($file->getNewStmts(), $file);
7070

71-
// 2. apply post rectors
72-
$postNewStmts = $this->postFileProcessor->traverse($newStmts, $file);
71+
// 2. apply post rectors
72+
$postNewStmts = $this->postFileProcessor->traverse($newStmts, $file);
7373

74-
// 3. this is needed for new tokens added in "afterTraverse()"
75-
$file->changeNewStmts($postNewStmts);
74+
// 3. this is needed for new tokens added in "afterTraverse()"
75+
$file->changeNewStmts($postNewStmts);
7676

77-
// file never changed and this pass changed nothing: the print would just reproduce the
78-
// original content, so skip the expensive format-preserving print entirely.
79-
// once the file has changed we keep printing every pass, as a reprint can still
80-
// stabilize the output (e.g. drop a leftover trailing comma)
81-
if (! $fileHasChanged && count($file->getRectorWithLineChanges()) === $changeCountBeforePass) {
82-
break;
83-
}
84-
85-
// 4. print to file or string
86-
// important to detect if file has changed
87-
$this->printFile($file, $configuration, $filePath);
77+
// file never changed and this pass changed nothing: the print would just reproduce the
78+
// original content, so skip the expensive format-preserving print entirely.
79+
// once the file has changed we keep printing every pass, as a reprint can still
80+
// stabilize the output (e.g. drop a leftover trailing comma)
81+
// if (! $fileHasChanged && count($file->getRectorWithLineChanges()) === $changeCountBeforePass) {
82+
// break;
83+
// }
8884

89-
// no change in current iteration, stop
90-
if (! $file->hasChanged()) {
91-
break;
92-
}
85+
// if (! $file->hasChanged()) {
86+
// continue;
9387

94-
$fileHasChanged = true;
95-
} while (true);
88+
// 4. print to file or string
89+
// important to detect if file has changed
90+
// $this->printFile($file, $configuration, $filePath);
9691

97-
// 5. add as cacheable if not changed at all
98-
if (! $fileHasChanged) {
92+
// no change in current iteration, stop
93+
if (! $file->hasChanged()) {
9994
$this->changedFilesDetector->addCacheableFile($filePath);
95+
return new FileProcessResult([], $file->getFileDiff(), $file->hasChanged());
96+
97+
// break;
98+
10099
} else {
101-
// when changed, set final status changed to true
102-
// to ensure it make sense to verify in next process when needed
103-
$file->changeHasChanged(true);
100+
$this->printFile($file, $configuration, $filePath);
104101
}
105102

106-
$rectorWithLineChanges = $file->getRectorWithLineChanges();
107-
if ($file->hasChanged() || $rectorWithLineChanges !== []) {
108-
$currentFileDiff = $this->fileDiffFactory->createFileDiffWithLineChanges(
109-
$configuration->shouldShowDiffs(),
110-
$file,
111-
$file->getOriginalFileContent(),
112-
$file->getFileContent(),
113-
$file->getRectorWithLineChanges()
114-
);
115-
116-
$file->setFileDiff($currentFileDiff);
117-
}
103+
// $fileHasChanged = true;
104+
// } while (true);
105+
106+
// 5. add as cacheable if not changed at all
107+
// if (! $fileHasChanged) {
108+
// $this->changedFilesDetector->addCacheableFile($filePath);
109+
// } else {
110+
// // when changed, set final status changed to true
111+
// // to ensure it make sense to verify in next process when needed
112+
// $file->changeHasChanged(true);
113+
// }
114+
115+
$file->getRectorWithLineChanges();
116+
// if ($file->hasChanged() || $rectorWithLineChanges !== []) {
117+
$currentFileDiff = $this->fileDiffFactory->createFileDiffWithLineChanges(
118+
$configuration->shouldShowDiffs(),
119+
$file,
120+
$file->getOriginalFileContent(),
121+
$file->getFileContent(),
122+
$file->getRectorWithLineChanges()
123+
);
124+
125+
$file->setFileDiff($currentFileDiff);
126+
// }
118127

119128
return new FileProcessResult([], $file->getFileDiff(), $file->hasChanged());
120129
}

‎src/ValueObject/Application/File.php‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
final class File
1919
{
20-
private bool $hasChanged = false;
20+
// private bool $hasChanged = false;
2121

2222
private readonly string $originalFileContent;
2323

@@ -74,7 +74,7 @@ public function changeFileContent(string $newFileContent): void
7474
}
7575

7676
$this->fileContent = $newFileContent;
77-
$this->hasChanged = true;
77+
// $this->hasChanged = true;
7878
}
7979

8080
public function getOriginalFileContent(): string
@@ -84,13 +84,14 @@ public function getOriginalFileContent(): string
8484

8585
public function hasChanged(): bool
8686
{
87-
return $this->hasChanged;
87+
return $this->rectorWithLineChanges !== [];
88+
// return $this->hasChanged;
8889
}
8990

90-
public function changeHasChanged(bool $status): void
91-
{
92-
$this->hasChanged = $status;
93-
}
91+
// public function changeHasChanged(bool $status): void
92+
// {
93+
// $this->hasChanged = $status;
94+
// }
9495

9596
public function setFileDiff(FileDiff $fileDiff): void
9697
{

0 commit comments

Comments
 (0)