Skip to content

Commit 42e9ec6

Browse files
authored
Add support for phpstan/phpdoc-parser 2 (#205)
I noticed locally that random test started to fail, turns out it's because [phpdocumentor/type-resolver added support for phpstan/phpdoc-parser v2](https://github.com/phpDocumentor/TypeResolver/releases/tag/1.10.0), but this library not ready for phpdoc-parser v2. I chose minimal version of 1.13 because minimal version of [phpdocumentor/type-resolver required it](https://github.com/phpDocumentor/TypeResolver/blob/1.7.0/composer.json#L15) so there should be no changes to `composer update --prefer-lowest`
2 parents 0dbc341 + f3b7f44 commit 42e9ec6

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1313

1414
### Changed
1515
- [GH#186](https://github.com/jolicode/automapper/pull/186) Optimize creation from constructor
16+
- [GH#205](https://github.com/jolicode/automapper/pull/205) Add support for phpstan/phpdoc-parser 2
1617

1718
### Fixed
1819
- [GH#184](https://github.com/jolicode/automapper/pull/184) Fix error when mapping from stdClass to constructor with nullable/optional arguments

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"symfony/lock": "^6.4 || ^7.0",
2525
"symfony/property-info": "^6.4 || ^7.0",
2626
"symfony/property-access": "^6.4 || ^7.0",
27-
"phpdocumentor/type-resolver": "^1.7"
27+
"phpdocumentor/type-resolver": "^1.7",
28+
"phpstan/phpdoc-parser": "^1.13 || ^2.0"
2829
},
2930
"require-dev": {
3031
"api-platform/core": "^3.0.4",

src/Extractor/GetTypeTrait.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use PHPStan\PhpDocParser\Parser\PhpDocParser;
1313
use PHPStan\PhpDocParser\Parser\TokenIterator;
1414
use PHPStan\PhpDocParser\Parser\TypeParser;
15+
use PHPStan\PhpDocParser\ParserConfig;
1516
use Symfony\Component\PropertyInfo\PhpStan\NameScopeFactory;
1617
use Symfony\Component\PropertyInfo\Type;
1718
use Symfony\Component\PropertyInfo\Util\PhpStanTypeHelper;
@@ -43,8 +44,20 @@ private function extractFromDocBlock(string|false|null $rawDocNode, string $clas
4344
return null;
4445
}
4546

46-
static $phpDocParser = new PhpDocParser(new TypeParser(new ConstExprParser()), new ConstExprParser());
47-
static $lexer = new Lexer();
47+
static $phpDocParser = null;
48+
static $lexer = null;
49+
50+
if ($phpDocParser === null || $lexer === null) {
51+
if (class_exists(ParserConfig::class)) {
52+
$config = new ParserConfig([]);
53+
$phpDocParser = new PhpDocParser($config, new TypeParser($config, new ConstExprParser($config)), new ConstExprParser($config));
54+
$lexer = new Lexer($config);
55+
} else {
56+
$phpDocParser = new PhpDocParser(new TypeParser(new ConstExprParser()), new ConstExprParser()); // @phpstan-ignore-line
57+
$lexer = new Lexer(); // @phpstan-ignore-line
58+
}
59+
}
60+
4861
static $nameScopeFactory = new NameScopeFactory();
4962
static $phpStanTypeHelper = new PhpStanTypeHelper();
5063

0 commit comments

Comments
 (0)