Skip to content

Commit

Permalink
Add support for phpstan/phpdoc-parser 2 (#205)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
Korbeil authored Nov 17, 2024
2 parents 0dbc341 + f3b7f44 commit 42e9ec6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

### Fixed
- [GH#184](https://github.com/jolicode/automapper/pull/184) Fix error when mapping from stdClass to constructor with nullable/optional arguments
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"symfony/lock": "^6.4 || ^7.0",
"symfony/property-info": "^6.4 || ^7.0",
"symfony/property-access": "^6.4 || ^7.0",
"phpdocumentor/type-resolver": "^1.7"
"phpdocumentor/type-resolver": "^1.7",
"phpstan/phpdoc-parser": "^1.13 || ^2.0"
},
"require-dev": {
"api-platform/core": "^3.0.4",
Expand Down
17 changes: 15 additions & 2 deletions src/Extractor/GetTypeTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PHPStan\PhpDocParser\Parser\PhpDocParser;
use PHPStan\PhpDocParser\Parser\TokenIterator;
use PHPStan\PhpDocParser\Parser\TypeParser;
use PHPStan\PhpDocParser\ParserConfig;
use Symfony\Component\PropertyInfo\PhpStan\NameScopeFactory;
use Symfony\Component\PropertyInfo\Type;
use Symfony\Component\PropertyInfo\Util\PhpStanTypeHelper;
Expand Down Expand Up @@ -43,8 +44,20 @@ private function extractFromDocBlock(string|false|null $rawDocNode, string $clas
return null;
}

static $phpDocParser = new PhpDocParser(new TypeParser(new ConstExprParser()), new ConstExprParser());
static $lexer = new Lexer();
static $phpDocParser = null;
static $lexer = null;

if ($phpDocParser === null || $lexer === null) {
if (class_exists(ParserConfig::class)) {
$config = new ParserConfig([]);
$phpDocParser = new PhpDocParser($config, new TypeParser($config, new ConstExprParser($config)), new ConstExprParser($config));
$lexer = new Lexer($config);
} else {
$phpDocParser = new PhpDocParser(new TypeParser(new ConstExprParser()), new ConstExprParser()); // @phpstan-ignore-line
$lexer = new Lexer(); // @phpstan-ignore-line
}
}

static $nameScopeFactory = new NameScopeFactory();
static $phpStanTypeHelper = new PhpStanTypeHelper();

Expand Down

0 comments on commit 42e9ec6

Please sign in to comment.