From 3bffbde0d4dd29150dfa79cbe0d5dc115e2342da Mon Sep 17 00:00:00 2001 From: Michele Orselli Date: Fri, 14 Mar 2025 22:06:01 +0100 Subject: [PATCH] allow using phpdoc-parser v1.2 --- composer.json | 2 +- src/Analyzer/NameResolver.php | 33 +++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/composer.json b/composer.json index f9d997a7..25329096 100644 --- a/composer.json +++ b/composer.json @@ -31,7 +31,7 @@ "nikic/php-parser": "~5", "webmozart/assert": "^1.9", "ext-json": "*", - "phpstan/phpdoc-parser": "^2.0", + "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 8bb306c8..642c773d 100644 --- a/src/Analyzer/NameResolver.php +++ b/src/Analyzer/NameResolver.php @@ -27,23 +27,17 @@ class NameResolver extends NodeVisitorAbstract { - /** @var NameContext Naming context */ protected NameContext $nameContext; - /** @var bool Whether to preserve original names */ protected bool $preserveOriginalNames; - /** @var bool Whether to replace resolved nodes in place, or to add resolvedNode attributes */ protected bool $replaceNodes; - /** @var bool Whether to parse DocBlock Custom Annotations */ - protected $parseCustomAnnotations; + protected bool $parseCustomAnnotations; - /** @var PhpDocParser */ - protected $phpDocParser; + protected PhpDocParser $phpDocParser; - /** @var Lexer */ - protected $phpDocLexer; + protected Lexer $phpDocLexer; /** * Constructs a name resolution visitor. @@ -58,6 +52,9 @@ class NameResolver extends NodeVisitorAbstract * * @param ErrorHandler|null $errorHandler Error handler * @param array{preserveOriginalNames?: bool, replaceNodes?: bool, parseCustomAnnotations?: bool} $options Options + * + * @psalm-suppress TooFewArguments + * @psalm-suppress InvalidArgument */ public function __construct(?ErrorHandler $errorHandler = null, array $options = []) { @@ -66,11 +63,19 @@ public function __construct(?ErrorHandler $errorHandler = null, array $options = $this->replaceNodes = $options['replaceNodes'] ?? true; $this->parseCustomAnnotations = $options['parseCustomAnnotations'] ?? true; - $parserConfig = new ParserConfig([]); - $constExprParser = new ConstExprParser($parserConfig); - $typeParser = new TypeParser($parserConfig, $constExprParser); - $this->phpDocParser = new PhpDocParser($parserConfig, $typeParser, $constExprParser); - $this->phpDocLexer = new Lexer($parserConfig); + // this if is to allow using v 1.2 or v2 + 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(); + } } /**