|
2 | 2 |
|
3 | 3 | namespace PHPStan\PhpDoc; |
4 | 4 |
|
| 5 | +use PHPStan\DependencyInjection\AutowiredParameter; |
5 | 6 | use PHPStan\DependencyInjection\AutowiredService; |
| 7 | +use PHPStan\Internal\LruCache; |
6 | 8 | use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode; |
7 | 9 | use PHPStan\PhpDocParser\Lexer\Lexer; |
8 | 10 | use PHPStan\PhpDocParser\Parser\PhpDocParser; |
|
12 | 14 | final class PhpDocStringResolver |
13 | 15 | { |
14 | 16 |
|
15 | | - public function __construct(private Lexer $phpDocLexer, private PhpDocParser $phpDocParser) |
| 17 | + /** |
| 18 | + * FileTypeMapper and the rules that validate PHPDocs parse the same strings, often one right after |
| 19 | + * another. The parsed nodes are not modified afterwards, so equal strings can share one node. |
| 20 | + * |
| 21 | + * @var LruCache<PhpDocNode> |
| 22 | + */ |
| 23 | + private LruCache $parsedPhpDocs; |
| 24 | + |
| 25 | + public function __construct( |
| 26 | + private Lexer $phpDocLexer, |
| 27 | + private PhpDocParser $phpDocParser, |
| 28 | + #[AutowiredParameter(ref: '%cache.parsedPhpDocCountMax%')] |
| 29 | + int $parsedPhpDocCountMax, |
| 30 | + ) |
16 | 31 | { |
| 32 | + $this->parsedPhpDocs = new LruCache($parsedPhpDocCountMax); |
17 | 33 | } |
18 | 34 |
|
19 | 35 | public function resolve(string $phpDocString): PhpDocNode |
20 | 36 | { |
| 37 | + $phpDocNode = $this->parsedPhpDocs->get($phpDocString); |
| 38 | + if ($phpDocNode !== null) { |
| 39 | + return $phpDocNode; |
| 40 | + } |
| 41 | + |
21 | 42 | $tokens = new TokenIterator($this->phpDocLexer->tokenize($phpDocString)); |
22 | 43 | $phpDocNode = $this->phpDocParser->parse($tokens); |
23 | 44 | $tokens->consumeTokenType(Lexer::TOKEN_END); // @phpstan-ignore missingType.checkedException |
24 | 45 |
|
| 46 | + $this->parsedPhpDocs->set($phpDocString, $phpDocNode, 0); |
| 47 | + |
25 | 48 | return $phpDocNode; |
26 | 49 | } |
27 | 50 |
|
|
0 commit comments