forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResolvedPhpDocBlock.php
670 lines (577 loc) · 16.9 KB
/
ResolvedPhpDocBlock.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
<?php declare(strict_types = 1);
namespace PHPStan\PhpDoc;
use PHPStan\Analyser\NameScope;
use PHPStan\PhpDoc\Tag\MixinTag;
use PHPStan\PhpDoc\Tag\ParamTag;
use PHPStan\PhpDoc\Tag\ReturnTag;
use PHPStan\PhpDoc\Tag\ThrowsTag;
use PHPStan\PhpDoc\Tag\TypeAliasImportTag;
use PHPStan\PhpDoc\Tag\TypeAliasTag;
use PHPStan\PhpDoc\Tag\TypedTag;
use PHPStan\PhpDoc\Tag\VarTag;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\Generic\TemplateTypeMap;
class ResolvedPhpDocBlock
{
private PhpDocNode $phpDocNode;
/** @var PhpDocNode[] */
private array $phpDocNodes;
private string $phpDocString;
private ?string $filename;
private ?NameScope $nameScope = null;
private TemplateTypeMap $templateTypeMap;
/** @var array<string, \PHPStan\PhpDoc\Tag\TemplateTag> */
private array $templateTags;
private \PHPStan\PhpDoc\PhpDocNodeResolver $phpDocNodeResolver;
/** @var array<string|int, \PHPStan\PhpDoc\Tag\VarTag>|false */
private $varTags = false;
/** @var array<string, \PHPStan\PhpDoc\Tag\MethodTag>|false */
private $methodTags = false;
/** @var array<string, \PHPStan\PhpDoc\Tag\PropertyTag>|false */
private $propertyTags = false;
/** @var array<string, \PHPStan\PhpDoc\Tag\ExtendsTag>|false */
private $extendsTags = false;
/** @var array<string, \PHPStan\PhpDoc\Tag\ImplementsTag>|false */
private $implementsTags = false;
/** @var array<string, \PHPStan\PhpDoc\Tag\UsesTag>|false */
private $usesTags = false;
/** @var array<string, \PHPStan\PhpDoc\Tag\ParamTag>|false */
private $paramTags = false;
/** @var \PHPStan\PhpDoc\Tag\ReturnTag|false|null */
private $returnTag = false;
/** @var \PHPStan\PhpDoc\Tag\ThrowsTag|false|null */
private $throwsTag = false;
/** @var array<MixinTag>|false */
private $mixinTags = false;
/** @var array<TypeAliasTag>|false */
private $typeAliasTags = false;
/** @var array<TypeAliasImportTag>|false */
private $typeAliasImportTags = false;
/** @var \PHPStan\PhpDoc\Tag\DeprecatedTag|false|null */
private $deprecatedTag = false;
private ?bool $isDeprecated = null;
private ?bool $isInternal = null;
private ?bool $isFinal = null;
/** @var bool|'notLoaded'|null */
private $isPure = 'notLoaded';
private function __construct()
{
}
/**
* @param \PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode $phpDocNode
* @param string $phpDocString
* @param string $filename
* @param \PHPStan\Analyser\NameScope $nameScope
* @param \PHPStan\Type\Generic\TemplateTypeMap $templateTypeMap
* @param \PHPStan\PhpDoc\Tag\TemplateTag[] $templateTags
* @param \PHPStan\PhpDoc\PhpDocNodeResolver $phpDocNodeResolver
* @return self
*/
public static function create(
PhpDocNode $phpDocNode,
string $phpDocString,
string $filename,
NameScope $nameScope,
TemplateTypeMap $templateTypeMap,
array $templateTags,
PhpDocNodeResolver $phpDocNodeResolver
): self
{
// new property also needs to be added to createEmpty() and merge()
$self = new self();
$self->phpDocNode = $phpDocNode;
$self->phpDocNodes = [$phpDocNode];
$self->phpDocString = $phpDocString;
$self->filename = $filename;
$self->nameScope = $nameScope;
$self->templateTypeMap = $templateTypeMap;
$self->templateTags = $templateTags;
$self->phpDocNodeResolver = $phpDocNodeResolver;
return $self;
}
public static function createEmpty(): self
{
// new property also needs to be added to merge()
$self = new self();
$self->phpDocString = '/** */';
$self->phpDocNodes = [];
$self->filename = null;
$self->templateTypeMap = TemplateTypeMap::createEmpty();
$self->templateTags = [];
$self->varTags = [];
$self->methodTags = [];
$self->propertyTags = [];
$self->extendsTags = [];
$self->implementsTags = [];
$self->usesTags = [];
$self->paramTags = [];
$self->returnTag = null;
$self->throwsTag = null;
$self->mixinTags = [];
$self->typeAliasTags = [];
$self->typeAliasImportTags = [];
$self->deprecatedTag = null;
$self->isDeprecated = false;
$self->isInternal = false;
$self->isFinal = false;
$self->isPure = null;
return $self;
}
/**
* @param array<int, self> $parents
* @param array<int, PhpDocBlock> $parentPhpDocBlocks
* @return self
*/
public function merge(array $parents, array $parentPhpDocBlocks): self
{
// new property also needs to be added to createEmpty()
$result = new self();
// we will resolve everything on $this here so these properties don't have to be populated
// skip $result->phpDocNode
// skip $result->phpDocString - just for stubs
$phpDocNodes = $this->phpDocNodes;
foreach ($parents as $parent) {
foreach ($parent->phpDocNodes as $phpDocNode) {
$phpDocNodes[] = $phpDocNode;
}
}
$result->phpDocNodes = $phpDocNodes;
$result->filename = $this->filename;
// skip $result->nameScope
$result->templateTypeMap = $this->templateTypeMap;
$result->templateTags = $this->templateTags;
// skip $result->phpDocNodeResolver
$result->varTags = self::mergeVarTags($this->getVarTags(), $parents, $parentPhpDocBlocks);
$result->methodTags = $this->getMethodTags();
$result->propertyTags = $this->getPropertyTags();
$result->extendsTags = $this->getExtendsTags();
$result->implementsTags = $this->getImplementsTags();
$result->usesTags = $this->getUsesTags();
$result->paramTags = self::mergeParamTags($this->getParamTags(), $parents, $parentPhpDocBlocks);
$result->returnTag = self::mergeReturnTags($this->getReturnTag(), $parents, $parentPhpDocBlocks);
$result->throwsTag = self::mergeThrowsTags($this->getThrowsTag(), $parents);
$result->mixinTags = $this->getMixinTags();
$result->typeAliasTags = $this->getTypeAliasTags();
$result->typeAliasImportTags = $this->getTypeAliasImportTags();
$result->deprecatedTag = $this->getDeprecatedTag();
$result->isDeprecated = $result->deprecatedTag !== null;
$result->isInternal = $this->isInternal();
$result->isFinal = $this->isFinal();
$result->isPure = $this->isPure();
return $result;
}
/**
* @param array<string, string> $parameterNameMapping
* @return self
*/
public function changeParameterNamesByMapping(array $parameterNameMapping): self
{
$paramTags = $this->getParamTags();
$newParamTags = [];
foreach ($paramTags as $key => $paramTag) {
if (!array_key_exists($key, $parameterNameMapping)) {
continue;
}
$newParamTags[$parameterNameMapping[$key]] = $paramTag;
}
$self = new self();
$self->phpDocNode = $this->phpDocNode;
$self->phpDocNodes = $this->phpDocNodes;
$self->phpDocString = $this->phpDocString;
$self->filename = $this->filename;
$self->nameScope = $this->nameScope;
$self->templateTypeMap = $this->templateTypeMap;
$self->templateTags = $this->templateTags;
$self->phpDocNodeResolver = $this->phpDocNodeResolver;
$self->varTags = $this->varTags;
$self->methodTags = $this->methodTags;
$self->propertyTags = $this->propertyTags;
$self->extendsTags = $this->extendsTags;
$self->implementsTags = $this->implementsTags;
$self->usesTags = $this->usesTags;
$self->paramTags = $newParamTags;
$self->returnTag = $this->returnTag;
$self->throwsTag = $this->throwsTag;
$self->mixinTags = $this->mixinTags;
$self->typeAliasTags = $this->typeAliasTags;
$self->typeAliasImportTags = $this->typeAliasImportTags;
$self->deprecatedTag = $this->deprecatedTag;
$self->isDeprecated = $this->isDeprecated;
$self->isInternal = $this->isInternal;
$self->isFinal = $this->isFinal;
$self->isPure = $this->isPure;
return $self;
}
public function getPhpDocString(): string
{
return $this->phpDocString;
}
/**
* @return PhpDocNode[]
*/
public function getPhpDocNodes(): array
{
return $this->phpDocNodes;
}
public function getFilename(): ?string
{
return $this->filename;
}
private function getNameScope(): NameScope
{
return $this->nameScope;
}
public function getNullableNameScope(): ?NameScope
{
return $this->nameScope;
}
/**
* @return array<string|int, \PHPStan\PhpDoc\Tag\VarTag>
*/
public function getVarTags(): array
{
if ($this->varTags === false) {
$this->varTags = $this->phpDocNodeResolver->resolveVarTags(
$this->phpDocNode,
$this->getNameScope()
);
}
return $this->varTags;
}
/**
* @return array<string, \PHPStan\PhpDoc\Tag\MethodTag>
*/
public function getMethodTags(): array
{
if ($this->methodTags === false) {
$this->methodTags = $this->phpDocNodeResolver->resolveMethodTags(
$this->phpDocNode,
$this->getNameScope()
);
}
return $this->methodTags;
}
/**
* @return array<string, \PHPStan\PhpDoc\Tag\PropertyTag>
*/
public function getPropertyTags(): array
{
if ($this->propertyTags === false) {
$this->propertyTags = $this->phpDocNodeResolver->resolvePropertyTags(
$this->phpDocNode,
$this->getNameScope()
);
}
return $this->propertyTags;
}
/**
* @return array<string, \PHPStan\PhpDoc\Tag\TemplateTag>
*/
public function getTemplateTags(): array
{
return $this->templateTags;
}
/**
* @return array<string, \PHPStan\PhpDoc\Tag\ExtendsTag>
*/
public function getExtendsTags(): array
{
if ($this->extendsTags === false) {
$this->extendsTags = $this->phpDocNodeResolver->resolveExtendsTags(
$this->phpDocNode,
$this->getNameScope()
);
}
return $this->extendsTags;
}
/**
* @return array<string, \PHPStan\PhpDoc\Tag\ImplementsTag>
*/
public function getImplementsTags(): array
{
if ($this->implementsTags === false) {
$this->implementsTags = $this->phpDocNodeResolver->resolveImplementsTags(
$this->phpDocNode,
$this->getNameScope()
);
}
return $this->implementsTags;
}
/**
* @return array<string, \PHPStan\PhpDoc\Tag\UsesTag>
*/
public function getUsesTags(): array
{
if ($this->usesTags === false) {
$this->usesTags = $this->phpDocNodeResolver->resolveUsesTags(
$this->phpDocNode,
$this->getNameScope()
);
}
return $this->usesTags;
}
/**
* @return array<string, \PHPStan\PhpDoc\Tag\ParamTag>
*/
public function getParamTags(): array
{
if ($this->paramTags === false) {
$this->paramTags = $this->phpDocNodeResolver->resolveParamTags(
$this->phpDocNode,
$this->getNameScope()
);
}
return $this->paramTags;
}
public function getReturnTag(): ?\PHPStan\PhpDoc\Tag\ReturnTag
{
if ($this->returnTag === false) {
$this->returnTag = $this->phpDocNodeResolver->resolveReturnTag(
$this->phpDocNode,
$this->getNameScope()
);
}
return $this->returnTag;
}
public function getThrowsTag(): ?\PHPStan\PhpDoc\Tag\ThrowsTag
{
if ($this->throwsTag === false) {
$this->throwsTag = $this->phpDocNodeResolver->resolveThrowsTags(
$this->phpDocNode,
$this->getNameScope()
);
}
return $this->throwsTag;
}
/**
* @return array<MixinTag>
*/
public function getMixinTags(): array
{
if ($this->mixinTags === false) {
$this->mixinTags = $this->phpDocNodeResolver->resolveMixinTags(
$this->phpDocNode,
$this->getNameScope()
);
}
return $this->mixinTags;
}
/**
* @return array<TypeAliasTag>
*/
public function getTypeAliasTags(): array
{
if ($this->typeAliasTags === false) {
$this->typeAliasTags = $this->phpDocNodeResolver->resolveTypeAliasTags(
$this->phpDocNode,
$this->getNameScope()
);
}
return $this->typeAliasTags;
}
/**
* @return array<TypeAliasImportTag>
*/
public function getTypeAliasImportTags(): array
{
if ($this->typeAliasImportTags === false) {
$this->typeAliasImportTags = $this->phpDocNodeResolver->resolveTypeAliasImportTags(
$this->phpDocNode,
$this->getNameScope()
);
}
return $this->typeAliasImportTags;
}
public function getDeprecatedTag(): ?\PHPStan\PhpDoc\Tag\DeprecatedTag
{
if ($this->deprecatedTag === false) {
$this->deprecatedTag = $this->phpDocNodeResolver->resolveDeprecatedTag(
$this->phpDocNode,
$this->getNameScope()
);
}
return $this->deprecatedTag;
}
public function isDeprecated(): bool
{
if ($this->isDeprecated === null) {
$this->isDeprecated = $this->phpDocNodeResolver->resolveIsDeprecated(
$this->phpDocNode
);
}
return $this->isDeprecated;
}
public function isInternal(): bool
{
if ($this->isInternal === null) {
$this->isInternal = $this->phpDocNodeResolver->resolveIsInternal(
$this->phpDocNode
);
}
return $this->isInternal;
}
public function isFinal(): bool
{
if ($this->isFinal === null) {
$this->isFinal = $this->phpDocNodeResolver->resolveIsFinal(
$this->phpDocNode
);
}
return $this->isFinal;
}
public function getTemplateTypeMap(): TemplateTypeMap
{
return $this->templateTypeMap;
}
public function isPure(): ?bool
{
if ($this->isPure === 'notLoaded') {
$pure = $this->phpDocNodeResolver->resolveIsPure(
$this->phpDocNode
);
if ($pure) {
$this->isPure = true;
return $this->isPure;
} else {
$impure = $this->phpDocNodeResolver->resolveIsImpure(
$this->phpDocNode
);
if ($impure) {
$this->isPure = false;
return $this->isPure;
}
}
$this->isPure = null;
}
return $this->isPure;
}
/**
* @param array<string|int, VarTag> $varTags
* @param array<int, self> $parents
* @param array<int, PhpDocBlock> $parentPhpDocBlocks
* @return array<string|int, VarTag>
*/
private static function mergeVarTags(array $varTags, array $parents, array $parentPhpDocBlocks): array
{
// Only allow one var tag per comment. Check the parent if child does not have this tag.
if (count($varTags) > 0) {
return $varTags;
}
foreach ($parents as $i => $parent) {
$result = self::mergeOneParentVarTags($parent, $parentPhpDocBlocks[$i]);
if ($result === null) {
continue;
}
return $result;
}
return [];
}
/**
* @param ResolvedPhpDocBlock $parent
* @param PhpDocBlock $phpDocBlock
* @return array<string|int, VarTag>|null
*/
private static function mergeOneParentVarTags(self $parent, PhpDocBlock $phpDocBlock): ?array
{
foreach ($parent->getVarTags() as $key => $parentVarTag) {
return [$key => self::resolveTemplateTypeInTag($parentVarTag, $phpDocBlock)];
}
return null;
}
/**
* @param array<string, ParamTag> $paramTags
* @param array<int, self> $parents
* @param array<int, PhpDocBlock> $parentPhpDocBlocks
* @return array<string, ParamTag>
*/
private static function mergeParamTags(array $paramTags, array $parents, array $parentPhpDocBlocks): array
{
foreach ($parents as $i => $parent) {
$paramTags = self::mergeOneParentParamTags($paramTags, $parent, $parentPhpDocBlocks[$i]);
}
return $paramTags;
}
/**
* @param array<string, ParamTag> $paramTags
* @param ResolvedPhpDocBlock $parent
* @param PhpDocBlock $phpDocBlock
* @return array<string, ParamTag>
*/
private static function mergeOneParentParamTags(array $paramTags, self $parent, PhpDocBlock $phpDocBlock): array
{
$parentParamTags = $phpDocBlock->transformArrayKeysWithParameterNameMapping($parent->getParamTags());
foreach ($parentParamTags as $name => $parentParamTag) {
if (array_key_exists($name, $paramTags)) {
continue;
}
$paramTags[$name] = self::resolveTemplateTypeInTag($parentParamTag, $phpDocBlock);
}
return $paramTags;
}
/**
* @param ReturnTag|null $returnTag
* @param array<int, self> $parents
* @param array<int, PhpDocBlock> $parentPhpDocBlocks
* @return ReturnTag|Null
*/
private static function mergeReturnTags(?ReturnTag $returnTag, array $parents, array $parentPhpDocBlocks): ?ReturnTag
{
if ($returnTag !== null) {
return $returnTag;
}
foreach ($parents as $i => $parent) {
$result = self::mergeOneParentReturnTag($returnTag, $parent, $parentPhpDocBlocks[$i]);
if ($result === null) {
continue;
}
return $result;
}
return null;
}
private static function mergeOneParentReturnTag(?ReturnTag $returnTag, self $parent, PhpDocBlock $phpDocBlock): ?ReturnTag
{
$parentReturnTag = $parent->getReturnTag();
if ($parentReturnTag === null) {
return $returnTag;
}
$parentType = $parentReturnTag->getType();
// Each parent would overwrite the previous one except if it returns a less specific type.
// Do not care for incompatible types as there is a separate rule for that.
if ($returnTag !== null && $parentType->isSuperTypeOf($returnTag->getType())->yes()) {
return null;
}
return self::resolveTemplateTypeInTag($parentReturnTag->toImplicit(), $phpDocBlock);
}
/**
* @param array<int, self> $parents
*/
private static function mergeThrowsTags(?ThrowsTag $throwsTag, array $parents): ?ThrowsTag
{
if ($throwsTag !== null) {
return $throwsTag;
}
foreach ($parents as $parent) {
$result = $parent->getThrowsTag();
if ($result === null) {
continue;
}
return $result;
}
return null;
}
/**
* @template T of \PHPStan\PhpDoc\Tag\TypedTag
* @param T $tag
* @param PhpDocBlock $phpDocBlock
* @return T
*/
private static function resolveTemplateTypeInTag(TypedTag $tag, PhpDocBlock $phpDocBlock): TypedTag
{
$type = TemplateTypeHelper::resolveTemplateTypes(
$tag->getType(),
$phpDocBlock->getClassReflection()->getActiveTemplateTypeMap()
);
return $tag->withType($type);
}
}