Skip to content

Commit 4c4f22f

Browse files
committed
Fix trait PHPDocs when checking overriden methods
1 parent 866ec3b commit 4c4f22f

File tree

5 files changed

+54
-5
lines changed

5 files changed

+54
-5
lines changed

src/Reflection/Php/PhpClassReflectionExtension.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -618,12 +618,16 @@ private function createMethod(
618618
);
619619
}
620620

621-
return $this->createUserlandMethodReflection($declaringClass, $declaringClass, $methodReflection);
621+
return $this->createUserlandMethodReflection(
622+
$declaringClass,
623+
$declaringClass,
624+
$methodReflection,
625+
$this->findMethodTrait($methodReflection),
626+
);
622627
}
623628

624-
public function createUserlandMethodReflection(ClassReflection $fileDeclaringClass, ClassReflection $actualDeclaringClass, BuiltinMethodReflection $methodReflection): PhpMethodReflection
629+
public function createUserlandMethodReflection(ClassReflection $fileDeclaringClass, ClassReflection $actualDeclaringClass, BuiltinMethodReflection $methodReflection, ?string $declaringTraitName): PhpMethodReflection
625630
{
626-
$declaringTraitName = $this->findMethodTrait($methodReflection);
627631
$resolvedPhpDoc = null;
628632
$stubPhpDocPair = $this->findMethodPhpDocIncludingAncestors($fileDeclaringClass, $methodReflection->getName(), array_map(static fn (ReflectionParameter $parameter): string => $parameter->getName(), $methodReflection->getParameters()));
629633
$phpDocBlockClassReflection = $fileDeclaringClass;

src/Rules/Methods/MethodSignatureRule.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,13 +150,15 @@ private function collectParentMethods(string $methodName, ClassReflection $class
150150
continue;
151151
}
152152

153+
$declaringTrait = $trait->getNativeMethod($methodName)->getDeclaringClass();
153154
$parentMethods[] = [
154155
$this->phpClassReflectionExtension->createUserlandMethodReflection(
155156
$trait,
156157
$class,
157158
new NativeBuiltinMethodReflection($methodReflection),
159+
$declaringTrait->getName(),
158160
),
159-
$trait->getNativeMethod($methodName)->getDeclaringClass(),
161+
$declaringTrait,
160162
];
161163
}
162164
}

src/Rules/Methods/OverridingMethodRule.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,15 @@ private function findPrototype(ClassReflection $classReflection, string $methodN
334334
$methodReflection = $nativeTraitReflection->getMethod($methodName);
335335
$isAbstract = $methodReflection->isAbstract();
336336
if ($isAbstract) {
337+
$declaringTrait = $trait->getNativeMethod($methodName)->getDeclaringClass();
337338
return [
338339
$this->phpClassReflectionExtension->createUserlandMethodReflection(
339340
$trait,
340341
$classReflection,
341342
new NativeBuiltinMethodReflection($methodReflection),
343+
$declaringTrait->getName(),
342344
),
343-
$trait->getNativeMethod($methodName)->getDeclaringClass(),
345+
$declaringTrait,
344346
false,
345347
];
346348
}

tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,4 +462,16 @@ public function testBug10166(): void
462462
]);
463463
}
464464

465+
public function testBug10184(): void
466+
{
467+
if (PHP_VERSION_ID < 80000) {
468+
$this->markTestSkipped('Test requires PHP 8.0.');
469+
}
470+
471+
$this->reportMaybes = true;
472+
$this->reportStatic = true;
473+
474+
$this->analyse([__DIR__ . '/data/bug-10184.php'], []);
475+
}
476+
465477
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Bug10184;
4+
5+
interface FooI {}
6+
class Bar implements FooI {}
7+
8+
/** @template TValue */
9+
class Collection {}
10+
11+
/** @template TValue of FooI */
12+
trait FooTrait
13+
{
14+
/** @return Collection<TValue> */
15+
abstract public function foo(): Collection;
16+
}
17+
18+
class Baz
19+
{
20+
/** @use FooTrait<Bar> */
21+
use FooTrait;
22+
23+
/** @return Collection<Bar> */
24+
public function foo(): Collection
25+
{
26+
/** @var Collection<Bar> */
27+
return new Collection();
28+
}
29+
}

0 commit comments

Comments
 (0)