From 8c6c12f09918ae972cf4e64439e05748447b11d7 Mon Sep 17 00:00:00 2001 From: homersimpsons Date: Tue, 9 Jan 2024 14:22:24 +0100 Subject: [PATCH] :bug: Resolve changes after upgrade to php-parser 5.0 Based on https://github.com/nikic/PHP-Parser/blob/master/UPGRADE-5.0.md --- phpunit-tests/StringTest.php | 5 +-- src/FilesRepresenter.php | 7 ++-- ...deVisitor.php => NormalizeNodeVisitor.php} | 33 ++++++++++--------- 3 files changed, 23 insertions(+), 22 deletions(-) rename src/{NodeVisitor.php => NormalizeNodeVisitor.php} (89%) diff --git a/phpunit-tests/StringTest.php b/phpunit-tests/StringTest.php index daa14b9..2b5459a 100644 --- a/phpunit-tests/StringTest.php +++ b/phpunit-tests/StringTest.php @@ -31,14 +31,15 @@ public function testDoubleQuotes(): void public function testDoubleQuotesWithEscapes(): void { + // We use `\\\\(\n)` because `\\(\n)` will be simplified to `\(\n)` by PhpParser as $code = <<<'CODE' assertRepresentation( $code, - var_export("\n\r\n\t\n\v\n\e\n\f\n\\\n\$\n\"\n\377\n\x26\n\u2665", true) . ';', + var_export("\n\r\n\t\n\v\n\e\n\f\n\\\\\n\$\n\"\n\377\n\x26\n\u{2665}", true) . ';', '{}', ); } diff --git a/src/FilesRepresenter.php b/src/FilesRepresenter.php index fd9ceaa..7b122cb 100644 --- a/src/FilesRepresenter.php +++ b/src/FilesRepresenter.php @@ -28,7 +28,7 @@ public function __construct( private Mapping $mapping = new Mapping(), private LoggerInterface $logger = new NullLogger(), ) { - $this->parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7); + $this->parser = (new ParserFactory())->createForNewestSupportedVersion(); $this->prettyPrinter = new Standard(); } @@ -52,9 +52,8 @@ public function represent(string $code): string static fn () => 'AST Before normalization: ' . (new NodeDumper())->dump($ast) )); - $visitor = new NodeVisitor($this->mapping); - $traverser = new NodeTraverser(); - $traverser->addVisitor($visitor); + $visitor = new NormalizeNodeVisitor($this->mapping); + $traverser = new NodeTraverser($visitor); $ast = $traverser->traverse($ast); $this->logger->debug(LazyString::fromCallable( diff --git a/src/NodeVisitor.php b/src/NormalizeNodeVisitor.php similarity index 89% rename from src/NodeVisitor.php rename to src/NormalizeNodeVisitor.php index 575ae0f..437e643 100644 --- a/src/NodeVisitor.php +++ b/src/NormalizeNodeVisitor.php @@ -4,6 +4,7 @@ namespace App; +use PhpParser\Modifiers; use PhpParser\Node; use PhpParser\Node\Expr\Array_; use PhpParser\Node\Expr\BinaryOp\Concat; @@ -15,15 +16,15 @@ use PhpParser\Node\Expr\StaticCall; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; +use PhpParser\Node\InterpolatedStringPart; use PhpParser\Node\Name; -use PhpParser\Node\Scalar\Encapsed; -use PhpParser\Node\Scalar\EncapsedStringPart; +use PhpParser\Node\Scalar\InterpolatedString; use PhpParser\Node\Scalar\String_; use PhpParser\Node\Stmt\Class_; use PhpParser\Node\Stmt\ClassMethod; use PhpParser\Node\Stmt\Function_; use PhpParser\Node\Stmt\InlineHTML; -use PhpParser\NodeTraverser; +use PhpParser\NodeVisitor; use PhpParser\NodeVisitorAbstract; use function array_map; @@ -34,7 +35,7 @@ /** * Apply transformations to normalize the AST */ -class NodeVisitor extends NodeVisitorAbstract +class NormalizeNodeVisitor extends NodeVisitorAbstract { public function __construct(private Mapping $mapping) { @@ -99,12 +100,12 @@ private function replaceNewClassName(New_ $class): void private function replaceMethodName(ClassMethod $node): void { // TRANSFORM: Declare everything public - $node->flags |= Class_::MODIFIER_PUBLIC; - $node->flags &= ~Class_::MODIFIER_PRIVATE; - $node->flags &= ~Class_::MODIFIER_PROTECTED; + $node->flags |= Modifiers::PUBLIC; + $node->flags &= ~Modifiers::PRIVATE; + $node->flags &= ~Modifiers::PROTECTED; // TRANSFORM: Remove final and readonly modifiers - $node->flags &= ~Class_::MODIFIER_FINAL; - $node->flags &= ~Class_::MODIFIER_READONLY; + $node->flags &= ~Modifiers::FINAL; + $node->flags &= ~Modifiers::READONLY; $node->name = new Identifier($this->mapping->addMethod($node->name->toString())); } @@ -147,17 +148,17 @@ private function normalizeString(String_ $string): void /** * TRANSFORM: All encapsed strings are single quotes concatenation */ - private function normalizeEncapsedString(Encapsed $string): Node + private function normalizeInterpolatedString(InterpolatedString $string): Node { $parts = array_map( - static fn (Node $part) => $part instanceof EncapsedStringPart + static fn (Node $part) => $part instanceof InterpolatedStringPart ? new String_($part->value, ['kind' => String_::KIND_SINGLE_QUOTED]) : $part, $string->parts, ); $left = array_shift($parts); - assert($left !== null, 'Encapsed string had 0 part.'); + assert($left !== null, 'Interpolated string had 0 part.'); while ($right = array_shift($parts)) { $left = new Concat($left, $right); } @@ -224,8 +225,8 @@ public function enterNode(Node $node) $this->replaceNewClassName($node); } elseif ($node instanceof String_) { $this->normalizeString($node); - } elseif ($node instanceof Encapsed) { - return $this->normalizeEncapsedString($node); + } elseif ($node instanceof InterpolatedString) { + return $this->normalizeInterpolatedString($node); } elseif ($node instanceof Array_) { $this->normalizeArray($node); } elseif ($node instanceof Exit_) { @@ -254,12 +255,12 @@ public function leaveNode(Node $node) // TRANSFORM: Remove empty statements from representation if ($node instanceof Node\Stmt\Nop) { - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } // TRANSFORM: Remove inline HTML from representation if ($node instanceof InlineHTML) { - return NodeTraverser::REMOVE_NODE; + return NodeVisitor::REMOVE_NODE; } return null;