Skip to content

Commit d1f6e7f

Browse files
allow using phpdoc-parser v1.2
1 parent 92bfae0 commit d1f6e7f

File tree

2 files changed

+20
-15
lines changed

2 files changed

+20
-15
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
"nikic/php-parser": "~5",
3232
"webmozart/assert": "^1.9",
3333
"ext-json": "*",
34-
"phpstan/phpdoc-parser": "^2.0",
34+
"phpstan/phpdoc-parser": "^1.2|^2.0",
3535
"ondram/ci-detector": "^4.1"
3636
},
3737
"require-dev": {

src/Analyzer/NameResolver.php

+19-14
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,17 @@
2727

2828
class NameResolver extends NodeVisitorAbstract
2929
{
30-
/** @var NameContext Naming context */
3130
protected NameContext $nameContext;
3231

33-
/** @var bool Whether to preserve original names */
3432
protected bool $preserveOriginalNames;
3533

36-
/** @var bool Whether to replace resolved nodes in place, or to add resolvedNode attributes */
3734
protected bool $replaceNodes;
3835

39-
/** @var bool Whether to parse DocBlock Custom Annotations */
40-
protected $parseCustomAnnotations;
36+
protected bool $parseCustomAnnotations;
4137

42-
/** @var PhpDocParser */
43-
protected $phpDocParser;
38+
protected PhpDocParser $phpDocParser;
4439

45-
/** @var Lexer */
46-
protected $phpDocLexer;
40+
protected Lexer $phpDocLexer;
4741

4842
/**
4943
* Constructs a name resolution visitor.
@@ -58,6 +52,9 @@ class NameResolver extends NodeVisitorAbstract
5852
*
5953
* @param ErrorHandler|null $errorHandler Error handler
6054
* @param array{preserveOriginalNames?: bool, replaceNodes?: bool, parseCustomAnnotations?: bool} $options Options
55+
*
56+
* @psalm-suppress TooFewArguments
57+
* @psalm-suppress InvalidArgument
6158
*/
6259
public function __construct(?ErrorHandler $errorHandler = null, array $options = [])
6360
{
@@ -66,11 +63,19 @@ public function __construct(?ErrorHandler $errorHandler = null, array $options =
6663
$this->replaceNodes = $options['replaceNodes'] ?? true;
6764
$this->parseCustomAnnotations = $options['parseCustomAnnotations'] ?? true;
6865

69-
$parserConfig = new ParserConfig([]);
70-
$constExprParser = new ConstExprParser($parserConfig);
71-
$typeParser = new TypeParser($parserConfig, $constExprParser);
72-
$this->phpDocParser = new PhpDocParser($parserConfig, $typeParser, $constExprParser);
73-
$this->phpDocLexer = new Lexer($parserConfig);
66+
// this if is to allow using v 1.2 or v2
67+
if (class_exists(ParserConfig::class)) {
68+
$parserConfig = new ParserConfig([]);
69+
$constExprParser = new ConstExprParser($parserConfig);
70+
$typeParser = new TypeParser($parserConfig, $constExprParser);
71+
$this->phpDocParser = new PhpDocParser($parserConfig, $typeParser, $constExprParser);
72+
$this->phpDocLexer = new Lexer($parserConfig);
73+
} else {
74+
$typeParser = new TypeParser();
75+
$constExprParser = new ConstExprParser();
76+
$this->phpDocParser = new PhpDocParser($typeParser, $constExprParser);
77+
$this->phpDocLexer = new Lexer();
78+
}
7479
}
7580

7681
/**

0 commit comments

Comments
 (0)