Skip to content

Commit

Permalink
[Core][Printer] Re-use resolveNewStmts() from BetterStandardPrinter o…
Browse files Browse the repository at this point in the history
…n FormatPerservingPrinter (#3411)

* [Core][Printer] Re-use resolveNewStmts() from BetterStandardPrinter on FormatPerservingPrinter

* [ci-review] Rector Rectify

* count the newStmts

* set back to private

* rollback position

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Feb 25, 2023
1 parent 2cca234 commit 7a6531d
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 32 deletions.
3 changes: 1 addition & 2 deletions e2e/plain-views/expected-output.diff
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@
- <div>
+<div>
<?php echo 1 ?>
- </div>
+ </div>
</div>
----------- end diff -----------

3) src/short_open_tag_with_import_name.php:0
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 @@ -116,7 +116,7 @@ public function printFormatPreserving(array $stmts, array $origStmts, array $ori
$content = parent::printFormatPreserving($newStmts, $origStmts, $origTokens);

// add new line in case of added stmts
if (count($stmts) !== count($origStmts) && ! StringUtils::isMatch($content, self::NEWLINE_END_REGEX)) {
if (count($newStmts) !== count($origStmts) && ! StringUtils::isMatch($content, self::NEWLINE_END_REGEX)) {
$content .= $this->nl;
}

Expand Down
36 changes: 7 additions & 29 deletions src/PhpParser/Printer/FormatPerservingPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
namespace Rector\Core\PhpParser\Printer;

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 Symfony\Component\Filesystem\Filesystem;

Expand Down Expand Up @@ -43,39 +40,20 @@ public function printToFile(string $filePath, array $newStmts, array $oldStmts,

public function printParsedStmstAndTokensToString(File $file): string
{
$newStmts = $this->resolveNewStmts($file);

return $this->betterStandardPrinter->printFormatPreserving(
$newStmts,
$file->getNewStmts(),
$file->getOldStmts(),
$file->getOldTokens()
);
}

public function printParsedStmstAndTokens(File $file): string
{
$newStmts = $this->resolveNewStmts($file);

return $this->printToFile($file->getFilePath(), $newStmts, $file->getOldStmts(), $file->getOldTokens());
}

/**
* @return Stmt[]|mixed[]
*/
private function resolveNewStmts(File $file): array
{
$newStmts = $file->getNewStmts();

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

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

return $onlyStmt->stmts;
return $this->printToFile(
$file->getFilePath(),
$file->getNewStmts(),
$file->getOldStmts(),
$file->getOldTokens()
);
}
}

0 comments on commit 7a6531d

Please sign in to comment.