Skip to content

Commit ae3a484

Browse files
authored
Merge branch refs/heads/1.12.x into 2.1.x
2 parents 449ca0a + b57bcad commit ae3a484

File tree

2 files changed

+92
-53
lines changed

2 files changed

+92
-53
lines changed

src/PhpDoc/PhpDocBlock.php

+51-53
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

@@ -128,18 +129,24 @@ public static function resolvePhpDocBlockForProperty(
128129
array $newPositionalParameterNames, // unused
129130
): self
130131
{
131-
return self::resolvePhpDocBlockTree(
132-
$docComment,
133-
$classReflection,
134-
$trait,
132+
$docBlocksFromParents = self::resolveParentPhpDocBlocks(
133+
self::getParentReflections($classReflection),
135134
$propertyName,
136-
$file,
137135
'hasNativeProperty',
138136
'getNativeProperty',
139137
__FUNCTION__,
140-
$explicit,
141-
[],
142-
[],
138+
$explicit ?? $docComment !== null,
139+
$newPositionalParameterNames,
140+
);
141+
142+
return new self(
143+
$docComment ?? ResolvedPhpDocBlock::EMPTY_DOC_STRING,
144+
$file,
145+
$classReflection,
146+
$trait,
147+
$explicit ?? true,
148+
self::remapParameterNames($originalPositionalParameterNames, $newPositionalParameterNames),
149+
$docBlocksFromParents,
143150
);
144151
}
145152

@@ -158,18 +165,24 @@ public static function resolvePhpDocBlockForConstant(
158165
array $newPositionalParameterNames, // unused
159166
): self
160167
{
161-
return self::resolvePhpDocBlockTree(
162-
$docComment,
163-
$classReflection,
164-
null,
168+
$docBlocksFromParents = self::resolveParentPhpDocBlocks(
169+
self::getParentReflections($classReflection),
165170
$constantName,
166-
$file,
167171
'hasConstant',
168172
'getConstant',
169173
__FUNCTION__,
170-
$explicit,
171-
[],
172-
[],
174+
$explicit ?? $docComment !== null,
175+
$newPositionalParameterNames,
176+
);
177+
178+
return new self(
179+
$docComment ?? ResolvedPhpDocBlock::EMPTY_DOC_STRING,
180+
$file,
181+
$classReflection,
182+
$trait,
183+
$explicit ?? true,
184+
self::remapParameterNames($originalPositionalParameterNames, $newPositionalParameterNames),
185+
$docBlocksFromParents,
173186
);
174187
}
175188

@@ -188,45 +201,30 @@ public static function resolvePhpDocBlockForMethod(
188201
array $newPositionalParameterNames,
189202
): self
190203
{
191-
return self::resolvePhpDocBlockTree(
192-
$docComment,
193-
$classReflection,
194-
$trait,
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+
222+
$docBlocksFromParents = self::resolveParentPhpDocBlocks(
223+
$parentReflections,
195224
$methodName,
196-
$file,
197225
'hasNativeMethod',
198226
'getNativeMethod',
199227
__FUNCTION__,
200-
$explicit,
201-
$originalPositionalParameterNames,
202-
$newPositionalParameterNames,
203-
);
204-
}
205-
206-
/**
207-
* @param array<int, string> $originalPositionalParameterNames
208-
* @param array<int, string> $newPositionalParameterNames
209-
*/
210-
private static function resolvePhpDocBlockTree(
211-
?string $docComment,
212-
ClassReflection $classReflection,
213-
?string $trait,
214-
string $name,
215-
?string $file,
216-
string $hasMethodName,
217-
string $getMethodName,
218-
string $resolveMethodName,
219-
?bool $explicit,
220-
array $originalPositionalParameterNames,
221-
array $newPositionalParameterNames,
222-
): self
223-
{
224-
$docBlocksFromParents = self::resolveParentPhpDocBlocks(
225-
$classReflection,
226-
$name,
227-
$hasMethodName,
228-
$getMethodName,
229-
$resolveMethodName,
230228
$explicit ?? $docComment !== null,
231229
$newPositionalParameterNames,
232230
);
@@ -264,11 +262,12 @@ private static function remapParameterNames(
264262
}
265263

266264
/**
265+
* @param array<int, ClassReflection> $parentReflections
267266
* @param array<int, string> $positionalParameterNames
268267
* @return array<int, self>
269268
*/
270269
private static function resolveParentPhpDocBlocks(
271-
ClassReflection $classReflection,
270+
array $parentReflections,
272271
string $name,
273272
string $hasMethodName,
274273
string $getMethodName,
@@ -278,7 +277,6 @@ private static function resolveParentPhpDocBlocks(
278277
): array
279278
{
280279
$result = [];
281-
$parentReflections = self::getParentReflections($classReflection);
282280

283281
foreach ($parentReflections as $parentReflection) {
284282
$oneResult = self::resolvePhpDocBlockFromClass(
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)