Skip to content

Commit 97124aa

Browse files
committed
Add nikic/php-parser v5 support
1 parent 8ab5e6f commit 97124aa

File tree

3 files changed

+42
-11
lines changed

3 files changed

+42
-11
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"doctrine/inflector": "^1.4|^2.0",
3030
"doctrine/orm": "^3.2",
3131
"doctrine/persistence": "^3.1",
32-
"nikic/php-parser": "^4.10",
32+
"nikic/php-parser": "^4.10|^5.0",
3333
"symfony/config": "^6.4|^7.0",
3434
"symfony/console": "^6.4|^7.0",
3535
"symfony/dependency-injection": "^6.4|^7.0",

phpstan-baseline.neon

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,18 @@ parameters:
1212
paths:
1313
- tests/App/Entity/
1414
- tests/App/GeneratedEntity/
15+
16+
17+
# Legacy Support for nikic/php-parser v4
18+
-
19+
message: '#Call to an undefined method PhpParser\\Lexer\\Emulative::getTokens\(\)#'
20+
path: src/EntityGenerator/Util/UseStatementManipulator.php
21+
-
22+
message: '#Call to function is_callable\(\) with array\{PhpParser\\Parser, ''getTokens''\} will always evaluate to true#'
23+
path: src/EntityGenerator/Util/UseStatementManipulator.php
24+
-
25+
message: '#Parameter \#1 \$phpVersion of class PhpParser\\Lexer\\Emulative constructor expects PhpParser\\PhpVersion\|null, array\<string, array\<int, string\>\> given#'
26+
path: src/EntityGenerator/Util/UseStatementManipulator.php
27+
-
28+
message: '#Parameter \#3 \$origTokens of method PhpParser\\PrettyPrinterAbstract::printFormatPreserving\(\) expects array\<PhpParser\\Token\>, array given#'
29+
path: src/EntityGenerator/Util/UseStatementManipulator.php

src/EntityGenerator/Util/UseStatementManipulator.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use PhpParser\NodeTraverser;
2020
use PhpParser\NodeVisitor;
2121
use PhpParser\Parser;
22+
use PhpParser\PhpVersion;
2223
use PhpParser\PrettyPrinter\Standard;
2324

2425
/**
@@ -29,7 +30,7 @@
2930
*/
3031
class UseStatementManipulator
3132
{
32-
protected Parser\Php7 $parser;
33+
protected Parser $parser;
3334
protected Lexer\Emulative $lexer;
3435
protected Standard $printer;
3536

@@ -51,14 +52,22 @@ class UseStatementManipulator
5152

5253
public function __construct(string $sourceCode)
5354
{
54-
$this->lexer = new Lexer\Emulative([
55-
'usedAttributes' => [
56-
'comments',
57-
'startLine', 'endLine',
58-
'startTokenPos', 'endTokenPos',
59-
],
60-
]);
61-
$this->parser = new Parser\Php7($this->lexer);
55+
/* @legacy Support for nikic/php-parser v4 */
56+
if (class_exists(PhpVersion::class)) {
57+
$version = PhpVersion::fromString(\PHP_VERSION);
58+
$this->lexer = new Lexer\Emulative($version);
59+
$this->parser = new Parser\Php8($this->lexer, $version);
60+
} else {
61+
$this->lexer = new Lexer\Emulative([
62+
'usedAttributes' => [
63+
'comments',
64+
'startLine', 'endLine',
65+
'startTokenPos', 'endTokenPos',
66+
],
67+
]);
68+
$this->parser = new Parser\Php7($this->lexer);
69+
}
70+
6271
$this->printer = new Standard();
6372

6473
$this->setSourceCode($sourceCode);
@@ -73,7 +82,14 @@ protected function setSourceCode(string $sourceCode): void
7382
{
7483
$this->sourceCode = $sourceCode;
7584
$this->oldStmts = $this->parser->parse($sourceCode);
76-
$this->oldTokens = $this->lexer->getTokens();
85+
86+
/* @legacy Support for nikic/php-parser v4 */
87+
if (\is_callable([$this->parser, 'getTokens'])) {
88+
$this->oldTokens = $this->parser->getTokens();
89+
} elseif (\is_callable($this->lexer->getTokens(...))) {
90+
$this->oldTokens = $this->lexer->getTokens();
91+
}
92+
7793
if (null === $this->oldStmts) {
7894
return;
7995
}

0 commit comments

Comments
 (0)