Skip to content

Commit 790945d

Browse files
committed
Re-add compatiblity for php parser v4
1 parent 43f884d commit 790945d

File tree

3 files changed

+31
-6
lines changed

3 files changed

+31
-6
lines changed

.github/workflows/ci.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,11 @@ jobs:
149149
composer-options: "--no-scripts --working-dir=tools/twigcs"
150150

151151
- name: "Install PHPUnit"
152-
run: vendor/bin/simple-phpunit install
152+
run: |
153+
if [[ ${{ matrix.dependency_versions == 'lowest' }} ]]; then
154+
echo "SYMFONY_PHPUNIT_REQUIRE=nikic/php-parser:^4.18" >> $GITHUB_ENV
155+
fi
156+
vendor/bin/simple-phpunit install
153157
154158
- name: "PHPUnit version"
155159
run: vendor/bin/simple-phpunit --version

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"require": {
1616
"php": ">=8.1",
1717
"doctrine/inflector": "^2.0",
18-
"nikic/php-parser": "^5.0",
18+
"nikic/php-parser": "^4.18|^5.0",
1919
"symfony/config": "^6.3|^7.0",
2020
"symfony/console": "^6.3|^7.0",
2121
"symfony/dependency-injection": "^6.3|^7.0",

src/Util/ClassSourceManipulator.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
use Doctrine\ORM\Mapping\OneToOne;
2424
use PhpParser\Builder;
2525
use PhpParser\BuilderHelpers;
26+
use PhpParser\Lexer;
2627
use PhpParser\Node;
2728
use PhpParser\NodeTraverser;
2829
use PhpParser\NodeVisitor;
2930
use PhpParser\Parser;
30-
use PhpParser\ParserFactory;
31+
use PhpParser\PhpVersion;
3132
use Symfony\Bundle\MakerBundle\ConsoleStyle;
3233
use Symfony\Bundle\MakerBundle\Doctrine\BaseCollectionRelation;
3334
use Symfony\Bundle\MakerBundle\Doctrine\BaseRelation;
@@ -48,7 +49,8 @@ final class ClassSourceManipulator
4849
private const CONTEXT_CLASS_METHOD = 'class_method';
4950
private const DEFAULT_VALUE_NONE = '__default_value_none';
5051

51-
private Parser\Php8 $parser;
52+
private Parser $parser;
53+
private Lexer\Emulative $lexer;
5254
private PrettyPrinter $printer;
5355
private ?ConsoleStyle $io = null;
5456

@@ -63,7 +65,21 @@ public function __construct(
6365
private bool $overwrite = false,
6466
private bool $useAttributesForDoctrineMapping = true,
6567
) {
66-
$this->parser = (new ParserFactory())->createForNewestSupportedVersion();
68+
if (class_exists(PhpVersion::class)) {
69+
$version = PhpVersion::fromString(\PHP_VERSION);
70+
$this->lexer = new Lexer\Emulative($version);
71+
$this->parser = new Parser\Php8($this->lexer, $version);
72+
} else {
73+
$this->lexer = new Lexer\Emulative([
74+
'usedAttributes' => [
75+
'comments',
76+
'startLine', 'endLine',
77+
'startTokenPos', 'endTokenPos',
78+
],
79+
]);
80+
$this->parser = new Parser\Php7($this->lexer);
81+
}
82+
6783
$this->printer = new PrettyPrinter();
6884

6985
$this->setSourceCode($sourceCode);
@@ -892,7 +908,12 @@ private function setSourceCode(string $sourceCode): void
892908
{
893909
$this->sourceCode = $sourceCode;
894910
$this->oldStmts = $this->parser->parse($sourceCode);
895-
$this->oldTokens = $this->parser->getTokens();
911+
912+
if (\is_callable([$this->parser, 'getTokens'])) {
913+
$this->oldTokens = $this->parser->getTokens();
914+
} elseif (\is_callable([$this->lexer, 'getTokens'])) {
915+
$this->oldTokens = $this->lexer->getTokens();
916+
}
896917

897918
$traverser = new NodeTraverser();
898919
$traverser->addVisitor(new NodeVisitor\CloningVisitor());

0 commit comments

Comments
 (0)