Skip to content

Commit aea9739

Browse files
committed
add support for phpstan/phpdoc-parser 2
1 parent 0c70d2c commit aea9739

File tree

4 files changed

+36
-16
lines changed

4 files changed

+36
-16
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"webmozart/assert": "^1.9.1",
2020
"phpdocumentor/reflection-common": "^2.2",
2121
"ext-filter": "*",
22-
"phpstan/phpdoc-parser": "^1.7",
22+
"phpstan/phpdoc-parser": "^1.7|^2.0",
2323
"doctrine/deprecations": "^1.1"
2424
},
2525
"require-dev": {

composer.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php

+22-10
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PHPStan\PhpDocParser\Parser\PhpDocParser;
2222
use PHPStan\PhpDocParser\Parser\TokenIterator;
2323
use PHPStan\PhpDocParser\Parser\TypeParser;
24+
use PHPStan\PhpDocParser\ParserConfig;
2425
use RuntimeException;
2526

2627
use function ltrim;
@@ -44,16 +45,27 @@ class AbstractPHPStanFactory implements Factory
4445

4546
public function __construct(PHPStanFactory ...$factories)
4647
{
47-
$this->lexer = new Lexer(true);
48-
$constParser = new ConstExprParser(true, true, ['lines' => true, 'indexes' => true]);
49-
$this->parser = new PhpDocParser(
50-
new TypeParser($constParser, true, ['lines' => true, 'indexes' => true]),
51-
$constParser,
52-
true,
53-
true,
54-
['lines' => true, 'indexes' => true],
55-
true
56-
);
48+
if (class_exists(ParserConfig::class)) {
49+
$config = new ParserConfig(['indexes' => true, 'lines' => true]);
50+
$this->lexer = new Lexer($config);
51+
$constParser = new ConstExprParser($config);
52+
$this->parser = new PhpDocParser(
53+
$config,
54+
new TypeParser($config, $constParser),
55+
$constParser
56+
);
57+
} else {
58+
$this->lexer = new Lexer(true);
59+
$constParser = new ConstExprParser(true, true, ['lines' => true, 'indexes' => true]);
60+
$this->parser = new PhpDocParser(
61+
new TypeParser($constParser, true, ['lines' => true, 'indexes' => true]),
62+
$constParser,
63+
true,
64+
true,
65+
['lines' => true, 'indexes' => true],
66+
true
67+
);
68+
}
5769
$this->factories = $factories;
5870
}
5971

tests/unit/DocBlock/Tags/Factory/TagFactoryTestCase.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use PHPStan\PhpDocParser\Parser\PhpDocParser;
2525
use PHPStan\PhpDocParser\Parser\TokenIterator;
2626
use PHPStan\PhpDocParser\Parser\TypeParser;
27+
use PHPStan\PhpDocParser\ParserConfig;
2728
use PHPUnit\Framework\TestCase;
2829

2930
use function property_exists;
@@ -32,11 +33,18 @@ abstract class TagFactoryTestCase extends TestCase
3233
{
3334
public function parseTag(string $tag): PhpDocTagNode
3435
{
35-
$lexer = new Lexer();
36-
$tokens = $lexer->tokenize($tag);
37-
$constParser = new ConstExprParser();
36+
if (class_exists(ParserConfig::class)) {
37+
$config = new ParserConfig([]);
38+
$lexer = new Lexer($config);
39+
$constParser = new ConstExprParser($config);
40+
$phpDocParser = new PhpDocParser($config, new TypeParser($config, $constParser), $constParser);
41+
} else {
42+
$lexer = new Lexer();
43+
$constParser = new ConstExprParser();
44+
$phpDocParser = new PhpDocParser(new TypeParser($constParser), $constParser);
45+
}
3846

39-
$tagNode = (new PhpDocParser(new TypeParser($constParser), $constParser))->parseTag(new TokenIterator($tokens));
47+
$tagNode = ($phpDocParser)->parseTag(new TokenIterator($lexer->tokenize($tag)));
4048
if (property_exists($tagNode->value, 'description') === true) {
4149
$tagNode->value->setAttribute('description', $tagNode->value->description);
4250
}

0 commit comments

Comments
 (0)