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

⬆️ Bump nikic/php-parser from 4.18.0 to 5.0.0 #129

Merged
merged 4 commits into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"php": "^8.1",
"league/flysystem": "^3.0",
"league/flysystem-memory": "^3.0",
"nikic/php-parser": "^4.15",
"nikic/php-parser": "^5.0",
"psr/log": "^3.0",
"symfony/console": "^6.2"
},
Expand Down
22 changes: 12 additions & 10 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
$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
2 changes: 1 addition & 1 deletion src/DirectoryRepresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function represent(): Result

return new Result(
$filesRepresentation,
'{"version":1}',
'{"version":2}',
$mapping->toJson(),
);
}
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 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 @@
/**
* 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 All @@ -173,7 +174,7 @@
if ($concat->left instanceof String_ && $concat->right instanceof String_) {
return new String_(
$concat->left->value . $concat->right->value,
['kind' => String_::KIND_SINGLE_QUOTED],

Check warning on line 177 in src/NormalizeNodeVisitor.php

View workflow job for this annotation

GitHub Actions / continuous-integration

Escaped Mutant for Mutator "ArrayItemRemoval": --- Original +++ New @@ @@ private function simplifyUselessConcat(Concat $concat) : String_|null { if ($concat->left instanceof String_ && $concat->right instanceof String_) { - return new String_($concat->left->value . $concat->right->value, ['kind' => String_::KIND_SINGLE_QUOTED]); + return new String_($concat->left->value . $concat->right->value, []); } return null; }
);
}

Expand Down Expand Up @@ -224,8 +225,8 @@
$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 @@

// 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
Loading