Skip to content

Commit

Permalink
Fix PHPDoc inheritance from generic trait
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Feb 5, 2025
1 parent 10388a9 commit 49c631a
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 16 deletions.
45 changes: 29 additions & 16 deletions src/PhpDoc/PhpDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,22 @@ public static function resolvePhpDocBlockForMethod(
array $newPositionalParameterNames,
): self
{
$parentReflections = self::getParentReflections($classReflection);
$docBlocksFromParents = [];
foreach (self::getParentReflections($classReflection) as $parentReflection) {
$oneResult = self::resolveMethodPhpDocBlockFromClass(
$parentReflection,
$methodName,
$explicit ?? $docComment !== null,
$newPositionalParameterNames,
);

if ($oneResult === null) { // Null if it is private or from a wrong trait.
continue;
}

$docBlocksFromParents[] = $oneResult;
}

foreach ($classReflection->getTraits(true) as $traitReflection) {
if (!$traitReflection->hasNativeMethod($methodName)) {
continue;
Expand All @@ -208,23 +223,21 @@ public static function resolvePhpDocBlockForMethod(
continue;
}

$parentReflections[] = $traitReflection;
}

$docBlocksFromParents = [];
foreach ($parentReflections as $parentReflection) {
$oneResult = self::resolveMethodPhpDocBlockFromClass(
$parentReflection,
$methodName,
$explicit ?? $docComment !== null,
$newPositionalParameterNames,
);

if ($oneResult === null) { // Null if it is private or from a wrong trait.
continue;
$methodVariant = $traitMethod->getOnlyVariant();
$positionalMethodParameterNames = [];
foreach ($methodVariant->getParameters() as $methodParameter) {
$positionalMethodParameterNames[] = $methodParameter->getName();
}

$docBlocksFromParents[] = $oneResult;
$docBlocksFromParents[] = new self(
$traitMethod->getDocComment() ?? ResolvedPhpDocBlock::EMPTY_DOC_STRING,
$classReflection->getFileName(),
$classReflection,
$traitReflection->getName(),
$explicit ?? $traitMethod->getDocComment() !== null,
self::remapParameterNames($newPositionalParameterNames, $positionalMethodParameterNames),
[],
);
}

return new self(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace AbstractGenericTraitMethodImplicitPhpDocInheritance;

use function PHPStan\Testing\assertType;

/**
* @template T
*/
trait Foo
{

/** @return T */
abstract public function doFoo();
}

class UseFoo
{

/** @use Foo<int> */
use Foo;

public function doFoo()
{
return 1;
}

}

function (UseFoo $f): void {
assertType('int', $f->doFoo());
};

0 comments on commit 49c631a

Please sign in to comment.