Skip to content

Commit 3f6ec1c

Browse files
authored
Merge pull request #8 from Kerigard/feat/separate-tags
feat: add separation of tags into groups
2 parents c10c41e + ef9f04d commit 3f6ec1c

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

src/Barryvdh/Reflection/DocBlock/Serializer.php

+39-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ class Serializer
3636
/** @var int|null The max length of a line. */
3737
protected $lineLength = null;
3838

39+
/** @var bool Separate tag groups. */
40+
protected $separateTags = false;
41+
3942
/**
4043
* Create a Serializer instance.
4144
*
@@ -45,17 +48,20 @@ class Serializer
4548
* @param bool $indentFirstLine Whether to indent the first line.
4649
* @param int|null $lineLength The max length of a line or NULL to
4750
* disable line wrapping.
51+
* @param bool $separateTags Separate tag groups.
4852
*/
4953
public function __construct(
5054
$indent = 0,
5155
$indentString = ' ',
5256
$indentFirstLine = true,
53-
$lineLength = null
57+
$lineLength = null,
58+
$separateTags = false
5459
) {
5560
$this->setIndentationString($indentString);
5661
$this->setIndent($indent);
5762
$this->setIsFirstLineIndented($indentFirstLine);
5863
$this->setLineLength($lineLength);
64+
$this->setSeparateTags($separateTags);
5965
}
6066

6167
/**
@@ -158,6 +164,29 @@ public function getLineLength()
158164
return $this->lineLength;
159165
}
160166

167+
/**
168+
* Sets whether there should be an empty line between tag groups.
169+
*
170+
* @param bool $separateTags The new value for this setting.
171+
*
172+
* @return $this This serializer object.
173+
*/
174+
public function setSeparateTags($separateTags)
175+
{
176+
$this->separateTags = (bool)$separateTags;
177+
return $this;
178+
}
179+
180+
/**
181+
* Gets whether there should be an empty line between tag groups.
182+
*
183+
* @return bool Whether there should be an empty line between tag groups.
184+
*/
185+
public function getSeparateTags()
186+
{
187+
return $this->separateTags;
188+
}
189+
161190
/**
162191
* Generate a DocBlock comment.
163192
*
@@ -180,15 +209,23 @@ public function getDocComment(DocBlock $docblock)
180209

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

212+
$tags = array_values($docblock->getTags());
213+
183214
/** @var Tag $tag */
184-
foreach ($docblock->getTags() as $tag) {
215+
foreach ($tags as $key => $tag) {
216+
$nextTag = isset($tags[$key + 1]) ? $tags[$key + 1] : null;
217+
185218
$tagText = (string) $tag;
186219
if ($this->lineLength) {
187220
$tagText = wordwrap($tagText, $wrapLength);
188221
}
189222
$tagText = str_replace("\n", "\n{$indent} * ", $tagText);
190223

191224
$comment .= "{$indent} * {$tagText}\n";
225+
226+
if ($this->separateTags && $nextTag !== null && ! $tag->inSameGroup($nextTag)) {
227+
$comment .= "{$indent} *\n";
228+
}
192229
}
193230

194231
$comment .= $indent . ' */';

src/Barryvdh/Reflection/DocBlock/Tag.php

+33
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,39 @@ public function setLocation(Location $location = null)
352352
return $this;
353353
}
354354

355+
/**
356+
* If the given tags should be together or apart.
357+
*
358+
* @param Tag $tag
359+
*
360+
* @return bool
361+
*/
362+
public function inSameGroup(Tag $tag)
363+
{
364+
$firstName = $this->getName();
365+
$secondName = $tag->getName();
366+
367+
if ($firstName === $secondName) {
368+
return true;
369+
}
370+
371+
$groups = array(
372+
array('deprecated', 'link', 'see', 'since'),
373+
array('author', 'copyright', 'license'),
374+
array('category', 'package', 'subpackage'),
375+
array('property', 'property-read', 'property-write'),
376+
array('param', 'return'),
377+
);
378+
379+
foreach ($groups as $group) {
380+
if (in_array($firstName, $group, true) && in_array($secondName, $group, true)) {
381+
return true;
382+
}
383+
}
384+
385+
return false;
386+
}
387+
355388
/**
356389
* Builds a string representation of this object.
357390
*

0 commit comments

Comments
 (0)