Skip to content

Commit 78597e2

Browse files
committed
Better inheritdoc processing
1 parent 5502b7f commit 78597e2

File tree

4 files changed

+110
-26
lines changed

4 files changed

+110
-26
lines changed

src/PHPFUI/InstaDoc/Section/CodeCommon.php

Lines changed: 71 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ protected function formatComments(?\phpDocumentor\Reflection\DocBlock $docBlock,
2929

3030
$container = new \PHPFUI\Container();
3131

32-
$container->add($this->parsedown->text($docBlock->getSummary()));
32+
$container->add($this->parsedown->text($this->getInheritedSummary($docBlock, $reflectionMethod)));
3333
$desc = $docBlock->getDescription();
3434

3535
if ($desc)
@@ -57,15 +57,25 @@ protected function formatComments(?\phpDocumentor\Reflection\DocBlock $docBlock,
5757
$description = \method_exists($tag, 'getDescription') ? \trim($tag->getDescription()) : '';
5858
$body = '';
5959
// punt on useless tags
60-
if (\in_array($name, ['method', 'param', 'inheritdoc']))
60+
if (\in_array($name, ['method', 'inheritdoc']))
6161
{
6262
continue;
6363
}
6464

65-
if ('var' == $name)
65+
if (\method_exists($tag, 'getType'))
66+
{
67+
$type = $tag->getType();
68+
}
69+
else
70+
{
71+
$type = '';
72+
}
73+
74+
75+
if ('var' == $name || 'param' == $name)
6676
{
6777
// useless if no description or type
68-
if (! $description && ! $tag->getType())
78+
if (! $description && ! $type)
6979
{
7080
continue;
7181
}
@@ -91,14 +101,9 @@ protected function formatComments(?\phpDocumentor\Reflection\DocBlock $docBlock,
91101
$body .= new \PHPFUI\Link($tag->getLink(), '', false);
92102
}
93103

94-
if (\method_exists($tag, 'getType'))
104+
if ($type)
95105
{
96-
$type = $tag->getType();
97-
98-
if ($type)
99-
{
100-
$body .= $this->getClassName($type) . ' ';
101-
}
106+
$body .= $this->getClassName($type) . ' ';
102107
}
103108

104109
if (\method_exists($tag, 'getVariableName'))
@@ -116,6 +121,10 @@ protected function formatComments(?\phpDocumentor\Reflection\DocBlock $docBlock,
116121

117122
$container->add($ul);
118123
}
124+
else
125+
{
126+
$container->add('no tags<br>');
127+
}
119128

120129
return $container;
121130
}
@@ -207,6 +216,7 @@ protected function getComments(?\phpDocumentor\Reflection\DocBlock $docBlock, ?\
207216
protected function getDocBlock($method) : ?\phpDocumentor\Reflection\DocBlock
208217
{
209218
$comments = $method->getDocComment();
219+
$comments = \str_replace('{@inheritdoc}', '@inheritdoc', $comments);
210220

211221
if (! $comments)
212222
{
@@ -233,37 +243,72 @@ protected function getHtmlClass(string $class) : string
233243
return \str_replace('\\', '-', $class);
234244
}
235245

236-
protected function getInheritedDocBlock(array $tags, \ReflectionMethod $reflectionMethod) : array
246+
protected function getInheritedSummary(\phpDocumentor\Reflection\DocBlock $docBlock, ?\ReflectionMethod $reflectionMethod = null) : string
237247
{
248+
$summary = $docBlock->getSummary();
249+
if (! $reflectionMethod)
250+
{
251+
return $summary;
252+
}
253+
254+
$tags = $docBlock->getTags();
238255
foreach ($tags as $index => $tag)
239256
{
240257
if (0 == \strcasecmp($tag->getName(), 'inheritDoc'))
241258
{
242259
$reflectionClass = $reflectionMethod->getDeclaringClass();
243260
$parent = $reflectionClass->getParentClass();
244261

245-
if (! $parent)
262+
while ($parent)
246263
{
247-
return []; // no parent, at top of tree, and no tags, go figure
248-
}
249-
$method = $parent->getMethod($reflectionMethod->name);
264+
$method = $parent->getMethod($reflectionMethod->name);
265+
if ($method)
266+
{
267+
$docBlock = $this->getDocBlock($method);
250268

251-
if (! $method)
252-
{
253-
return []; // no method here, kinda strange
269+
if ($docBlock)
270+
{
271+
return $summary . "\n" . $this->getInheritedSummary($docBlock, $method);
272+
}
273+
}
274+
$parent = $parent->getParentClass();
254275
}
255-
$docBlock = $this->getDocBlock($method);
256276

257-
if ($docBlock)
277+
break;
278+
}
279+
}
280+
281+
return $summary;
282+
}
283+
284+
protected function getInheritedDocBlock(array $tags, \ReflectionMethod $reflectionMethod) : array
285+
{
286+
foreach ($tags as $index => $tag)
287+
{
288+
if (0 == \strcasecmp($tag->getName(), 'inheritDoc'))
289+
{
290+
$reflectionClass = $reflectionMethod->getDeclaringClass();
291+
$parent = $reflectionClass->getParentClass();
292+
293+
while ($parent)
258294
{
259-
// add in the new tags and check parent
260-
\array_splice($tags, $index, 1, $docBlock->getTags());
295+
$method = $parent->getMethod($reflectionMethod->name);
296+
if ($method)
297+
{
298+
$docBlock = $this->getDocBlock($method);
299+
300+
if ($docBlock)
301+
{
302+
// add in the new tags and check parent
303+
\array_splice($tags, $index, 1, $docBlock->getTags());
261304

262-
return $this->getInheritedDocBlock($tags, $method);
305+
return $this->getInheritedDocBlock($tags, $method);
306+
}
307+
}
308+
$parent = $parent->getParentClass();
263309
}
264310

265-
// Nothing at this level, but go up one and try the parent method
266-
return $this->getInheritedDocBlock($tags, $method);
311+
break;
267312
}
268313
}
269314

src/PHPFUI/InstaDoc/Section/Doc.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function generate(\PHPFUI\Instadoc\PageInterface $page, string $fullClass
3131
}
3232

3333
$comments = $this->reflection->getDocComment();
34+
$comments = \str_replace('{@inheritdoc}', '@inheritdoc', $comments);
3435

3536
if ($comments)
3637
{

src/PHPFUI/InstaDoc/Tests/Test80A.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace PHPFUI\InstaDoc\Tests;
4+
5+
/**
6+
* A test class with no functionality.
7+
*
8+
* It is just to test InstaDoc
9+
*
10+
* @author bruce (1/3/2020)
11+
*/
12+
class Test80A extends Test80
13+
{
14+
15+
}

src/PHPFUI/InstaDoc/Tests/Test80B.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace PHPFUI\InstaDoc\Tests;
4+
5+
/**
6+
* A test class with no functionality.
7+
*
8+
* It is just to test InstaDoc
9+
*
10+
* @author bruce (1/3/2020)
11+
*/
12+
class Test80B extends Test80A
13+
{
14+
/**
15+
* @inheritDoc
16+
*/
17+
protected function protected_function_no_return(?string $fred, $unknown = 3.14) : void {}
18+
19+
/**
20+
* {@inheritDoc}
21+
*/
22+
private function private_function_no_return(string | Test80 | null $fred = 'Eythel') : void {}
23+
}

0 commit comments

Comments
 (0)