Skip to content

Commit

Permalink
Re-add compatiblity for dependency php parser v4
Browse files Browse the repository at this point in the history
  • Loading branch information
drieschel committed Jan 10, 2024
1 parent 43f884d commit 3762524
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
6 changes: 5 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ jobs:
composer-options: "--no-scripts --working-dir=tools/twigcs"

- name: "Install PHPUnit"
run: vendor/bin/simple-phpunit install
run: |
if [[ ${{ matrix.dependency_versions == 'lowest' }} ]]; then
echo "SYMFONY_PHPUNIT_REQUIRE=nikic/php-parser:^4.18" >> $GITHUB_ENV
fi
vendor/bin/simple-phpunit install
- name: "PHPUnit version"
run: vendor/bin/simple-phpunit --version
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"require": {
"php": ">=8.1",
"doctrine/inflector": "^2.0",
"nikic/php-parser": "^5.0",
"nikic/php-parser": "^4.18|^5.0",
"symfony/config": "^6.3|^7.0",
"symfony/console": "^6.3|^7.0",
"symfony/dependency-injection": "^6.3|^7.0",
Expand Down
29 changes: 25 additions & 4 deletions src/Util/ClassSourceManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@
use Doctrine\ORM\Mapping\OneToOne;
use PhpParser\Builder;
use PhpParser\BuilderHelpers;
use PhpParser\Lexer;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitor;
use PhpParser\Parser;
use PhpParser\ParserFactory;
use PhpParser\PhpVersion;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\Doctrine\BaseCollectionRelation;
use Symfony\Bundle\MakerBundle\Doctrine\BaseRelation;
Expand All @@ -48,7 +49,8 @@ final class ClassSourceManipulator
private const CONTEXT_CLASS_METHOD = 'class_method';
private const DEFAULT_VALUE_NONE = '__default_value_none';

private Parser\Php8 $parser;
private Parser $parser;
private Lexer\Emulative $lexer;
private PrettyPrinter $printer;
private ?ConsoleStyle $io = null;

Expand All @@ -63,7 +65,21 @@ public function __construct(
private bool $overwrite = false,
private bool $useAttributesForDoctrineMapping = true,
) {
$this->parser = (new ParserFactory())->createForNewestSupportedVersion();
if (class_exists(PhpVersion::class)) {
$version = PhpVersion::fromString(\PHP_VERSION);
$this->lexer = new Lexer\Emulative($version);
$this->parser = new Parser\Php8($this->lexer, $version);
} else {
$this->lexer = new Lexer\Emulative([
'usedAttributes' => [
'comments',
'startLine', 'endLine',
'startTokenPos', 'endTokenPos',
],
]);
$this->parser = new Parser\Php7($this->lexer);
}

$this->printer = new PrettyPrinter();

$this->setSourceCode($sourceCode);
Expand Down Expand Up @@ -892,7 +908,12 @@ private function setSourceCode(string $sourceCode): void
{
$this->sourceCode = $sourceCode;
$this->oldStmts = $this->parser->parse($sourceCode);
$this->oldTokens = $this->parser->getTokens();

if (\is_callable([$this->parser, 'getTokens'])) {
$this->oldTokens = $this->parser->getTokens();
} elseif (\is_callable([$this->lexer, 'getTokens'])) {
$this->oldTokens = $this->lexer->getTokens();
}

$traverser = new NodeTraverser();
$traverser->addVisitor(new NodeVisitor\CloningVisitor());
Expand Down

0 comments on commit 3762524

Please sign in to comment.