Skip to content

Commit

Permalink
[NodeTraverser] Remove double apply FileWithoutNamespace, remove recu…
Browse files Browse the repository at this point in the history
…rsive lookup stmts on BetterStandardPrinter (#4097)

* [NodeTraverser] Remove double apply FileWithoutNamespace, remove recursive lookup stmts on BetterStandardPrinter

* ensure no need retraverse traversed
  • Loading branch information
samsonasik committed Jun 7, 2023
1 parent b7760eb commit f4f94bc
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ public function processNodes(
Assert::allIsInstanceOf($stmts, Stmt::class);
$this->nodeTraverser->traverse($stmts);

if (! $isScopeRefreshing) {
$isFileWithoutNamespace = count($stmts) === 1 && $stmts[0] instanceof FileWithoutNamespace;
if (! $isScopeRefreshing && ! $isFileWithoutNamespace) {
$stmts = $this->fileWithoutNamespaceNodeTraverser->traverse($stmts);

if (count($stmts) === 1 && $stmts[0] instanceof FileWithoutNamespace) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public function __construct(
*/
public function traverse(array $nodes): array
{
if (current($nodes) instanceof FileWithoutNamespace) {
return $nodes;
}

$hasNamespace = (bool) $this->nodeFinder->findFirstInstanceOf($nodes, Namespace_::class);
if (! $hasNamespace && $nodes !== []) {
$fileWithoutNamespace = new FileWithoutNamespace($nodes);
Expand Down
2 changes: 1 addition & 1 deletion src/PhpParser/Printer/BetterStandardPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -531,7 +531,7 @@ private function resolveNewStmts(array $stmts): array
$stmts = array_values($stmts);

if (count($stmts) === 1 && $stmts[0] instanceof FileWithoutNamespace) {
return $this->resolveNewStmts($stmts[0]->stmts);
return $stmts[0]->stmts;
}

return $stmts;
Expand Down

0 comments on commit f4f94bc

Please sign in to comment.