Skip to content

Commit acc0fd9

Browse files
committed
Do not attempt to pretty-print new code if node was not found
1 parent 07486f5 commit acc0fd9

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/Analyser/RuleErrorTransformer.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,13 @@ public function transform(
130130
/** @var Stmt[] $newStmts */
131131
$newStmts = $traverser->traverse($newStmts);
132132

133-
$printer = new PhpPrinter(['indent' => str_repeat($indentDetector->indentCharacter, $indentDetector->indentSize)]);
134-
$newCode = $printer->printFormatPreserving($newStmts, $fileNodes, $oldTokens);
133+
if ($visitor->isFound()) {
134+
$printer = new PhpPrinter(['indent' => str_repeat($indentDetector->indentCharacter, $indentDetector->indentSize)]);
135+
$newCode = $printer->printFormatPreserving($newStmts, $fileNodes, $oldTokens);
135136

136-
if ($oldCode !== $newCode) {
137-
$fixedErrorDiff = new FixedErrorDiff($hash, $this->differ->diff($oldCode, $newCode));
137+
if ($oldCode !== $newCode) {
138+
$fixedErrorDiff = new FixedErrorDiff($hash, $this->differ->diff($oldCode, $newCode));
139+
}
138140
}
139141
}
140142

src/Fixable/ReplacingNodeVisitor.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
final class ReplacingNodeVisitor extends NodeVisitorAbstract
1111
{
1212

13+
private bool $found = false;
14+
1315
/**
1416
* @param callable(Node): Node $newNodeCallable
1517
*/
@@ -24,6 +26,8 @@ public function enterNode(Node $node): ?Node
2426
return null;
2527
}
2628

29+
$this->found = true;
30+
2731
$callable = $this->newNodeCallable;
2832
$newNode = $callable($node);
2933
if ($newNode instanceof VirtualNode) {
@@ -33,4 +37,9 @@ public function enterNode(Node $node): ?Node
3337
return $newNode;
3438
}
3539

40+
public function isFound(): bool
41+
{
42+
return $this->found;
43+
}
44+
3645
}

0 commit comments

Comments
 (0)