From 1e5928632e4fb1b41cc5eca415c471a12d0b907f Mon Sep 17 00:00:00 2001 From: Emil Masiakowski Date: Thu, 2 Jan 2025 14:49:58 +0100 Subject: [PATCH] Add compatibility with phpstan/phpdoc-parser v2 --- composer.json | 2 +- src/Analyzer/NameResolver.php | 20 ++++++++++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/composer.json b/composer.json index 600433b4..c421b50c 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "nikic/php-parser": "~4", "webmozart/assert": "^1.9", "ext-json": "*", - "phpstan/phpdoc-parser": "^1.2", + "phpstan/phpdoc-parser": "^1.2|^2.0", "ondram/ci-detector": "^4.1" }, "require-dev": { diff --git a/src/Analyzer/NameResolver.php b/src/Analyzer/NameResolver.php index 47e5a49d..1d99787a 100644 --- a/src/Analyzer/NameResolver.php +++ b/src/Analyzer/NameResolver.php @@ -23,6 +23,7 @@ use PHPStan\PhpDocParser\Parser\PhpDocParser; use PHPStan\PhpDocParser\Parser\TokenIterator; use PHPStan\PhpDocParser\Parser\TypeParser; +use PHPStan\PhpDocParser\ParserConfig; class NameResolver extends NodeVisitorAbstract { @@ -55,6 +56,9 @@ class NameResolver extends NodeVisitorAbstract * namespacedName attribute, as usual.) * * parseCustomAnnotations (default true): Whether to parse DocBlock Custom Annotations. * + * @psalm-suppress TooFewArguments + * @psalm-suppress InvalidArgument + * * @param ErrorHandler|null $errorHandler Error handler * @param array $options Options */ @@ -65,10 +69,18 @@ public function __construct(?ErrorHandler $errorHandler = null, array $options = $this->replaceNodes = $options['replaceNodes'] ?? true; $this->parseCustomAnnotations = $options['parseCustomAnnotations'] ?? true; - $typeParser = new TypeParser(); - $constExprParser = new ConstExprParser(); - $this->phpDocParser = new PhpDocParser($typeParser, $constExprParser); - $this->phpDocLexer = new Lexer(); + if (class_exists(ParserConfig::class)) { + $parserConfig = new ParserConfig([]); + $constExprParser = new ConstExprParser($parserConfig); + $typeParser = new TypeParser($parserConfig, $constExprParser); + $this->phpDocParser = new PhpDocParser($parserConfig, $typeParser, $constExprParser); + $this->phpDocLexer = new Lexer($parserConfig); + } else { + $typeParser = new TypeParser(); + $constExprParser = new ConstExprParser(); + $this->phpDocParser = new PhpDocParser($typeParser, $constExprParser); + $this->phpDocLexer = new Lexer(); + } } /**