Skip to content

Add support for phpstan/phpdoc-parser v2 #388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"webmozart/assert": "^1.9.1",
"phpdocumentor/reflection-common": "^2.2",
"ext-filter": "*",
"phpstan/phpdoc-parser": "^1.7",
"phpstan/phpdoc-parser": "^1.7 || ^2.0",
"doctrine/deprecations": "^1.1"
},
"require-dev": {
Expand Down
44 changes: 22 additions & 22 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 50 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,55 @@ parameters:
ignoreErrors:
- '#Method phpDocumentor\\Reflection\\DocBlock\\StandardTagFactory::createTag\(\) should return phpDocumentor\\Reflection\\DocBlock\\Tag but returns mixed#'
- '#Offset 2 on array\{string, 28, int\} on left side of \?\? always exists and is not nullable\.#'
-
message: "#^Class PHPStan\\\\PhpDocParser\\\\Parser\\\\ConstExprParser constructor invoked with 3 parameters, 1 required\\.$#"
count: 1
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php

-
message: "#^Class PHPStan\\\\PhpDocParser\\\\Parser\\\\PhpDocParser constructor invoked with 6 parameters, 3 required\\.$#"
count: 1
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php

-
message: "#^Class PHPStan\\\\PhpDocParser\\\\Parser\\\\TypeParser constructor invoked with 3 parameters, 2 required\\.$#"
count: 1
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php

-
message: "#^Parameter \\#1 \\$config of class PHPStan\\\\PhpDocParser\\\\Lexer\\\\Lexer constructor expects PHPStan\\\\PhpDocParser\\\\ParserConfig, true given\\.$#"
count: 1
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php

-
message: "#^Parameter \\#1 \\$config of class PHPStan\\\\PhpDocParser\\\\Parser\\\\ConstExprParser constructor expects PHPStan\\\\PhpDocParser\\\\ParserConfig, true given\\.$#"
count: 1
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php

-
message: "#^Parameter \\#1 \\$config of class PHPStan\\\\PhpDocParser\\\\Parser\\\\PhpDocParser constructor expects PHPStan\\\\PhpDocParser\\\\ParserConfig, PHPStan\\\\PhpDocParser\\\\Parser\\\\TypeParser given\\.$#"
count: 1
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php

-
message: "#^Parameter \\#1 \\$config of class PHPStan\\\\PhpDocParser\\\\Parser\\\\TypeParser constructor expects PHPStan\\\\PhpDocParser\\\\ParserConfig, PHPStan\\\\PhpDocParser\\\\Parser\\\\ConstExprParser given\\.$#"
count: 1
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php

-
message: "#^Parameter \\#2 \\$constExprParser of class PHPStan\\\\PhpDocParser\\\\Parser\\\\TypeParser constructor expects PHPStan\\\\PhpDocParser\\\\Parser\\\\ConstExprParser, true given\\.$#"
count: 1
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php

-
message: "#^Parameter \\#2 \\$typeParser of class PHPStan\\\\PhpDocParser\\\\Parser\\\\PhpDocParser constructor expects PHPStan\\\\PhpDocParser\\\\Parser\\\\TypeParser, PHPStan\\\\PhpDocParser\\\\Parser\\\\ConstExprParser given\\.$#"
count: 1
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php

-
message: "#^Parameter \\#3 \\$constantExprParser of class PHPStan\\\\PhpDocParser\\\\Parser\\\\PhpDocParser constructor expects PHPStan\\\\PhpDocParser\\\\Parser\\\\ConstExprParser, true given\\.$#"
count: 1
path: src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php

paths:
- src
21 changes: 21 additions & 0 deletions psalm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,26 @@
<file name="src/Utils.php"/>
</errorLevel>
</ArgumentTypeCoercion>

<InvalidArgument>
<errorLevel type="suppress">
<referencedFunction name="PHPStan\PhpDocParser\Lexer\Lexer::__construct"/>
<referencedFunction name="PHPStan\PhpDocParser\Parser\ConstExprParser::__construct"/>
<referencedFunction name="PHPStan\PhpDocParser\Parser\TypeParser::__construct"/>
<referencedFunction name="PHPStan\PhpDocParser\Parser\PhpDocParser::__construct"/>
</errorLevel>
</InvalidArgument>
<UndefinedDocblockClass>
<errorLevel type="suppress">
<referencedClass name="PHPStan\PhpDocParser\ParserConfig"/>
</errorLevel>
</UndefinedDocblockClass>
<MixedArgument>
<errorLevel type="suppress">
<referencedFunction name="PHPStan\PhpDocParser\Lexer\Lexer::__construct"/>
<referencedFunction name="PHPStan\PhpDocParser\Parser\ConstExprParser::__construct"/>
<referencedFunction name="PHPStan\PhpDocParser\Parser\TypeParser::__construct"/>
</errorLevel>
</MixedArgument>
</issueHandlers>
</psalm>
33 changes: 23 additions & 10 deletions src/DocBlock/Tags/Factory/AbstractPHPStanFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;
use PHPStan\PhpDocParser\ParserConfig;
use RuntimeException;

use function ltrim;
Expand All @@ -44,16 +45,28 @@ class AbstractPHPStanFactory implements Factory

public function __construct(PHPStanFactory ...$factories)
{
$this->lexer = new Lexer(true);
$constParser = new ConstExprParser(true, true, ['lines' => true, 'indexes' => true]);
$this->parser = new PhpDocParser(
new TypeParser($constParser, true, ['lines' => true, 'indexes' => true]),
$constParser,
true,
true,
['lines' => true, 'indexes' => true],
true
);
if (class_exists(ParserConfig::class)) {
$parserConfig = new ParserConfig(['lines' => true, 'indexes' => true]);
$this->lexer = new Lexer($parserConfig);
$constParser = new ConstExprParser($parserConfig);
$this->parser = new PhpDocParser(
$parserConfig,
new TypeParser($parserConfig, $constParser),
$constParser
);
} else {
$this->lexer = new Lexer(true);
$constParser = new ConstExprParser(true, true, ['lines' => true, 'indexes' => true]);
$this->parser = new PhpDocParser(
new TypeParser($constParser, true, ['lines' => true, 'indexes' => true]),
$constParser,
true,
true,
['lines' => true, 'indexes' => true],
true
);
}

$this->factories = $factories;
}

Expand Down
20 changes: 17 additions & 3 deletions tests/unit/DocBlock/Tags/Factory/TagFactoryTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,33 @@
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;
use PHPStan\PhpDocParser\ParserConfig;
use PHPUnit\Framework\TestCase;

use function class_exists;
use function property_exists;

abstract class TagFactoryTestCase extends TestCase
{
public function parseTag(string $tag): PhpDocTagNode
{
$lexer = new Lexer();
if (class_exists(ParserConfig::class)) {
$parserConfig = new ParserConfig([]);
$lexer = new Lexer($parserConfig);
$constParser = new ConstExprParser($parserConfig);
$phpDocParser = new PhpDocParser(
$parserConfig,
new TypeParser($parserConfig, $constParser),
$constParser
);
} else {
$lexer = new Lexer();
$constParser = new ConstExprParser();
$phpDocParser = new PhpDocParser(new TypeParser($constParser), $constParser);
}
$tokens = $lexer->tokenize($tag);
$constParser = new ConstExprParser();

$tagNode = (new PhpDocParser(new TypeParser($constParser), $constParser))->parseTag(new TokenIterator($tokens));
$tagNode = $phpDocParser->parseTag(new TokenIterator($tokens));
if (property_exists($tagNode->value, 'description') === true) {
$tagNode->value->setAttribute('description', $tagNode->value->description);
}
Expand Down