Skip to content
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

Add support for phpstan/phpdoc-parser 2 #205

Merged
merged 1 commit into from
Nov 17, 2024
Merged
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
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
Loading