Skip to content

Commit

Permalink
Fix trait PHPDocs when checking overriden methods
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Dec 1, 2023
1 parent 866ec3b commit 4c4f22f
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 5 deletions.
10 changes: 7 additions & 3 deletions src/Reflection/Php/PhpClassReflectionExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,12 +618,16 @@ private function createMethod(
);
}

return $this->createUserlandMethodReflection($declaringClass, $declaringClass, $methodReflection);
return $this->createUserlandMethodReflection(
$declaringClass,
$declaringClass,
$methodReflection,
$this->findMethodTrait($methodReflection),
);
}

public function createUserlandMethodReflection(ClassReflection $fileDeclaringClass, ClassReflection $actualDeclaringClass, BuiltinMethodReflection $methodReflection): PhpMethodReflection
public function createUserlandMethodReflection(ClassReflection $fileDeclaringClass, ClassReflection $actualDeclaringClass, BuiltinMethodReflection $methodReflection, ?string $declaringTraitName): PhpMethodReflection
{
$declaringTraitName = $this->findMethodTrait($methodReflection);
$resolvedPhpDoc = null;
$stubPhpDocPair = $this->findMethodPhpDocIncludingAncestors($fileDeclaringClass, $methodReflection->getName(), array_map(static fn (ReflectionParameter $parameter): string => $parameter->getName(), $methodReflection->getParameters()));
$phpDocBlockClassReflection = $fileDeclaringClass;
Expand Down
4 changes: 3 additions & 1 deletion src/Rules/Methods/MethodSignatureRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,15 @@ private function collectParentMethods(string $methodName, ClassReflection $class
continue;
}

$declaringTrait = $trait->getNativeMethod($methodName)->getDeclaringClass();
$parentMethods[] = [
$this->phpClassReflectionExtension->createUserlandMethodReflection(
$trait,
$class,
new NativeBuiltinMethodReflection($methodReflection),
$declaringTrait->getName(),
),
$trait->getNativeMethod($methodName)->getDeclaringClass(),
$declaringTrait,
];
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/Rules/Methods/OverridingMethodRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,15 @@ private function findPrototype(ClassReflection $classReflection, string $methodN
$methodReflection = $nativeTraitReflection->getMethod($methodName);
$isAbstract = $methodReflection->isAbstract();
if ($isAbstract) {
$declaringTrait = $trait->getNativeMethod($methodName)->getDeclaringClass();
return [
$this->phpClassReflectionExtension->createUserlandMethodReflection(
$trait,
$classReflection,
new NativeBuiltinMethodReflection($methodReflection),
$declaringTrait->getName(),
),
$trait->getNativeMethod($methodName)->getDeclaringClass(),
$declaringTrait,
false,
];
}
Expand Down
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,16 @@ public function testBug10166(): void
]);
}

public function testBug10184(): void
{
if (PHP_VERSION_ID < 80000) {
$this->markTestSkipped('Test requires PHP 8.0.');
}

$this->reportMaybes = true;
$this->reportStatic = true;

$this->analyse([__DIR__ . '/data/bug-10184.php'], []);
}

}
29 changes: 29 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-10184.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Bug10184;

interface FooI {}
class Bar implements FooI {}

/** @template TValue */
class Collection {}

/** @template TValue of FooI */
trait FooTrait
{
/** @return Collection<TValue> */
abstract public function foo(): Collection;
}

class Baz
{
/** @use FooTrait<Bar> */
use FooTrait;

/** @return Collection<Bar> */
public function foo(): Collection
{
/** @var Collection<Bar> */
return new Collection();
}
}

0 comments on commit 4c4f22f

Please sign in to comment.