Skip to content

Commit 06e5df6

Browse files
committed
Refactored ResolvedPhpDocBlock to be more lazy
1 parent 209ec91 commit 06e5df6

15 files changed

+339
-230
lines changed

src/Analyser/NameScope.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,4 +107,19 @@ public function withTemplateTypeMap(TemplateTypeMap $map): self
107107
);
108108
}
109109

110+
/**
111+
* @param mixed[] $properties
112+
* @return self
113+
*/
114+
public static function __set_state(array $properties): self
115+
{
116+
return new self(
117+
$properties['namespace'],
118+
$properties['uses'],
119+
$properties['className'],
120+
$properties['functionName'],
121+
$properties['templateTypeMap']
122+
);
123+
}
124+
110125
}

src/PhpDoc/NameScopedPhpDocString.php

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace PHPStan\PhpDoc;
4+
5+
use PHPStan\Analyser\NameScope;
6+
7+
class NameScopedPhpDocString
8+
{
9+
10+
/** @var string */
11+
private $phpDocString;
12+
13+
/** @var \PHPStan\Analyser\NameScope */
14+
private $nameScope;
15+
16+
public function __construct(string $phpDocString, NameScope $nameScope)
17+
{
18+
$this->phpDocString = $phpDocString;
19+
$this->nameScope = $nameScope;
20+
}
21+
22+
public function getPhpDocString(): string
23+
{
24+
return $this->phpDocString;
25+
}
26+
27+
public function getNameScope(): NameScope
28+
{
29+
return $this->nameScope;
30+
}
31+
32+
/**
33+
* @param mixed[] $properties
34+
* @return self
35+
*/
36+
public static function __set_state(array $properties): self
37+
{
38+
return new self(
39+
$properties['phpDocString'],
40+
$properties['nameScope']
41+
);
42+
}
43+
44+
}

src/PhpDoc/PhpDocNodeResolver.php

Lines changed: 14 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020
use PHPStan\PhpDocParser\Ast\PhpDoc\ThrowsTagValueNode;
2121
use PHPStan\Reflection\PassedByReference;
2222
use PHPStan\Type\ArrayType;
23-
use PHPStan\Type\Generic\TemplateTypeFactory;
24-
use PHPStan\Type\Generic\TemplateTypeMap;
2523
use PHPStan\Type\Generic\TemplateTypeVariance;
2624
use PHPStan\Type\IntegerType;
2725
use PHPStan\Type\MixedType;
@@ -43,50 +41,12 @@ public function __construct(TypeNodeResolver $typeNodeResolver, ConstExprNodeRes
4341
$this->constExprNodeResolver = $constExprNodeResolver;
4442
}
4543

46-
public function resolve(PhpDocNode $phpDocNode, NameScope $nameScope): ResolvedPhpDocBlock
47-
{
48-
$templateTags = $this->resolveTemplateTags($phpDocNode, $nameScope);
49-
$templateTypeScope = $nameScope->getTemplateTypeScope();
50-
51-
if ($templateTypeScope !== null) {
52-
$templateTypeMap = new TemplateTypeMap(array_map(static function (TemplateTag $tag) use ($templateTypeScope): Type {
53-
return TemplateTypeFactory::fromTemplateTag($templateTypeScope, $tag);
54-
}, $templateTags));
55-
$nameScope = $nameScope->withTemplateTypeMap(
56-
new TemplateTypeMap(array_merge(
57-
$nameScope->getTemplateTypeMap()->getTypes(),
58-
$templateTypeMap->getTypes()
59-
))
60-
);
61-
} else {
62-
$templateTypeMap = TemplateTypeMap::createEmpty();
63-
}
64-
65-
return ResolvedPhpDocBlock::create(
66-
$this->resolveVarTags($phpDocNode, $nameScope),
67-
$this->resolveMethodTags($phpDocNode, $nameScope),
68-
$this->resolvePropertyTags($phpDocNode, $nameScope),
69-
$templateTags,
70-
$this->resolveExtendsTags($phpDocNode, $nameScope),
71-
$this->resolveImplementsTags($phpDocNode, $nameScope),
72-
$this->resolveUsesTags($phpDocNode, $nameScope),
73-
$this->resolveParamTags($phpDocNode, $nameScope),
74-
$this->resolveReturnTag($phpDocNode, $nameScope),
75-
$this->resolveThrowsTags($phpDocNode, $nameScope),
76-
$this->resolveDeprecatedTag($phpDocNode, $nameScope),
77-
$this->resolveIsDeprecated($phpDocNode),
78-
$this->resolveIsInternal($phpDocNode),
79-
$this->resolveIsFinal($phpDocNode),
80-
$templateTypeMap
81-
);
82-
}
83-
8444
/**
8545
* @param PhpDocNode $phpDocNode
8646
* @param NameScope $nameScope
8747
* @return array<string|int, \PHPStan\PhpDoc\Tag\VarTag>
8848
*/
89-
private function resolveVarTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
49+
public function resolveVarTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
9050
{
9151
foreach (['@phpstan-var', '@psalm-var', '@var'] as $tagName) {
9252
$resolved = [];
@@ -114,7 +74,7 @@ private function resolveVarTags(PhpDocNode $phpDocNode, NameScope $nameScope): a
11474
* @param NameScope $nameScope
11575
* @return array<string, \PHPStan\PhpDoc\Tag\PropertyTag>
11676
*/
117-
private function resolvePropertyTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
77+
public function resolvePropertyTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
11878
{
11979
$resolved = [];
12080

@@ -159,7 +119,7 @@ private function resolvePropertyTags(PhpDocNode $phpDocNode, NameScope $nameScop
159119
* @param NameScope $nameScope
160120
* @return array<string, \PHPStan\PhpDoc\Tag\MethodTag>
161121
*/
162-
private function resolveMethodTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
122+
public function resolveMethodTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
163123
{
164124
$resolved = [];
165125

@@ -200,7 +160,7 @@ private function resolveMethodTags(PhpDocNode $phpDocNode, NameScope $nameScope)
200160
/**
201161
* @return array<string, \PHPStan\PhpDoc\Tag\ExtendsTag>
202162
*/
203-
private function resolveExtendsTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
163+
public function resolveExtendsTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
204164
{
205165
$resolved = [];
206166

@@ -218,7 +178,7 @@ private function resolveExtendsTags(PhpDocNode $phpDocNode, NameScope $nameScope
218178
/**
219179
* @return array<string, \PHPStan\PhpDoc\Tag\ImplementsTag>
220180
*/
221-
private function resolveImplementsTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
181+
public function resolveImplementsTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
222182
{
223183
$resolved = [];
224184

@@ -236,7 +196,7 @@ private function resolveImplementsTags(PhpDocNode $phpDocNode, NameScope $nameSc
236196
/**
237197
* @return array<string, \PHPStan\PhpDoc\Tag\UsesTag>
238198
*/
239-
private function resolveUsesTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
199+
public function resolveUsesTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
240200
{
241201
$resolved = [];
242202

@@ -256,7 +216,7 @@ private function resolveUsesTags(PhpDocNode $phpDocNode, NameScope $nameScope):
256216
* @param NameScope $nameScope
257217
* @return array<string, \PHPStan\PhpDoc\Tag\TemplateTag>
258218
*/
259-
private function resolveTemplateTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
219+
public function resolveTemplateTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
260220
{
261221
$resolved = [];
262222

@@ -288,7 +248,7 @@ private function resolveTemplateTags(PhpDocNode $phpDocNode, NameScope $nameScop
288248
* @param NameScope $nameScope
289249
* @return array<string, \PHPStan\PhpDoc\Tag\ParamTag>
290250
*/
291-
private function resolveParamTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
251+
public function resolveParamTags(PhpDocNode $phpDocNode, NameScope $nameScope): array
292252
{
293253
$resolved = [];
294254

@@ -316,7 +276,7 @@ private function resolveParamTags(PhpDocNode $phpDocNode, NameScope $nameScope):
316276
return $resolved;
317277
}
318278

319-
private function resolveReturnTag(PhpDocNode $phpDocNode, NameScope $nameScope): ?\PHPStan\PhpDoc\Tag\ReturnTag
279+
public function resolveReturnTag(PhpDocNode $phpDocNode, NameScope $nameScope): ?\PHPStan\PhpDoc\Tag\ReturnTag
320280
{
321281
$resolved = null;
322282

@@ -329,7 +289,7 @@ private function resolveReturnTag(PhpDocNode $phpDocNode, NameScope $nameScope):
329289
return $resolved;
330290
}
331291

332-
private function resolveThrowsTags(PhpDocNode $phpDocNode, NameScope $nameScope): ?\PHPStan\PhpDoc\Tag\ThrowsTag
292+
public function resolveThrowsTags(PhpDocNode $phpDocNode, NameScope $nameScope): ?\PHPStan\PhpDoc\Tag\ThrowsTag
333293
{
334294
$types = array_map(function (ThrowsTagValueNode $throwsTagValue) use ($nameScope): Type {
335295
return $this->typeNodeResolver->resolve($throwsTagValue->type, $nameScope);
@@ -342,7 +302,7 @@ private function resolveThrowsTags(PhpDocNode $phpDocNode, NameScope $nameScope)
342302
return new ThrowsTag(TypeCombinator::union(...$types));
343303
}
344304

345-
private function resolveDeprecatedTag(PhpDocNode $phpDocNode, NameScope $nameScope): ?\PHPStan\PhpDoc\Tag\DeprecatedTag
305+
public function resolveDeprecatedTag(PhpDocNode $phpDocNode, NameScope $nameScope): ?\PHPStan\PhpDoc\Tag\DeprecatedTag
346306
{
347307
foreach ($phpDocNode->getDeprecatedTagValues() as $deprecatedTagValue) {
348308
$description = (string) $deprecatedTagValue;
@@ -352,21 +312,21 @@ private function resolveDeprecatedTag(PhpDocNode $phpDocNode, NameScope $nameSco
352312
return null;
353313
}
354314

355-
private function resolveIsDeprecated(PhpDocNode $phpDocNode): bool
315+
public function resolveIsDeprecated(PhpDocNode $phpDocNode): bool
356316
{
357317
$deprecatedTags = $phpDocNode->getTagsByName('@deprecated');
358318

359319
return count($deprecatedTags) > 0;
360320
}
361321

362-
private function resolveIsInternal(PhpDocNode $phpDocNode): bool
322+
public function resolveIsInternal(PhpDocNode $phpDocNode): bool
363323
{
364324
$internalTags = $phpDocNode->getTagsByName('@internal');
365325

366326
return count($internalTags) > 0;
367327
}
368328

369-
private function resolveIsFinal(PhpDocNode $phpDocNode): bool
329+
public function resolveIsFinal(PhpDocNode $phpDocNode): bool
370330
{
371331
$finalTags = $phpDocNode->getTagsByName('@final');
372332

src/PhpDoc/PhpDocStringResolver.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace PHPStan\PhpDoc;
44

5-
use PHPStan\Analyser\NameScope;
5+
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
66
use PHPStan\PhpDocParser\Lexer\Lexer;
77
use PHPStan\PhpDocParser\Parser\PhpDocParser;
88
use PHPStan\PhpDocParser\Parser\TokenIterator;
@@ -16,23 +16,19 @@ class PhpDocStringResolver
1616
/** @var PhpDocParser */
1717
private $phpDocParser;
1818

19-
/** @var PhpDocNodeResolver */
20-
private $phpDocNodeResolver;
21-
22-
public function __construct(Lexer $phpDocLexer, PhpDocParser $phpDocParser, PhpDocNodeResolver $phpDocNodeResolver)
19+
public function __construct(Lexer $phpDocLexer, PhpDocParser $phpDocParser)
2320
{
24-
$this->phpDocNodeResolver = $phpDocNodeResolver;
2521
$this->phpDocLexer = $phpDocLexer;
2622
$this->phpDocParser = $phpDocParser;
2723
}
2824

29-
public function resolve(string $phpDocString, NameScope $nameScope): ResolvedPhpDocBlock
25+
public function resolve(string $phpDocString): PhpDocNode
3026
{
3127
$tokens = new TokenIterator($this->phpDocLexer->tokenize($phpDocString));
3228
$phpDocNode = $this->phpDocParser->parse($tokens);
3329
$tokens->consumeTokenType(Lexer::TOKEN_END);
3430

35-
return $this->phpDocNodeResolver->resolve($phpDocNode, $nameScope);
31+
return $phpDocNode;
3632
}
3733

3834
}

0 commit comments

Comments
 (0)