Skip to content

Commit 009889b

Browse files
committed
[perf][expr] remove leftover commented do-while block
1 parent 42e429c commit 009889b

2 files changed

Lines changed: 17 additions & 103 deletions

File tree

‎src/Application/FileProcessor.php‎

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

59-
// $fileHasChanged = false;
6059
$filePath = $file->getFilePath();
6160

62-
// do {
63-
// $file->changeHasChanged(false);
64-
65-
// every rule and post rector records a line change when it mutates the AST
66-
// $changeCountBeforePass = count($file->getRectorWithLineChanges());
67-
6861
// 1. change nodes with Rector Rules
6962
$newStmts = $this->rectorNodeTraverser->traverse($file->getNewStmts(), $file);
7063

@@ -74,46 +67,15 @@ public function processFile(File $file, Configuration $configuration): FileProce
7467
// 3. this is needed for new tokens added in "afterTraverse()"
7568
$file->changeNewStmts($postNewStmts);
7669

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-
// if (! $file->hasChanged()) {
86-
// continue;
87-
88-
// 4. print to file or string
89-
// important to detect if file has changed
90-
// $this->printFile($file, $configuration, $filePath);
91-
92-
// no change in current iteration, stop
70+
// no change, stop
9371
if (! $file->hasChanged()) {
9472
$this->changedFilesDetector->addCacheableFile($filePath);
9573
return new FileProcessResult([], $file->getFileDiff(), $file->hasChanged());
96-
97-
// break;
98-
99-
} else {
100-
$this->printFile($file, $configuration, $filePath);
10174
}
10275

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-
// }
76+
// 4. print to file or string
77+
$this->printFile($file, $configuration, $filePath);
11478

115-
// $file->getRectorWithLineChanges();
116-
// if ($file->hasChanged() || $rectorWithLineChanges !== []) {
11779
$currentFileDiff = $this->fileDiffFactory->createFileDiffWithLineChanges(
11880
$configuration->shouldShowDiffs(),
11981
$file,
@@ -123,7 +85,6 @@ public function processFile(File $file, Configuration $configuration): FileProce
12385
);
12486

12587
$file->setFileDiff($currentFileDiff);
126-
// }
12788

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

‎src/PhpParser/NodeTraverser/RectorNodeTraverser.php‎

Lines changed: 14 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,16 @@
1010
use PhpParser\Node\Stmt;
1111
use PhpParser\NodeVisitor;
1212
use Rector\Application\RectorRegistry;
13-
use Rector\Exception\ShouldNotHappenException;
14-
use Rector\PhpParser\Printer\BetterStandardPrinter;
1513
use Rector\Rector\RectorRunner;
1614
use Rector\ValueObject\Application\File;
1715

1816
final class RectorNodeTraverser
1917
{
20-
/**
21-
* Safety cap on local re-visits of a single node, so a rule that never settles cannot loop forever.
22-
*/
23-
private const int MAX_REVISITS = 50;
24-
2518
private bool $stopTraversal;
2619

2720
public function __construct(
2821
private readonly RectorRunner $rectorRunner,
2922
private readonly RectorRegistry $rectorRegistry,
30-
private readonly BetterStandardPrinter $betterStandardPrinter,
3123
) {
3224
}
3325

@@ -109,67 +101,33 @@ private function traverseArray(array $nodes, File $file): array
109101
}
110102

111103
/**
112-
* Applies the node's rules and traverses into its children, then re-applies its rules while a
113-
* descendant keeps changing - so a rule that depends on a change made deeper in the tree (ordered
114-
* via OrderedRectorInterface, or a plain multi-pass chain) converges within a single file pass.
115-
*
116-
* Termination is structural: the loop stops once a round reports no change, or reports a change
117-
* that leaves the node's printed form identical (a rule that keeps returning the same result).
104+
* Applies the node's rules, then traverses into its children.
118105
*
119106
* @param bool $allowListReturn whether an array / REMOVE_NODE return is valid here (only for a
120107
* node that lives inside a list, not a single sub-node slot)
121108
* @return NodeVisitor::REMOVE_NODE|Node|Node[]
122109
*/
123110
private function refactorNode(Node $node, File $file, bool $allowListReturn): int|Node|array
124111
{
125-
$lastPrint = null;
126-
$revisits = 0;
112+
$return = $this->applyRectors($node, $file);
127113

128-
while (true) {
129-
$changeCountBefore = $this->changeCount($file);
130-
$return = $this->applyRectors($node, $file);
131-
132-
if (\is_array($return) || $return === NodeVisitor::REMOVE_NODE) {
133-
if (! $allowListReturn) {
134-
throw new LogicException('RectorRunner::run() returned invalid value of type ' . gettype($return));
135-
}
136-
137-
// a list / removal replaces the node in its parent list; its children are not traversed
138-
return $return;
139-
}
140-
141-
$typeChanged = $return::class !== $node::class;
142-
$node = $return;
143-
144-
if ($typeChanged) {
145-
// stop traversing as node type changed and visitors won't work
146-
return $node;
114+
if (\is_array($return) || $return === NodeVisitor::REMOVE_NODE) {
115+
if (! $allowListReturn) {
116+
throw new LogicException('RectorRunner::run() returned invalid value of type ' . gettype($return));
147117
}
148118

149-
$this->traverseNode($node, $file);
150-
if ($this->stopTraversal) {
151-
return $node;
152-
}
153-
154-
// no rule (here or deeper) reported a change this round: the subtree is stable
155-
if ($this->changeCount($file) === $changeCountBefore) {
156-
return $node;
157-
}
119+
// a list / removal replaces the node in its parent list; its children are not traversed
120+
return $return;
121+
}
158122

159-
// a change was reported; keep going only while it actually reshapes the node
160-
$printNow = $this->betterStandardPrinter->print($node);
161-
if ($printNow === $lastPrint) {
162-
return $node;
163-
}
123+
if ($return::class !== $node::class) {
124+
// stop traversing as node type changed and visitors won't work
125+
return $return;
126+
}
164127

165-
$lastPrint = $printNow;
128+
$this->traverseNode($return, $file);
166129

167-
if (++$revisits >= self::MAX_REVISITS) {
168-
throw new ShouldNotHappenException(
169-
sprintf('Node "%s" did not converge after %d re-visits', $node->getType(), $revisits)
170-
);
171-
}
172-
}
130+
return $return;
173131
}
174132

175133
/**
@@ -211,11 +169,6 @@ private function applyRectors(Node $node, File $file): int|Node|array
211169
return $node;
212170
}
213171

214-
private function changeCount(File $file): int
215-
{
216-
return count($file->getRectorWithLineChanges());
217-
}
218-
219172
private function ensureReplacementReasonable(Node $old, Node $new): void
220173
{
221174
if ($old instanceof Stmt) {

0 commit comments

Comments
 (0)