Skip to content

Commit edccd8f

Browse files
committed
Add nikic/php-parser v5 support
1 parent 0c5e4d2 commit edccd8f

File tree

3 files changed

+37
-11
lines changed

3 files changed

+37
-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": "^2.7",
3131
"doctrine/persistence": "^1.1|^2.0|^3.0",
32-
"nikic/php-parser": "^4.10",
32+
"nikic/php-parser": "^4.10|^5.0",
3333
"symfony/config": "^6.4|^7.1",
3434
"symfony/console": "^6.4|^7.1",
3535
"symfony/dependency-injection": "^6.4|^7.1",

phpstan-baseline.neon

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,14 @@ parameters:
1515
- # arrays in Doctrine\ORM\Mapping\ClassMetadataInfo -> AssociationMapping not defined
1616
message: '#Method Ecommit\\DoctrineEntitiesGeneratorBundle\\EntityGenerator\\EntityGenerator::addAssociation.*\(\) has parameter \$associationMapping with no value type specified in iterable type array#'
1717
path: src/EntityGenerator/EntityGenerator.php
18+
19+
# Legacy Support for nikic/php-parser v4
20+
-
21+
message: '#Call to an undefined method PhpParser\\Lexer\\Emulative::getTokens\(\)#'
22+
path: src/EntityGenerator/Util/UseStatementManipulator.php
23+
-
24+
message: '#Parameter \#1 \$phpVersion of class PhpParser\\Lexer\\Emulative constructor expects PhpParser\\PhpVersion\|null, array\<string, array\<int, string\>\> given#'
25+
path: src/EntityGenerator/Util/UseStatementManipulator.php
26+
-
27+
message: '#Parameter \#3 \$origTokens of method PhpParser\\PrettyPrinterAbstract::printFormatPreserving\(\) expects array\<PhpParser\\Token\>, array given#'
28+
path: src/EntityGenerator/Util/UseStatementManipulator.php

src/EntityGenerator/Util/UseStatementManipulator.php

Lines changed: 25 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
/**
@@ -30,7 +31,7 @@
3031
class UseStatementManipulator
3132
{
3233
/**
33-
* @var Parser\Php7
34+
* @var Parser
3435
*/
3536
protected $parser;
3637

@@ -66,14 +67,21 @@ class UseStatementManipulator
6667

6768
public function __construct(string $sourceCode)
6869
{
69-
$this->lexer = new Lexer\Emulative([
70-
'usedAttributes' => [
71-
'comments',
72-
'startLine', 'endLine',
73-
'startTokenPos', 'endTokenPos',
74-
],
75-
]);
76-
$this->parser = new Parser\Php7($this->lexer);
70+
/* @legacy Support for nikic/php-parser v4 */
71+
if (class_exists(PhpVersion::class)) {
72+
$version = PhpVersion::fromString(\PHP_VERSION);
73+
$this->lexer = new Lexer\Emulative($version);
74+
$this->parser = new Parser\Php8($this->lexer, $version);
75+
} else {
76+
$this->lexer = new Lexer\Emulative([
77+
'usedAttributes' => [
78+
'comments',
79+
'startLine', 'endLine',
80+
'startTokenPos', 'endTokenPos',
81+
],
82+
]);
83+
$this->parser = new Parser\Php7($this->lexer);
84+
}
7785
$this->printer = new Standard();
7886

7987
$this->setSourceCode($sourceCode);
@@ -88,7 +96,14 @@ protected function setSourceCode(string $sourceCode): void
8896
{
8997
$this->sourceCode = $sourceCode;
9098
$this->oldStmts = $this->parser->parse($sourceCode);
91-
$this->oldTokens = $this->lexer->getTokens();
99+
100+
/* @legacy Support for nikic/php-parser v4 */
101+
if (\is_callable([$this->parser, 'getTokens'])) {
102+
$this->oldTokens = $this->parser->getTokens();
103+
} elseif (\is_callable($this->lexer->getTokens(...))) {
104+
$this->oldTokens = $this->lexer->getTokens();
105+
}
106+
92107
if (null === $this->oldStmts) {
93108
return;
94109
}

0 commit comments

Comments
 (0)