Skip to content

Commit

Permalink
Fixed invalid tags break the parseDocBlock()
Browse files Browse the repository at this point in the history
  • Loading branch information
qaharmdz committed Jul 26, 2021
1 parent 319852f commit ba6e829
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 34 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
---

## [Unreleased]

### Fixed
- Invalid tags break the parseDocBlock()

## [v1.0.0] - 2021-07-25
### Added
Expand Down
2 changes: 2 additions & 0 deletions examples/Library/Acme.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
* @author Another Author <[email protected]>
* @copyright 2021 (c) Acme Corporation
* @license [https://opensource.org/licenses/MIT](https://opensource.org/licenses/MIT)
*
* @return [type] Show invalid tags as is
*/
class Acme extends ParentClass implements \Countable
{
Expand Down
88 changes: 55 additions & 33 deletions src/DocTracer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Mdz;

use phpDocumentor\Reflection\DocBlockFactory;
use phpDocumentor\Reflection\DocBlock\Tags\InvalidTag;
use Michelf\MarkdownExtra;

/**
Expand Down Expand Up @@ -375,26 +376,28 @@ protected function parseDocBlock(\Reflector $ref): array

$i = 0;
foreach ($docBlock->getTags() as $docTags) {
if ($docTags->getName() === 'param') {
$tags[$docTags->getName()][$i] = [
'name' => '@' . $docTags->getName(),
'type' => (string)$docTags->getType(),
'variable' => '$' . $docTags->getVariableName(),
'description' => $docTags->getDescription()->render(),
];
} elseif (in_array($docTags->getName(), ['var', 'return', 'throws'])) {
$tags[$docTags->getName()][$i] = [
'name' => '@' . $docTags->getName(),
'type' => (string)$docTags->getType(),
'description' => $docTags->getDescription()->render(),
];
} else {
$tags[$docTags->getName()][$i] = [
'name' => '@' . $docTags->getName(),
];
if ($docTags && !$docTags instanceof InvalidTag) {
if ($docTags->getName() === 'param') {
$tags[$docTags->getName()][$i] = [
'name' => '@' . $docTags->getName(),
'type' => (string)$docTags->getType(),
'variable' => '$' . $docTags->getVariableName(),
'description' => $docTags->getDescription()->render(),
];
} elseif (in_array($docTags->getName(), ['var', 'return', 'throws'])) {
$tags[$docTags->getName()][$i] = [
'name' => '@' . $docTags->getName(),
'type' => (string)$docTags->getType(),
'description' => $docTags->getDescription()->render(),
];
} else {
$tags[$docTags->getName()][$i] = [
'name' => '@' . $docTags->getName(),
];
}
}

$tags[$docTags->getName()][$i]['render'] = trim(str_replace('@' . $docTags->getName(), '', $docTags->render()));
$tags[$docTags->getName()][$i]['render'] = trim($docTags->render());

$i++;
}
Expand Down Expand Up @@ -469,23 +472,42 @@ protected function formatDocBlock(array $docBlock): string

foreach ($tags as $tag) {
if ($name === 'param') {
$docs .= '<tr>';
$docs .= '<td class="dt-doc-tag-name">' . $tag['name'] . '</td>';
$docs .= '<td class="dt-doc-tag-type">' . $this->wordbreak($tag['type']) . '</td>';
$docs .= '<td class="dt-doc-tag-variable">' . $tag['variable'] . '</td>';
$docs .= '<td class="dt-doc-tag-description">' . $this->markdown($tag['description']) . '</td>';
$docs .= '</tr>';
if (isset($tag['name'])) {
$docs .= '<tr>';
$docs .= '<td class="dt-doc-tag-name">' . $tag['name'] . '</td>';
$docs .= '<td class="dt-doc-tag-type">' . $this->wordbreak($tag['type']) . '</td>';
$docs .= '<td class="dt-doc-tag-variable">' . $tag['variable'] . '</td>';
$docs .= '<td class="dt-doc-tag-description">' . $this->markdown($tag['description']) . '</td>';
$docs .= '</tr>';
} else {
$docs .= '<tr>';
$docs .= '<td class="dt-doc-tag-render" colspan="4">' . $this->markdown($tag['render']) . '</td>';
$docs .= '</tr>';
}
} elseif (in_array($name, ['var', 'return', 'throws'])) {
$docs .= '<tr>';
$docs .= '<td class="dt-doc-tag-name">' . $tag['name'] . '</td>';
$docs .= '<td class="dt-doc-tag-type">' . $this->wordbreak($tag['type']) . '</td>';
$docs .= '<td class="dt-doc-tag-description">' . $this->markdown($tag['description']) . '</td>';
$docs .= '</tr>';
if (isset($tag['name'])) {
$docs .= '<tr>';
$docs .= '<td class="dt-doc-tag-name">' . $tag['name'] . '</td>';
$docs .= '<td class="dt-doc-tag-type">' . $this->wordbreak($tag['type']) . '</td>';
$docs .= '<td class="dt-doc-tag-description">' . $this->markdown($tag['description']) . '</td>';
$docs .= '</tr>';
} else {
$docs .= '<tr>';
$docs .= '<td class="dt-doc-tag-render" colspan="3">' . $this->markdown($tag['render']) . '</td>';
$docs .= '</tr>';
}
} else {
$docs .= '<tr>';
$docs .= '<td class="dt-doc-tag-name">' . $tag['name'] . '</td>';
$docs .= '<td class="dt-doc-tag-render">' . $this->markdown($tag['render']) . '</td>';
$docs .= '</tr>';
if (isset($tag['name'])) {
$render = str_replace('@' . $tag['name'], '', $tag['render']);
$docs .= '<tr>';
$docs .= '<td class="dt-doc-tag-name">' . $tag['name'] . '</td>';
$docs .= '<td class="dt-doc-tag-render">' . $this->markdown($render) . '</td>';
$docs .= '</tr>';
} else {
$docs .= '<tr>';
$docs .= '<td class="dt-doc-tag-render" colspan="2">' . $this->markdown($tag['render']) . '</td>';
$docs .= '</tr>';
}
}
}
$docs .= '</table>';
Expand Down

0 comments on commit ba6e829

Please sign in to comment.