Merge pull request #8 from Kerigard/feat/separate-tags

feat: add separation of tags into groups
This commit is contained in:
Barry vd. Heuvel
2022-10-31 16:06:25 +01:00
committed by GitHub
2 changed files with 72 additions and 2 deletions
@@ -36,6 +36,9 @@ class Serializer
/** @var int|null The max length of a line. */ /** @var int|null The max length of a line. */
protected $lineLength = null; protected $lineLength = null;
/** @var bool Separate tag groups. */
protected $separateTags = false;
/** /**
* Create a Serializer instance. * Create a Serializer instance.
* *
@@ -45,17 +48,20 @@ class Serializer
* @param bool $indentFirstLine Whether to indent the first line. * @param bool $indentFirstLine Whether to indent the first line.
* @param int|null $lineLength The max length of a line or NULL to * @param int|null $lineLength The max length of a line or NULL to
* disable line wrapping. * disable line wrapping.
* @param bool $separateTags Separate tag groups.
*/ */
public function __construct( public function __construct(
$indent = 0, $indent = 0,
$indentString = ' ', $indentString = ' ',
$indentFirstLine = true, $indentFirstLine = true,
$lineLength = null $lineLength = null,
$separateTags = false
) { ) {
$this->setIndentationString($indentString); $this->setIndentationString($indentString);
$this->setIndent($indent); $this->setIndent($indent);
$this->setIsFirstLineIndented($indentFirstLine); $this->setIsFirstLineIndented($indentFirstLine);
$this->setLineLength($lineLength); $this->setLineLength($lineLength);
$this->setSeparateTags($separateTags);
} }
/** /**
@@ -158,6 +164,29 @@ class Serializer
return $this->lineLength; 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. * Generate a DocBlock comment.
* *
@@ -180,8 +209,12 @@ class Serializer
$comment = "{$firstIndent}/**\n{$indent} * {$text}\n{$indent} *\n"; $comment = "{$firstIndent}/**\n{$indent} * {$text}\n{$indent} *\n";
$tags = array_values($docblock->getTags());
/** @var Tag $tag */ /** @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; $tagText = (string) $tag;
if ($this->lineLength) { if ($this->lineLength) {
$tagText = wordwrap($tagText, $wrapLength); $tagText = wordwrap($tagText, $wrapLength);
@@ -189,6 +222,10 @@ class Serializer
$tagText = str_replace("\n", "\n{$indent} * ", $tagText); $tagText = str_replace("\n", "\n{$indent} * ", $tagText);
$comment .= "{$indent} * {$tagText}\n"; $comment .= "{$indent} * {$tagText}\n";
if ($this->separateTags && $nextTag !== null && ! $tag->inSameGroup($nextTag)) {
$comment .= "{$indent} *\n";
}
} }
$comment .= $indent . ' */'; $comment .= $indent . ' */';
+33
View File
@@ -352,6 +352,39 @@ class Tag implements \Reflector
return $this; 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. * Builds a string representation of this object.
* *