Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix test-suite regarding indentation #7

Merged
merged 4 commits into from
Oct 9, 2024
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
3 changes: 1 addition & 2 deletions bin/simple-downgrade
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ foreach ([__DIR__ . '/../../../autoload.php', __DIR__ . '/../vendor/autoload.php
$lexer = new \PhpParser\Lexer\Emulative();

$parser = new Php7($lexer);
$printer = new PhpPrinter();

$phpDocParserConfig = new ParserConfig(['lines' => true, 'indexes' => true]);
$constExprParser = new ConstExprParser($phpDocParserConfig);
Expand All @@ -29,6 +28,6 @@ $phpDocParser = new PhpDocParser($phpDocParserConfig, $typeParser, $constExprPar
$phpDocLexer = new PHPStan\PhpDocParser\Lexer\Lexer($phpDocParserConfig);

$app = new Application();
$app->add(new DowngradeCommand($parser, $printer, $phpDocLexer, $phpDocParser));
$app->add(new DowngradeCommand($parser, $phpDocLexer, $phpDocParser));
$app->setCatchExceptions(false);
$app->run();
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"license": ["MIT"],
"require": {
"php": "^7.4|^8.0",
"nikic/php-parser": "^5.0",
"nikic/php-parser": "^5.3.0",
"symfony/console": "^5.4",
"nette/utils": "^3.2.5",
"symfony/finder": "^5.4",
Expand Down
16 changes: 11 additions & 5 deletions src/Console/DowngradeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Nette\Utils\Strings;
use PhpParser\Internal\TokenStream;
use PhpParser\Node\Stmt;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
Expand All @@ -14,6 +15,7 @@
use PHPStan\PhpDocParser\Printer\Printer;
use SimpleDowngrader\Php\FollowedByCommaAnalyser;
use SimpleDowngrader\Php\PhpPrinter;
use SimpleDowngrader\Php\PhpPrinterIndentationDetectorVisitor;
use SimpleDowngrader\PhpDoc\PhpDocEditor;
use SimpleDowngrader\Visitor\DowngradeMixedTypeVisitor;
use SimpleDowngrader\Visitor\DowngradeNonCapturingCatchesVisitor;
Expand Down Expand Up @@ -47,6 +49,7 @@
use function is_string;
use function preg_quote;
use function sprintf;
use function str_repeat;
use function str_replace;

class DowngradeCommand extends Command
Expand All @@ -57,19 +60,16 @@ class DowngradeCommand extends Command

private Parser $parser;

private PhpPrinter $printer;

private Lexer $phpDocLexer;

private PhpDocParser $phpDocParser;

private NodeTraverser $cloningTraverser;

public function __construct(Parser $parser, PhpPrinter $printer, Lexer $phpDocLexer, PhpDocParser $phpDocParser)
public function __construct(Parser $parser, Lexer $phpDocLexer, PhpDocParser $phpDocParser)
{
parent::__construct();
$this->parser = $parser;
$this->printer = $printer;
$this->phpDocLexer = $phpDocLexer;
$this->phpDocParser = $phpDocParser;
$this->cloningTraverser = new NodeTraverser();
Expand Down Expand Up @@ -142,6 +142,11 @@ private function processFile(string $file, array $visitors): void
/** @var Stmt[] $newStmts */
$newStmts = $this->cloningTraverser->traverse($oldStmts);

$indentTraverser = new NodeTraverser();
$indentDetector = new PhpPrinterIndentationDetectorVisitor(new TokenStream($oldTokens, PhpPrinter::TAB_WIDTH));
$indentTraverser->addVisitor($indentDetector);
$indentTraverser->traverse($oldStmts);

foreach ($visitors as $visitor) {
$traverser = new NodeTraverser();
$traverser->addVisitor($visitor);
Expand All @@ -154,7 +159,8 @@ private function processFile(string $file, array $visitors): void
$newStmts = $traverser->traverse($newStmts);
}

$newCode = $this->printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
$printer = new PhpPrinter(['indent' => str_repeat($indentDetector->indentCharacter, $indentDetector->indentSize)]);
$newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);
$result = file_put_contents($file, $newCode);
if ($result === false) {
throw new Exception(sprintf('%s could not be written', $file));
Expand Down
49 changes: 1 addition & 48 deletions src/Php/PhpPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,16 @@
namespace SimpleDowngrader\Php;

use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\PrettyPrinter\Standard;
use function count;
use function rtrim;
use function str_repeat;

class PhpPrinter extends Standard
{

public const TAB_WIDTH = 4;
public const FUNC_ARGS_TRAILING_COMMA_ATTRIBUTE = 'trailing_comma';

private string $indentCharacter = ' ';

private int $indentSize = 4;

protected function resetState(): void
{
parent::resetState();
$this->indentCharacter = ' ';
$this->indentSize = 4;
}

protected function preprocessNodes(array $nodes): void
{
parent::preprocessNodes($nodes);
if ($this->origTokens === null) {
return;
}

$traverser = new NodeTraverser();

$visitor = new PhpPrinterIndentationDetectorVisitor($this->origTokens);
$traverser->addVisitor($visitor);
$traverser->traverse($nodes);

$this->indentCharacter = $visitor->indentCharacter;
$this->indentSize = $visitor->indentSize;
}

protected function setIndentLevel(int $level): void
{
$this->indentLevel = $level;
$this->nl = "\n" . str_repeat($this->indentCharacter, $level);
}

protected function indent(): void
{
$this->indentLevel += $this->indentSize;
$this->nl = "\n" . str_repeat($this->indentCharacter, $this->indentLevel);
}

protected function outdent(): void
{
$this->indentLevel -= $this->indentSize;
$this->nl = "\n" . str_repeat($this->indentCharacter, $this->indentLevel);
}

/**
* @param Node[] $nodes
*/
Expand Down
10 changes: 9 additions & 1 deletion tests/Visitor/AbstractVisitorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace SimpleDowngrader\Visitor;

use PhpParser\Internal\TokenStream;
use PhpParser\Lexer\Emulative;
use PhpParser\Node\Stmt;
use PhpParser\NodeTraverser;
Expand All @@ -16,7 +17,9 @@
use PHPStan\PhpDocParser\Printer\Printer;
use PHPUnit\Framework\TestCase;
use SimpleDowngrader\Php\PhpPrinter;
use SimpleDowngrader\Php\PhpPrinterIndentationDetectorVisitor;
use SimpleDowngrader\PhpDoc\PhpDocEditor;
use function str_repeat;

abstract class AbstractVisitorTestCase extends TestCase
{
Expand All @@ -38,6 +41,11 @@ public function testVisitor(string $codeBefore, string $codeAfter): void
$oldStmts = $parser->parse($codeBefore);
$oldTokens = $parser->getTokens();

$indentTraverser = new NodeTraverser();
$indentDetector = new PhpPrinterIndentationDetectorVisitor(new TokenStream($oldTokens, PhpPrinter::TAB_WIDTH));
$indentTraverser->addVisitor($indentDetector);
$indentTraverser->traverse($oldStmts);

$cloningTraverser = new NodeTraverser();
$cloningTraverser->addVisitor(new CloningVisitor());

Expand All @@ -54,7 +62,7 @@ public function testVisitor(string $codeBefore, string $codeAfter): void
/** @var Stmt[] $newStmts */
$newStmts = $traverser->traverse($newStmts);

$printer = new PhpPrinter();
$printer = new PhpPrinter(['indent' => str_repeat($indentDetector->indentCharacter, $indentDetector->indentSize)]);
$newCode = $printer->printFormatPreserving($newStmts, $oldStmts, $oldTokens);

$this->assertSame($codeAfter, $newCode);
Expand Down
Loading