Skip to content

Commit b57bcad

Browse files
committed
Inherit PHPDoc implicitly from abstract trait method
1 parent ebcb5da commit b57bcad

File tree

2 files changed

+61
-1
lines changed

2 files changed

+61
-1
lines changed

Diff for: src/PhpDoc/PhpDocBlock.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\Type\TypeTraverser;
1717
use function array_key_exists;
1818
use function count;
19+
use function is_bool;
1920
use function strtolower;
2021
use function substr;
2122

@@ -200,8 +201,26 @@ public static function resolvePhpDocBlockForMethod(
200201
array $newPositionalParameterNames,
201202
): self
202203
{
204+
$parentReflections = self::getParentReflections($classReflection);
205+
foreach ($classReflection->getTraits(true) as $traitReflection) {
206+
if (!$traitReflection->hasNativeMethod($methodName)) {
207+
continue;
208+
}
209+
$traitMethod = $traitReflection->getNativeMethod($methodName);
210+
$abstract = $traitMethod->isAbstract();
211+
if (is_bool($abstract)) {
212+
if (!$abstract) {
213+
continue;
214+
}
215+
} elseif (!$abstract->yes()) {
216+
continue;
217+
}
218+
219+
$parentReflections[] = $traitReflection;
220+
}
221+
203222
$docBlocksFromParents = self::resolveParentPhpDocBlocks(
204-
self::getParentReflections($classReflection),
223+
$parentReflections,
205224
$methodName,
206225
'hasNativeMethod',
207226
'getNativeMethod',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace InheritAbstractTraitMethodPhpDoc;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
trait FooTrait
8+
{
9+
10+
/** @return int */
11+
abstract public function doFoo();
12+
13+
/** @return int */
14+
public function doBar()
15+
{
16+
return 1;
17+
}
18+
19+
}
20+
21+
class Foo
22+
{
23+
24+
use FooTrait;
25+
26+
public function doFoo()
27+
{
28+
return 1;
29+
}
30+
31+
public function doBar()
32+
{
33+
return 1;
34+
}
35+
36+
}
37+
38+
function (Foo $foo): void {
39+
assertType('int', $foo->doFoo());
40+
assertType('mixed', $foo->doBar());
41+
};

0 commit comments

Comments
 (0)