Skip to content

Commit

Permalink
[Core] Reduce getNewStmts() call on FormatPerservingPrinter (#1652)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Jan 9, 2022
1 parent 6cc6870 commit 3fd890b
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions src/PhpParser/Printer/FormatPerservingPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use PhpParser\Node;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Namespace_;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\ValueObject\Application\File;
use Symplify\SmartFileSystem\SmartFileInfo;
Expand Down Expand Up @@ -65,13 +66,18 @@ public function printParsedStmstAndTokens(File $file): string
*/
private function resolveNewStmts(File $file): array
{
if (count($file->getNewStmts()) === 1) {
$onlyStmt = $file->getNewStmts()[0];
if ($onlyStmt instanceof FileWithoutNamespace) {
return $onlyStmt->stmts;
}
$newStmts = $file->getNewStmts();

if (count($newStmts) !== 1) {
return $newStmts;
}

/** @var Namespace_|FileWithoutNamespace $onlyStmt */
$onlyStmt = $newStmts[0];
if (! $onlyStmt instanceof FileWithoutNamespace) {
return $newStmts;
}

return $file->getNewStmts();
return $onlyStmt->stmts;
}
}

0 comments on commit 3fd890b

Please sign in to comment.