Skip to content

Commit

Permalink
🐛 Resolve changes after upgrade to php-parser 5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
homersimpsons committed Jan 9, 2024
1 parent 7cf7c21 commit 8c6c12f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 22 deletions.
5 changes: 3 additions & 2 deletions phpunit-tests/StringTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
<?php
"\n\r\n\t\n\v\n\e\n\f\n\\\n\$\n\"\n\377\n\x26\n\u2665";
"\n\r\n\t\n\v\n\e\n\f\n\\\\\n\$\n\"\n\377\n\x26\n\u{2665}";
CODE;

$this->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) . ';',
'{}',
);
}
Expand Down
7 changes: 3 additions & 4 deletions src/FilesRepresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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(
Expand Down
33 changes: 17 additions & 16 deletions src/NodeVisitor.php → src/NormalizeNodeVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App;

use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\BinaryOp\Concat;
Expand All @@ -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;
Expand All @@ -34,7 +35,7 @@
/**
* Apply transformations to normalize the AST
*/
class NodeVisitor extends NodeVisitorAbstract
class NormalizeNodeVisitor extends NodeVisitorAbstract
{
public function __construct(private Mapping $mapping)
{
Expand Down Expand Up @@ -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()));
}
Expand Down Expand Up @@ -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])

Check warning on line 155 in src/NormalizeNodeVisitor.php

View workflow job for this annotation

GitHub Actions / continuous-integration

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ */ private function normalizeInterpolatedString(InterpolatedString $string) : Node { - $parts = array_map(static fn(Node $part) => $part instanceof InterpolatedStringPart ? new String_($part->value, ['kind' => String_::KIND_SINGLE_QUOTED]) : $part, $string->parts); + $parts = array_map(static fn(Node $part) => $part instanceof InterpolatedStringPart ? new String_($part->value, []) : $part, $string->parts); $left = array_shift($parts); assert($left !== null, 'Interpolated string had 0 part.'); while ($right = array_shift($parts)) {
: $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);
}
Expand Down Expand Up @@ -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_) {
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 8c6c12f

Please sign in to comment.