Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/Analyser/RuleErrorTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,14 @@ private function fix(string $fixingFile, array $fileNodes, array $fixes): ?Fixed
return null;
}

$nameResolver = new NameResolver(new Collecting(), ['replaceNodes' => false]);
/** @var Stmt[] $newStmts */
$newStmts = (new NodeTraverser(
$nameResolver,
new NewClassNamesVisitor($nameResolver->getNameContext(), $this->importNames, $this->preferSelf),
))->traverse($newStmts);
if (NewClassNamesVisitor::hasNewNames($replacingVisitor->getNewNodes())) {
$nameResolver = new NameResolver(new Collecting(), ['replaceNodes' => false]);
/** @var Stmt[] $newStmts */
$newStmts = (new NodeTraverser(
$nameResolver,
new NewClassNamesVisitor($nameResolver->getNameContext(), $this->importNames, $this->preferSelf),
))->traverse($newStmts);
}

$newCode = $this->getPrinter($indent)->printFormatPreserving($newStmts, $fileNodes, $parsedFile['tokens']);
if ($parsedFile['code'] === $newCode) {
Expand Down
27 changes: 27 additions & 0 deletions src/Fixable/NewClassNamesVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,33 @@ public function __construct(
$this->phpDocParser = new PhpDocParser($config, new TypeParser($config, $constExprParser), $constExprParser);
}

/**
* Whether there is something to write in $newNodes, the nodes returned by fixes.
* The rest of the file is cloned unchanged, so the visitor would not change it.
* Traversing the whole file is skipped when this returns false,
* which matters in files with many fixable errors.
*
* @param Node[] $newNodes
*/
public static function hasNewNames(array $newNodes): bool
{
return (new NodeFinder())->findFirst($newNodes, static function (Node $node): bool {
if ($node instanceof FullyQualified && $node->getAttribute('origNode') === null) {
return true;
}

$doc = $node->getDocComment();
if ($doc === null) {
return false;
}

$originalNode = $node->getAttribute('origNode');
$originalDoc = $originalNode instanceof Node ? $originalNode->getDocComment() : null;

return $originalDoc === null || $originalDoc->getText() !== $doc->getText();
}) !== null;
}

#[Override]
public function beforeTraverse(array $nodes): ?array
{
Expand Down
15 changes: 15 additions & 0 deletions src/Fixable/ReplacingNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ final class ReplacingNodeVisitor extends NodeVisitorAbstract

private int $appliedCount = 0;

/** @var list<Node> */
private array $newNodes = [];

private bool $failed = false;

/**
Expand Down Expand Up @@ -115,9 +118,21 @@ public function leaveNode(Node $node): Node|int|null
}
}

$this->newNodes[] = $newNode;

return $newNode;
}

/**
* The nodes returned by the fixes, the rest of the file is cloned unchanged.
*
* @return list<Node>
*/
public function getNewNodes(): array
{
return $this->newNodes;
}

/**
* Whether every node was found and fixed.
*/
Expand Down
Loading