Skip to content

Commit

Permalink
fix support for phpstan/phpdoc-parser 2
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Nov 7, 2024
1 parent a130321 commit 51535dd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
11 changes: 9 additions & 2 deletions TypeContext/TypeContextFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;
use PHPStan\PhpDocParser\ParserConfig;
use Symfony\Component\TypeInfo\Exception\RuntimeException;
use Symfony\Component\TypeInfo\Exception\UnsupportedException;
use Symfony\Component\TypeInfo\Type;
Expand Down Expand Up @@ -157,8 +158,14 @@ private function collectTemplates(\ReflectionClass|\ReflectionFunctionAbstract $
return [];
}

$this->phpstanLexer ??= new Lexer();
$this->phpstanParser ??= new PhpDocParser(new TypeParser(new ConstExprParser()), new ConstExprParser());
if (class_exists(ParserConfig::class)) {
$config = new ParserConfig([]);
$this->phpstanLexer ??= new Lexer($config);
$this->phpstanParser ??= new PhpDocParser($config, new TypeParser($config, new ConstExprParser($config)), new ConstExprParser($config));
} else {
$this->phpstanLexer ??= new Lexer();
$this->phpstanParser ??= new PhpDocParser(new TypeParser(new ConstExprParser()), new ConstExprParser());
}

$tokens = new TokenIterator($this->phpstanLexer->tokenize($rawDocNode));

Expand Down
11 changes: 9 additions & 2 deletions TypeResolver/StringTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use PHPStan\PhpDocParser\Parser\ConstExprParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;
use PHPStan\PhpDocParser\ParserConfig;
use Symfony\Component\TypeInfo\Exception\InvalidArgumentException;
use Symfony\Component\TypeInfo\Exception\UnsupportedException;
use Symfony\Component\TypeInfo\Type;
Expand Down Expand Up @@ -63,8 +64,14 @@ final class StringTypeResolver implements TypeResolverInterface

public function __construct()
{
$this->lexer = new Lexer();
$this->parser = new TypeParser(new ConstExprParser());
if (class_exists(ParserConfig::class)) {
$config = new ParserConfig([]);
$this->lexer = new Lexer($config);
$this->parser = new TypeParser($config, new ConstExprParser($config));
} else {
$this->lexer = new Lexer();
$this->parser = new TypeParser(new ConstExprParser());
}
}

public function resolve(mixed $subject, ?TypeContext $typeContext = null): Type
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"psr/container": "^1.1|^2.0"
},
"require-dev": {
"phpstan/phpdoc-parser": "^1.0",
"phpstan/phpdoc-parser": "^1.0|^2.0",
"symfony/dependency-injection": "^6.4|^7.0",
"symfony/property-info": "^6.4|^7.0"
},
Expand Down

0 comments on commit 51535dd

Please sign in to comment.