Skip to content

Commit e9dd3a3

Browse files
committed
Fix normal vs. stub PHPDoc
1 parent ae6dca5 commit e9dd3a3

9 files changed

+36
-74
lines changed

Diff for: src/PhpDoc/PhpDocInheritanceResolver.php

+35-6
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,24 @@
44

55
use PHPStan\Reflection\ClassReflection;
66
use PHPStan\Type\FileTypeMapper;
7+
use ReflectionParameter;
8+
use function array_map;
79
use function strtolower;
810

911
class PhpDocInheritanceResolver
1012
{
1113

1214
private FileTypeMapper $fileTypeMapper;
1315

16+
private StubPhpDocProvider $stubPhpDocProvider;
17+
1418
public function __construct(
1519
FileTypeMapper $fileTypeMapper,
20+
StubPhpDocProvider $stubPhpDocProvider,
1621
)
1722
{
1823
$this->fileTypeMapper = $fileTypeMapper;
24+
$this->stubPhpDocProvider = $stubPhpDocProvider;
1925
}
2026

2127
public function resolvePhpDocForProperty(
@@ -37,7 +43,7 @@ public function resolvePhpDocForProperty(
3743
[],
3844
);
3945

40-
return $this->docBlockTreeToResolvedDocBlock($phpDocBlock, $declaringTraitName, null);
46+
return $this->docBlockTreeToResolvedDocBlock($phpDocBlock, $declaringTraitName, null, $propertyName, null);
4147
}
4248

4349
public function resolvePhpDocForConstant(
@@ -58,7 +64,7 @@ public function resolvePhpDocForConstant(
5864
[],
5965
);
6066

61-
return $this->docBlockTreeToResolvedDocBlock($phpDocBlock, null, null);
67+
return $this->docBlockTreeToResolvedDocBlock($phpDocBlock, null, null, null, $constantName);
6268
}
6369

6470
/**
@@ -84,10 +90,10 @@ public function resolvePhpDocForMethod(
8490
$positionalParameterNames,
8591
);
8692

87-
return $this->docBlockTreeToResolvedDocBlock($phpDocBlock, $phpDocBlock->getTrait(), $methodName);
93+
return $this->docBlockTreeToResolvedDocBlock($phpDocBlock, $phpDocBlock->getTrait(), $methodName, null, null);
8894
}
8995

90-
private function docBlockTreeToResolvedDocBlock(PhpDocBlock $phpDocBlock, ?string $traitName, ?string $functionName): ResolvedPhpDocBlock
96+
private function docBlockTreeToResolvedDocBlock(PhpDocBlock $phpDocBlock, ?string $traitName, ?string $functionName, ?string $propertyName, ?string $constantName): ResolvedPhpDocBlock
9197
{
9298
$parents = [];
9399
$parentPhpDocBlocks = [];
@@ -104,17 +110,40 @@ private function docBlockTreeToResolvedDocBlock(PhpDocBlock $phpDocBlock, ?strin
104110
$parentPhpDocBlock,
105111
$parentPhpDocBlock->getTrait(),
106112
$functionName,
113+
$propertyName,
114+
$constantName,
107115
);
108116
$parentPhpDocBlocks[] = $parentPhpDocBlock;
109117
}
110118

111-
$oneResolvedDockBlock = $this->docBlockToResolvedDocBlock($phpDocBlock, $traitName, $functionName);
119+
$oneResolvedDockBlock = $this->docBlockToResolvedDocBlock($phpDocBlock, $traitName, $functionName, $propertyName, $constantName);
112120
return $oneResolvedDockBlock->merge($parents, $parentPhpDocBlocks);
113121
}
114122

115-
private function docBlockToResolvedDocBlock(PhpDocBlock $phpDocBlock, ?string $traitName, ?string $functionName): ResolvedPhpDocBlock
123+
private function docBlockToResolvedDocBlock(PhpDocBlock $phpDocBlock, ?string $traitName, ?string $functionName, ?string $propertyName, ?string $constantName): ResolvedPhpDocBlock
116124
{
117125
$classReflection = $phpDocBlock->getClassReflection();
126+
if ($functionName !== null && $classReflection->getNativeReflection()->hasMethod($functionName)) {
127+
$methodReflection = $classReflection->getNativeReflection()->getMethod($functionName);
128+
$stub = $this->stubPhpDocProvider->findMethodPhpDoc($classReflection->getName(), $functionName, array_map(static fn (ReflectionParameter $parameter): string => $parameter->getName(), $methodReflection->getParameters()));
129+
if ($stub !== null) {
130+
return $stub;
131+
}
132+
}
133+
134+
if ($propertyName !== null && $classReflection->getNativeReflection()->hasProperty($propertyName)) {
135+
$stub = $this->stubPhpDocProvider->findPropertyPhpDoc($classReflection->getName(), $propertyName);
136+
if ($stub !== null) {
137+
return $stub;
138+
}
139+
}
140+
141+
if ($constantName !== null && $classReflection->getNativeReflection()->hasConstant($constantName)) {
142+
$stub = $this->stubPhpDocProvider->findClassConstantPhpDoc($classReflection->getName(), $constantName);
143+
if ($stub !== null) {
144+
return $stub;
145+
}
146+
}
118147

119148
return $this->fileTypeMapper->getResolvedPhpDoc(
120149
$phpDocBlock->getFile(),

Diff for: src/Reflection/Native/NativeMethodReflection.php

-8
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ class NativeMethodReflection implements MethodReflection
3030

3131
private TrinaryLogic $hasSideEffects;
3232

33-
private ?string $stubPhpDocString;
34-
3533
private ?Type $throwType;
3634

3735
/**
@@ -43,7 +41,6 @@ public function __construct(
4341
BuiltinMethodReflection $reflection,
4442
array $variants,
4543
TrinaryLogic $hasSideEffects,
46-
?string $stubPhpDocString,
4744
?Type $throwType,
4845
)
4946
{
@@ -52,7 +49,6 @@ public function __construct(
5249
$this->reflection = $reflection;
5350
$this->variants = $variants;
5451
$this->hasSideEffects = $hasSideEffects;
55-
$this->stubPhpDocString = $stubPhpDocString;
5652
$this->throwType = $throwType;
5753
}
5854

@@ -173,10 +169,6 @@ private function isVoid(): bool
173169

174170
public function getDocComment(): ?string
175171
{
176-
if ($this->stubPhpDocString !== null) {
177-
return $this->stubPhpDocString;
178-
}
179-
180172
return $this->reflection->getDocComment();
181173
}
182174

Diff for: src/Reflection/Php/PhpClassReflectionExtension.php

-10
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,6 @@ private function createProperty(
221221
$declaringClassName,
222222
$propertyReflection->getName(),
223223
);
224-
$stubPhpDocString = null;
225224
if ($resolvedPhpDoc === null) {
226225
if ($declaringClassReflection->getFileName() !== null) {
227226
$declaringTraitName = $this->findPropertyTrait($propertyReflection);
@@ -253,7 +252,6 @@ private function createProperty(
253252
}
254253
} else {
255254
$phpDocBlockClassReflection = $declaringClassReflection;
256-
$stubPhpDocString = $resolvedPhpDoc->getPhpDocString();
257255
}
258256

259257
if ($resolvedPhpDoc !== null) {
@@ -338,7 +336,6 @@ private function createProperty(
338336
$deprecatedDescription,
339337
$isDeprecated,
340338
$isInternal,
341-
$stubPhpDocString,
342339
);
343340
}
344341

@@ -463,7 +460,6 @@ private function createMethod(
463460
$i++;
464461
}
465462

466-
$stubPhpDocString = null;
467463
$variants = [];
468464
$reflectionMethod = null;
469465
$throwType = null;
@@ -491,7 +487,6 @@ private function createMethod(
491487
$stubPhpDocPair = $this->findMethodPhpDocIncludingAncestors($declaringClass, $methodReflection->getName(), array_map(static fn (ParameterSignature $parameterSignature): string => $parameterSignature->getName(), $methodSignature->getParameters()));
492488
if ($stubPhpDocPair !== null) {
493489
[$stubPhpDoc, $stubDeclaringClass] = $stubPhpDocPair;
494-
$stubPhpDocString = $stubPhpDoc->getPhpDocString();
495490
$templateTypeMap = $stubDeclaringClass->getActiveTemplateTypeMap();
496491
$returnTag = $stubPhpDoc->getReturnTag();
497492
if ($returnTag !== null) {
@@ -561,7 +556,6 @@ private function createMethod(
561556
$methodReflection,
562557
$variants,
563558
$hasSideEffects,
564-
$stubPhpDocString,
565559
$throwType,
566560
);
567561
}
@@ -573,7 +567,6 @@ private function createMethod(
573567
if ($stubPhpDocPair !== null) {
574568
[$resolvedPhpDoc, $phpDocBlockClassReflection] = $stubPhpDocPair;
575569
}
576-
$stubPhpDocString = null;
577570

578571
if ($resolvedPhpDoc === null) {
579572
if ($declaringClass->getFileName() !== null) {
@@ -590,8 +583,6 @@ private function createMethod(
590583
);
591584
$phpDocBlockClassReflection = $declaringClass;
592585
}
593-
} else {
594-
$stubPhpDocString = $resolvedPhpDoc->getPhpDocString();
595586
}
596587

597588
$declaringTrait = null;
@@ -692,7 +683,6 @@ private function createMethod(
692683
$isDeprecated,
693684
$isInternal,
694685
$isFinal,
695-
$stubPhpDocString,
696686
$isPure,
697687
);
698688
}

Diff for: src/Reflection/Php/PhpMethodReflection.php

-8
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ class PhpMethodReflection implements MethodReflection
8686

8787
private ?bool $isPure;
8888

89-
private ?string $stubPhpDocString;
90-
9189
/** @var FunctionVariantWithPhpDocs[]|null */
9290
private ?array $variants = null;
9391

@@ -110,7 +108,6 @@ public function __construct(
110108
bool $isDeprecated,
111109
bool $isInternal,
112110
bool $isFinal,
113-
?string $stubPhpDocString,
114111
?bool $isPure,
115112
)
116113
{
@@ -129,7 +126,6 @@ public function __construct(
129126
$this->isDeprecated = $isDeprecated;
130127
$this->isInternal = $isInternal;
131128
$this->isFinal = $isFinal;
132-
$this->stubPhpDocString = $stubPhpDocString;
133129
$this->isPure = $isPure;
134130
}
135131

@@ -145,10 +141,6 @@ public function getDeclaringTrait(): ?ClassReflection
145141

146142
public function getDocComment(): ?string
147143
{
148-
if ($this->stubPhpDocString !== null) {
149-
return $this->stubPhpDocString;
150-
}
151-
152144
return $this->reflection->getDocComment();
153145
}
154146

Diff for: src/Reflection/Php/PhpMethodReflectionFactory.php

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public function create(
2525
bool $isDeprecated,
2626
bool $isInternal,
2727
bool $isFinal,
28-
?string $stubPhpDocString,
2928
?bool $isPure = null,
3029
): PhpMethodReflection;
3130

Diff for: src/Reflection/Php/PhpPropertyReflection.php

-8
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ class PhpPropertyReflection implements PropertyReflection
3636

3737
private bool $isInternal;
3838

39-
private ?string $stubPhpDocString;
40-
4139
public function __construct(
4240
ClassReflection $declaringClass,
4341
?ClassReflection $declaringTrait,
@@ -47,7 +45,6 @@ public function __construct(
4745
?string $deprecatedDescription,
4846
bool $isDeprecated,
4947
bool $isInternal,
50-
?string $stubPhpDocString,
5148
)
5249
{
5350
$this->declaringClass = $declaringClass;
@@ -58,7 +55,6 @@ public function __construct(
5855
$this->deprecatedDescription = $deprecatedDescription;
5956
$this->isDeprecated = $isDeprecated;
6057
$this->isInternal = $isInternal;
61-
$this->stubPhpDocString = $stubPhpDocString;
6258
}
6359

6460
public function getDeclaringClass(): ClassReflection
@@ -73,10 +69,6 @@ public function getDeclaringTrait(): ?ClassReflection
7369

7470
public function getDocComment(): ?string
7571
{
76-
if ($this->stubPhpDocString !== null) {
77-
return $this->stubPhpDocString;
78-
}
79-
8072
$docComment = $this->reflection->getDocComment();
8173
if ($docComment === false) {
8274
return null;

Diff for: tests/PHPStan/Analyser/AnalyserTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -492,7 +492,7 @@ private function createAnalyser(bool $reportUnmatchedIgnoredErrors): Analyser
492492

493493
$typeSpecifier = self::getContainer()->getService('typeSpecifier');
494494
$fileTypeMapper = self::getContainer()->getByType(FileTypeMapper::class);
495-
$phpDocInheritanceResolver = new PhpDocInheritanceResolver($fileTypeMapper);
495+
$phpDocInheritanceResolver = new PhpDocInheritanceResolver($fileTypeMapper, self::getContainer()->getByType(StubPhpDocProvider::class));
496496

497497
$nodeScopeResolver = new NodeScopeResolver(
498498
$reflectionProvider,

Diff for: tests/PHPStan/Levels/data/stubs-methods-3.json

-5
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,5 @@
2323
"message": "Anonymous function should return int but returns string.",
2424
"line": 128,
2525
"ignorable": true
26-
},
27-
{
28-
"message": "Method StubsIntegrationTest\\YetYetAnotherFoo::doFoo() should return stdClass but returns string.",
29-
"line": 219,
30-
"ignorable": true
3126
}
3227
]

Diff for: tests/PHPStan/Levels/data/stubs-methods-6.json

-27
This file was deleted.

0 commit comments

Comments
 (0)