Skip to content

Commit 49c631a

Browse files
committed
Fix PHPDoc inheritance from generic trait
1 parent 10388a9 commit 49c631a

File tree

2 files changed

+61
-16
lines changed

2 files changed

+61
-16
lines changed

src/PhpDoc/PhpDocBlock.php

+29-16
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,22 @@ public static function resolvePhpDocBlockForMethod(
193193
array $newPositionalParameterNames,
194194
): self
195195
{
196-
$parentReflections = self::getParentReflections($classReflection);
196+
$docBlocksFromParents = [];
197+
foreach (self::getParentReflections($classReflection) as $parentReflection) {
198+
$oneResult = self::resolveMethodPhpDocBlockFromClass(
199+
$parentReflection,
200+
$methodName,
201+
$explicit ?? $docComment !== null,
202+
$newPositionalParameterNames,
203+
);
204+
205+
if ($oneResult === null) { // Null if it is private or from a wrong trait.
206+
continue;
207+
}
208+
209+
$docBlocksFromParents[] = $oneResult;
210+
}
211+
197212
foreach ($classReflection->getTraits(true) as $traitReflection) {
198213
if (!$traitReflection->hasNativeMethod($methodName)) {
199214
continue;
@@ -208,23 +223,21 @@ public static function resolvePhpDocBlockForMethod(
208223
continue;
209224
}
210225

211-
$parentReflections[] = $traitReflection;
212-
}
213-
214-
$docBlocksFromParents = [];
215-
foreach ($parentReflections as $parentReflection) {
216-
$oneResult = self::resolveMethodPhpDocBlockFromClass(
217-
$parentReflection,
218-
$methodName,
219-
$explicit ?? $docComment !== null,
220-
$newPositionalParameterNames,
221-
);
222-
223-
if ($oneResult === null) { // Null if it is private or from a wrong trait.
224-
continue;
226+
$methodVariant = $traitMethod->getOnlyVariant();
227+
$positionalMethodParameterNames = [];
228+
foreach ($methodVariant->getParameters() as $methodParameter) {
229+
$positionalMethodParameterNames[] = $methodParameter->getName();
225230
}
226231

227-
$docBlocksFromParents[] = $oneResult;
232+
$docBlocksFromParents[] = new self(
233+
$traitMethod->getDocComment() ?? ResolvedPhpDocBlock::EMPTY_DOC_STRING,
234+
$classReflection->getFileName(),
235+
$classReflection,
236+
$traitReflection->getName(),
237+
$explicit ?? $traitMethod->getDocComment() !== null,
238+
self::remapParameterNames($newPositionalParameterNames, $positionalMethodParameterNames),
239+
[],
240+
);
228241
}
229242

230243
return new self(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace AbstractGenericTraitMethodImplicitPhpDocInheritance;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @template T
9+
*/
10+
trait Foo
11+
{
12+
13+
/** @return T */
14+
abstract public function doFoo();
15+
}
16+
17+
class UseFoo
18+
{
19+
20+
/** @use Foo<int> */
21+
use Foo;
22+
23+
public function doFoo()
24+
{
25+
return 1;
26+
}
27+
28+
}
29+
30+
function (UseFoo $f): void {
31+
assertType('int', $f->doFoo());
32+
};

0 commit comments

Comments
 (0)