Skip to content

Commit

Permalink
Inherit PHPDoc implicitly from abstract trait method
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 24, 2025
1 parent ebcb5da commit b57bcad
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/PhpDoc/PhpDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Type\TypeTraverser;
use function array_key_exists;
use function count;
use function is_bool;
use function strtolower;
use function substr;

Expand Down Expand Up @@ -200,8 +201,26 @@ public static function resolvePhpDocBlockForMethod(
array $newPositionalParameterNames,
): self
{
$parentReflections = self::getParentReflections($classReflection);
foreach ($classReflection->getTraits(true) as $traitReflection) {
if (!$traitReflection->hasNativeMethod($methodName)) {
continue;
}
$traitMethod = $traitReflection->getNativeMethod($methodName);
$abstract = $traitMethod->isAbstract();
if (is_bool($abstract)) {
if (!$abstract) {
continue;
}
} elseif (!$abstract->yes()) {
continue;
}

$parentReflections[] = $traitReflection;
}

$docBlocksFromParents = self::resolveParentPhpDocBlocks(
self::getParentReflections($classReflection),
$parentReflections,
$methodName,
'hasNativeMethod',
'getNativeMethod',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace InheritAbstractTraitMethodPhpDoc;

use function PHPStan\Testing\assertType;

trait FooTrait
{

/** @return int */
abstract public function doFoo();

/** @return int */
public function doBar()
{
return 1;
}

}

class Foo
{

use FooTrait;

public function doFoo()
{
return 1;
}

public function doBar()
{
return 1;
}

}

function (Foo $foo): void {
assertType('int', $foo->doFoo());
assertType('mixed', $foo->doBar());
};

0 comments on commit b57bcad

Please sign in to comment.