Skip to content

Commit 5763b5b

Browse files
authored
Merge pull request #386 from xabbuh/phpstan-phpdoc-parser-2
add support for phpstan/phpdoc-parser 2
2 parents 0c70d2c + 2a8b28d commit 5763b5b

File tree

7 files changed

+75
-17
lines changed

7 files changed

+75
-17
lines changed

composer-require-checker.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
"symbol-whitelist" : [
33
"null", "true", "false",
44
"static", "self", "parent",
5-
"array", "string", "int", "float", "bool", "iterable", "callable", "void", "object", "XSLTProcessor"
5+
"array", "string", "int", "float", "bool", "iterable", "callable", "void", "object", "XSLTProcessor",
6+
"PHPStan\\PhpDocParser\\ParserConfig"
67
],
78
"php-core-extensions" : [
89
"Core",

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.

phpstan.neon

+28
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,33 @@ parameters:
33
ignoreErrors:
44
- '#Method phpDocumentor\\Reflection\\DocBlock\\StandardTagFactory::createTag\(\) should return phpDocumentor\\Reflection\\DocBlock\\Tag but returns mixed#'
55
- '#Offset 2 on array\{string, 28, int\} on left side of \?\? always exists and is not nullable\.#'
6+
-
7+
message: "#^Parameter \\#1 \\$constExprParser of class PHPStan\\\\PhpDocParser\\\\Parser\\\\TypeParser constructor expects PHPStan\\\\PhpDocParser\\\\Parser\\\\ConstExprParser\\|null, PHPStan\\\\PhpDocParser\\\\ParserConfig given\\.$#"
8+
count: 1
9+
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php
10+
-
11+
message: "#^Parameter \\#1 \\$parseDoctrineAnnotations of class PHPStan\\\\PhpDocParser\\\\Lexer\\\\Lexer constructor expects bool, PHPStan\\\\PhpDocParser\\\\ParserConfig given\\.$#"
12+
count: 1
13+
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php
14+
-
15+
message: "#^Parameter \\#1 \\$typeParser of class PHPStan\\\\PhpDocParser\\\\Parser\\\\PhpDocParser constructor expects PHPStan\\\\PhpDocParser\\\\Parser\\\\TypeParser, PHPStan\\\\PhpDocParser\\\\ParserConfig given\\.$#"
16+
count: 1
17+
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php
18+
-
19+
message: "#^Parameter \\#1 \\$unescapeStrings of class PHPStan\\\\PhpDocParser\\\\Parser\\\\ConstExprParser constructor expects bool, PHPStan\\\\PhpDocParser\\\\ParserConfig given\\.$#"
20+
count: 1
21+
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php
22+
-
23+
message: "#^Parameter \\#2 \\$constantExprParser of class PHPStan\\\\PhpDocParser\\\\Parser\\\\PhpDocParser constructor expects PHPStan\\\\PhpDocParser\\\\Parser\\\\ConstExprParser, PHPStan\\\\PhpDocParser\\\\Parser\\\\TypeParser given\\.$#"
24+
count: 1
25+
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php
26+
-
27+
message: "#^Parameter \\#2 \\$quoteAwareConstExprString of class PHPStan\\\\PhpDocParser\\\\Parser\\\\TypeParser constructor expects bool, PHPStan\\\\PhpDocParser\\\\Parser\\\\ConstExprParser given\\.$#"
28+
count: 1
29+
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php
30+
-
31+
message: "#^Parameter \\#3 \\$requireWhitespaceBeforeDescription of class PHPStan\\\\PhpDocParser\\\\Parser\\\\PhpDocParser constructor expects bool, PHPStan\\\\PhpDocParser\\\\Parser\\\\ConstExprParser given\\.$#"
32+
count: 1
33+
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php
634
paths:
735
- src

psalm.xml

+7
Original file line numberDiff line numberDiff line change
@@ -68,5 +68,12 @@
6868
<file name="src/Utils.php"/>
6969
</errorLevel>
7070
</ArgumentTypeCoercion>
71+
72+
<InvalidArgument>
73+
<errorLevel type="suppress">
74+
<referencedFunction name="PHPStan\PhpDocParser\Parser\PhpDocParser::__construct"/>
75+
<referencedFunction name="PHPStan\PhpDocParser\Parser\TypeParser::__construct"/>
76+
</errorLevel>
77+
</InvalidArgument>
7178
</issueHandlers>
7279
</psalm>

src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php

+23-10
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
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

27+
use function class_exists;
2628
use function ltrim;
2729
use function property_exists;
2830
use function rtrim;
@@ -44,16 +46,27 @@ class AbstractPHPStanFactory implements Factory
4446

4547
public function __construct(PHPStanFactory ...$factories)
4648
{
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-
);
49+
if (class_exists(ParserConfig::class)) {
50+
$config = new ParserConfig(['indexes' => true, 'lines' => true]);
51+
$this->lexer = new Lexer($config);
52+
$constParser = new ConstExprParser($config);
53+
$this->parser = new PhpDocParser(
54+
$config,
55+
new TypeParser($config, $constParser),
56+
$constParser
57+
);
58+
} else {
59+
$this->lexer = new Lexer(true);
60+
$constParser = new ConstExprParser(true, true, ['lines' => true, 'indexes' => true]);
61+
$this->parser = new PhpDocParser(
62+
new TypeParser($constParser, true, ['lines' => true, 'indexes' => true]),
63+
$constParser,
64+
true,
65+
true,
66+
['lines' => true, 'indexes' => true],
67+
true
68+
);
69+
}
5770
$this->factories = $factories;
5871
}
5972

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

+13-4
Original file line numberDiff line numberDiff line change
@@ -24,19 +24,28 @@
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

30+
use function class_exists;
2931
use function property_exists;
3032

3133
abstract class TagFactoryTestCase extends TestCase
3234
{
3335
public function parseTag(string $tag): PhpDocTagNode
3436
{
35-
$lexer = new Lexer();
36-
$tokens = $lexer->tokenize($tag);
37-
$constParser = new ConstExprParser();
37+
if (class_exists(ParserConfig::class)) {
38+
$config = new ParserConfig([]);
39+
$lexer = new Lexer($config);
40+
$constParser = new ConstExprParser($config);
41+
$phpDocParser = new PhpDocParser($config, new TypeParser($config, $constParser), $constParser);
42+
} else {
43+
$lexer = new Lexer();
44+
$constParser = new ConstExprParser();
45+
$phpDocParser = new PhpDocParser(new TypeParser($constParser), $constParser);
46+
}
3847

39-
$tagNode = (new PhpDocParser(new TypeParser($constParser), $constParser))->parseTag(new TokenIterator($tokens));
48+
$tagNode = ($phpDocParser)->parseTag(new TokenIterator($lexer->tokenize($tag)));
4049
if (property_exists($tagNode->value, 'description') === true) {
4150
$tagNode->value->setAttribute('description', $tagNode->value->description);
4251
}

0 commit comments

Comments
 (0)