Skip to content

Commit 1dad3a1

Browse files
committed
@inheritdoc for class level docblock
1 parent 3533ad5 commit 1dad3a1

File tree

3 files changed

+62
-35
lines changed

3 files changed

+62
-35
lines changed

src/PHPFUI/InstaDoc/Controller.php

+1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ public function display(array $classPagesToShow = Controller::VALID_CLASS_PAGES,
167167
$callout->addClass('small');
168168
$parameters = $this->getConstructorParameters($fullClassName);
169169
$parameters = \str_replace("\n", '', $parameters);
170+
// @phpstan-ignore-next-line hack for now
170171
$page->addCopyToClipboard("new \\{$fullClassName}({$parameters});", $icon, $callout);
171172
$div->add($callout);
172173
$mainColumn->add($div);

src/PHPFUI/InstaDoc/Section/CodeCommon.php

+60-34
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,19 @@ public function getValueString(mixed $value) : string
188188

189189
/**
190190
* Format comments without indentation
191+
* @template T of \ReflectionClass
192+
* @param \ReflectionMethod | \ReflectionClass<T> | null $reflection
191193
*/
192-
protected function formatComments(?\phpDocumentor\Reflection\DocBlock $docBlock, ?\ReflectionMethod $reflectionMethod = null) : string
194+
protected function formatComments(?\phpDocumentor\Reflection\DocBlock $docBlock, \ReflectionMethod | \ReflectionClass | null $reflection = null) : string
193195
{
194196
if (! $docBlock)
195197
{
196198
return '';
197199
}
198200

199201
$container = new \PHPFUI\Container();
200-
201-
$container->add($this->parsedown->text($this->getInheritedText($docBlock, $reflectionMethod, 'getSummary')));
202-
$desc = $this->getInheritedText($docBlock, $reflectionMethod, 'getDescription');
202+
$container->add($this->parsedown->text($this->getInheritedText($docBlock, $reflection, 'getSummary')));
203+
$desc = $this->getInheritedText($docBlock, $reflection);
203204

204205
if ($desc)
205206
{
@@ -211,9 +212,9 @@ protected function formatComments(?\phpDocumentor\Reflection\DocBlock $docBlock,
211212

212213
$tags = $docBlock->getTags();
213214
// if we are in a method, inheritdoc makes sense, and we should get the correct doc block comments
214-
if ($reflectionMethod)
215+
if ($reflection instanceof \ReflectionMethod)
215216
{
216-
$tags = $this->getInheritedDocBlock($tags, $reflectionMethod);
217+
$tags = $this->getInheritedDocBlock($tags, $reflection);
217218
}
218219

219220
if ($tags)
@@ -288,7 +289,7 @@ protected function formatComments(?\phpDocumentor\Reflection\DocBlock $docBlock,
288289
$ul->addItem(new \PHPFUI\ListItem($this->getColor('name', $name) . ' ' . $this->getColor('description', $body)));
289290
}
290291

291-
$attributes = $this->getAttributes($reflectionMethod);
292+
$attributes = $this->getAttributes($reflection);
292293

293294
foreach ($attributes as $attribute)
294295
{
@@ -367,7 +368,7 @@ protected function getColor(string $class, string $name) : string
367368
/**
368369
* Get comments indented
369370
*/
370-
protected function getComments(?\phpDocumentor\Reflection\DocBlock $docBlock, ?\ReflectionMethod $reflectionMethod = null) : string
371+
protected function getComments(?\phpDocumentor\Reflection\DocBlock $docBlock, ?\ReflectionMethod $reflection = null) : string
371372
{
372373
if (! $docBlock)
373374
{
@@ -379,7 +380,7 @@ protected function getComments(?\phpDocumentor\Reflection\DocBlock $docBlock, ?\
379380
$cell1->add('&nbsp;');
380381
$gridX->add($cell1);
381382
$cell11 = new \PHPFUI\Cell(11);
382-
$cell11->add($this->formatComments($docBlock, $reflectionMethod));
383+
$cell11->add($this->formatComments($docBlock, $reflection));
383384
$gridX->add($cell11);
384385

385386
return $gridX;
@@ -417,11 +418,15 @@ protected function getHtmlClass(string $class) : string
417418
return \str_replace('\\', '-', $class);
418419
}
419420

420-
protected function getInheritedText(\phpDocumentor\Reflection\DocBlock $docBlock, ?\ReflectionMethod $reflectionMethod = null, string $textType = 'getDescription') : string
421+
/**
422+
* @template T of \ReflectionClass
423+
* @param \ReflectionMethod | \ReflectionClass<T> | null $reflection
424+
*/
425+
protected function getInheritedText(\phpDocumentor\Reflection\DocBlock $docBlock, \ReflectionMethod | \ReflectionClass | null $reflection = null, string $textType = 'getDescription') : string
421426
{
422427
$summary = $docBlock->{$textType}();
423428

424-
if (! $reflectionMethod)
429+
if (! $reflection || 'getSummary' == $textType)
425430
{
426431
return $summary;
427432
}
@@ -430,35 +435,54 @@ protected function getInheritedText(\phpDocumentor\Reflection\DocBlock $docBlock
430435

431436
foreach ($tags as $index => $tag)
432437
{
433-
$pos = \stripos($tag->getName(), 'inheritdoc');
434-
435-
if (false !== $pos && 0 <= $pos)
438+
if (false !== \stripos($tag->getName(), 'inheritdoc'))
436439
{
437-
$reflectionClass = $reflectionMethod->getDeclaringClass();
438-
$parent = $reflectionClass->getParentClass();
439-
440-
while ($parent)
440+
if ($reflection instanceof \ReflectionMethod)
441441
{
442-
try
443-
{
444-
$method = $parent->getMethod($reflectionMethod->name);
445-
}
446-
catch (\Throwable)
442+
$reflectionClass = $reflection->getDeclaringClass();
443+
$parent = $reflectionClass->getParentClass();
444+
445+
while ($parent)
447446
{
448-
$method = null;
447+
try
448+
{
449+
$method = $parent->getMethod($reflection->name);
450+
}
451+
catch (\Throwable)
452+
{
453+
$method = null;
454+
}
455+
456+
if ($method)
457+
{
458+
$docBlock = $this->getDocBlock($method);
459+
460+
if ($docBlock)
461+
{
462+
return $this->getInheritedText($docBlock, $method) . $summary;
463+
}
464+
}
465+
$parent = $parent->getParentClass();
449466
}
450467

451-
if ($method)
468+
break;
469+
}
470+
471+
472+
$parent = $reflection->getParentClass();
473+
474+
while ($parent)
452475
{
453-
$docBlock = $this->getDocBlock($method);
476+
$comments = $parent->getDocComment();
454477

455-
if ($docBlock)
478+
if ($comments)
456479
{
457-
return $this->getInheritedText($docBlock, $method, $textType) . $summary;
480+
$comments = \str_replace('{@inheritdoc}', '@inheritdoc', $comments);
481+
$docblock = $this->factory->create($comments);
482+
$summary = $this->formatComments($docblock, $parent) . $summary;
458483
}
484+
$parent = $parent->getParentClass();
459485
}
460-
$parent = $parent->getParentClass();
461-
}
462486

463487
break;
464488
}
@@ -469,23 +493,25 @@ protected function getInheritedText(\phpDocumentor\Reflection\DocBlock $docBlock
469493

470494
/**
471495
* @param array<int, \phpDocumentor\Reflection\DocBlock\Tag> $tags
496+
* @template T of \ReflectionClass
497+
* @param \ReflectionMethod | \ReflectionClass<T> | null $reflection
472498
*
473499
* @return array<int, \phpDocumentor\Reflection\DocBlock\Tag>
474500
*/
475-
protected function getInheritedDocBlock(array $tags, \ReflectionMethod $reflectionMethod) : array
501+
protected function getInheritedDocBlock(array $tags, \ReflectionMethod | \ReflectionClass | null $reflection) : array
476502
{
477503
foreach ($tags as $index => $tag)
478504
{
479-
if (0 >= \stripos($tag->getName(), 'inheritdoc'))
505+
if (false !== \stripos($tag->getName(), 'inheritdoc'))
480506
{
481-
$reflectionClass = $reflectionMethod->getDeclaringClass();
507+
$reflectionClass = ($reflection instanceof \ReflectionMethod) ? $reflection->getDeclaringClass() : $reflection;
482508
$parent = $reflectionClass->getParentClass();
483509

484510
while ($parent)
485511
{
486512
try
487513
{
488-
$method = $parent->getMethod($reflectionMethod->name);
514+
$method = $parent->getMethod($reflection->name);
489515
}
490516
catch (\Throwable)
491517
{

src/PHPFUI/InstaDoc/Section/Doc.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function generate(\PHPFUI\InstaDoc\PageInterface $page, string $fullClass
4747
{
4848
$docblock = $this->factory->create($comments);
4949
$callout = new \PHPFUI\Callout('secondary');
50-
$callout->add($this->formatComments($docblock));
50+
$callout->add($this->formatComments($docblock, $this->reflection));
5151
$container->add($callout);
5252
}
5353

0 commit comments

Comments
 (0)