Skip to content

feat: add separation of tags into groups #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 39 additions & 2 deletions src/Barryvdh/Reflection/DocBlock/Serializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ class Serializer
/** @var int|null The max length of a line. */
protected $lineLength = null;

/** @var bool Separate tag groups. */
protected $separateTags = false;

/**
* Create a Serializer instance.
*
Expand All @@ -45,17 +48,20 @@ class Serializer
* @param bool $indentFirstLine Whether to indent the first line.
* @param int|null $lineLength The max length of a line or NULL to
* disable line wrapping.
* @param bool $separateTags Separate tag groups.
*/
public function __construct(
$indent = 0,
$indentString = ' ',
$indentFirstLine = true,
$lineLength = null
$lineLength = null,
$separateTags = false
) {
$this->setIndentationString($indentString);
$this->setIndent($indent);
$this->setIsFirstLineIndented($indentFirstLine);
$this->setLineLength($lineLength);
$this->setSeparateTags($separateTags);
}

/**
Expand Down Expand Up @@ -158,6 +164,29 @@ public function getLineLength()
return $this->lineLength;
}

/**
* Sets whether there should be an empty line between tag groups.
*
* @param bool $separateTags The new value for this setting.
*
* @return $this This serializer object.
*/
public function setSeparateTags($separateTags)
{
$this->separateTags = (bool)$separateTags;
return $this;
}

/**
* Gets whether there should be an empty line between tag groups.
*
* @return bool Whether there should be an empty line between tag groups.
*/
public function getSeparateTags()
{
return $this->separateTags;
}

/**
* Generate a DocBlock comment.
*
Expand All @@ -180,15 +209,23 @@ public function getDocComment(DocBlock $docblock)

$comment = "{$firstIndent}/**\n{$indent} * {$text}\n{$indent} *\n";

$tags = array_values($docblock->getTags());

/** @var Tag $tag */
foreach ($docblock->getTags() as $tag) {
foreach ($tags as $key => $tag) {
$nextTag = isset($tags[$key + 1]) ? $tags[$key + 1] : null;

$tagText = (string) $tag;
if ($this->lineLength) {
$tagText = wordwrap($tagText, $wrapLength);
}
$tagText = str_replace("\n", "\n{$indent} * ", $tagText);

$comment .= "{$indent} * {$tagText}\n";

if ($this->separateTags && $nextTag !== null && ! $tag->inSameGroup($nextTag)) {
$comment .= "{$indent} *\n";
}
}

$comment .= $indent . ' */';
Expand Down
33 changes: 33 additions & 0 deletions src/Barryvdh/Reflection/DocBlock/Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,39 @@ public function setLocation(Location $location = null)
return $this;
}

/**
* If the given tags should be together or apart.
*
* @param Tag $tag
*
* @return bool
*/
public function inSameGroup(Tag $tag)
{
$firstName = $this->getName();
$secondName = $tag->getName();

if ($firstName === $secondName) {
return true;
}

$groups = array(
array('deprecated', 'link', 'see', 'since'),
array('author', 'copyright', 'license'),
array('category', 'package', 'subpackage'),
array('property', 'property-read', 'property-write'),
array('param', 'return'),
);

foreach ($groups as $group) {
if (in_array($firstName, $group, true) && in_array($secondName, $group, true)) {
return true;
}
}

return false;
}

/**
* Builds a string representation of this object.
*
Expand Down