Skip to content

Commit b7797b4

Browse files
committed
Added getters for Serializer options;
Swapped the indent and indent string options at Serializer's constructor; Renamed Serializer::setIndentFirstLine() to setIsFirstLineIndented() in accordance with the getter (PHPMD fix); Line length is now ACTUALLY line length, i.e. it takes the indentation into account, and is applied to tags as well; Fixed ReturnTag::setContent() to set "types" to NULL; A lot of doc and CS fixes at Serializer.php.
1 parent 8b52963 commit b7797b4

File tree

3 files changed

+163
-61
lines changed

3 files changed

+163
-61
lines changed

src/phpDocumentor/Reflection/DocBlock.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -229,24 +229,38 @@ protected function parseTags($tags)
229229
$this->tags = $result;
230230
}
231231

232-
public function getText(){
232+
/**
233+
* Gets the text portion of the doc block.
234+
*
235+
* Gets the text portion (short and long description combined) of the doc
236+
* block.
237+
*
238+
* @return string The text portion of the doc block.
239+
*/
240+
public function getText()
241+
{
233242
$short = $this->getShortDescription();
234243
$long = $this->getLongDescription()->getContents();
235244

236-
if($long){
245+
if ($long) {
237246
return $short . "\n\n" . $long;
238-
}else{
247+
} else {
239248
return $short;
240249
}
241250
}
242251

243252
/**
244-
* Set the short and long description.
253+
* Set the text portion of the doc block.
254+
*
255+
* Sets the text portion (short and long description combined) of the doc
256+
* block.
245257
*
246-
* @param string $docblock
247-
* @return $this
258+
* @param string $docblock The new text portion of the doc block.
259+
*
260+
* @return $this This doc block.
248261
*/
249-
public function setText($comment){
262+
public function setText($comment)
263+
{
250264
list($short, $long) = $this->splitDocBlock($comment);
251265
$this->short_description = $short;
252266
$this->long_description = new DocBlock\Description($long, $this);

src/phpDocumentor/Reflection/DocBlock/Serializer.php

Lines changed: 141 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
*
55
* PHP Version 5.3
66
*
7-
* @author Mike van Riel <mike.vanriel@naenius.com>
8-
* @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
7+
* @author Barry vd. Heuvel <barryvdh@gmail.com>
8+
* @copyright 2013 Mike van Riel / Naenius (http://www.naenius.com)
99
* @license http://www.opensource.org/licenses/mit-license.php MIT
1010
* @link http://phpdoc.org
1111
*/
@@ -15,7 +15,7 @@
1515
use phpDocumentor\Reflection\DocBlock;
1616

1717
/**
18-
* Serializes a DocBlock instance
18+
* Serializes a DocBlock instance.
1919
*
2020
* @author Barry vd. Heuvel <[email protected]>
2121
* @license http://www.opensource.org/licenses/mit-license.php MIT
@@ -24,98 +24,185 @@
2424
class Serializer
2525
{
2626

27-
/** @var string The string to indent the comment with */
28-
protected $indentString;
27+
/** @var string The string to indent the comment with. */
28+
protected $indentString = ' ';
2929

30-
/** @var int The number of times $indentString is repeated */
31-
protected $indent;
30+
/** @var int The number of times the indent string is repeated. */
31+
protected $indent = 0;
3232

33-
/** @var bool Indent the first line */
34-
protected $indentFirstLine;
33+
/** @var bool Whether to indent the first line. */
34+
protected $isFirstLineIndented = true;
3535

36-
/** @var int The max length of a description line. */
36+
/** @var int The max length of a line. */
3737
protected $lineLength = null;
3838

3939
/**
4040
* Create a Serializer instance.
4141
*
42-
* @param string $indentString
43-
* @param int $indent
44-
* @param bool $indentFirstLine
45-
* @internal param string $indentationString The indentation string.
42+
* @param int $indent The number of times the indent string is
43+
* repeated.
44+
* @param string $indentString The string to indent the comment with.
45+
* @param bool $indentFirstLine Whether to indent the first line.
46+
* @param int $lineLength The max length of a line.
4647
*/
47-
public function __construct($indentString = ' ', $indent = 4, $indentFirstLine = true)
48-
{
49-
$this->indentString = $indentString;
50-
$this->indent = $indent;
51-
$this->indentFirstLine = $indentFirstLine;
48+
public function __construct(
49+
$indent = 0,
50+
$indentString = ' ',
51+
$indentFirstLine = true,
52+
$lineLength = null
53+
) {
54+
$this->setIndentationString($indentString);
55+
$this->setIndent($indent);
56+
$this->setIsFirstLineIndented($indentFirstLine);
57+
$this->setLineLength($lineLength);
5258
}
5359

5460
/**
55-
* @param $indentationString
56-
* @return $this
61+
* Sets the string to indent comments with.
62+
*
63+
* @param string $indentationString The string to indent comments with.
64+
*
65+
* @return $this This serializer object.
5766
*/
58-
public function setIndentationString($indentationString)
67+
public function setIndentationString($indentString)
5968
{
60-
$this->indentation = $indentationString;
69+
$this->indentString = (string)$indentString;
6170
return $this;
6271
}
6372

6473
/**
65-
* @param $indent
66-
* @return $this
74+
* Gets the string to indent comments with.
75+
*
76+
* @return string The indent string.
6777
*/
68-
public function setIndent($indent){
69-
$this->indent = $indent;
70-
return $this;
78+
public function getIndentationString()
79+
{
80+
return $this->indentString;
7181
}
7282

7383
/**
74-
* @param $indentFirstLine
75-
* @return $this
84+
* Sets the number of indents.
85+
*
86+
* @param int $indent The number of times the indent string is repeated.
87+
*
88+
* @return $this This serializer object.
7689
*/
77-
public function setIndentFirstLine($indentFirstLine){
78-
$this->indentFirstLine = $indentFirstLine;
90+
public function setIndent($indent)
91+
{
92+
$this->indent = (int)$indent;
7993
return $this;
8094
}
8195

8296
/**
83-
* @param $lineLength
84-
* @return $this
97+
* Gets the number of indents.
98+
*
99+
* @return int The number of times the indent string is repeated.
100+
*/
101+
public function getIndent()
102+
{
103+
return $this->indent;
104+
}
105+
106+
/**
107+
* Sets whether or not the first line should be indented.
108+
*
109+
* Sets whether or not the first line (the one with the "/**") should be
110+
* indented.
111+
*
112+
* @param bool $indentFirstLine The new value for this setting.
113+
*
114+
* @return $this This serializer object.
85115
*/
86-
public function setLineLength($lineLength){
87-
$this->lineLength = $lineLength;
116+
public function setIsFirstLineIndented($indentFirstLine)
117+
{
118+
$this->isFirstLineIndented = (bool)$indentFirstLine;
88119
return $this;
89120
}
90121

91122
/**
92-
* Generate a DocBlock Comment
93-
*
94-
* @param DocBlock The DocBlock to serialize
95-
* @return string
123+
* Gets whether or not the first line should be indented.
124+
*
125+
* @return bool Whether or not the first line should be indented.
96126
*/
97-
public function getDocComment($phpdoc){
127+
public function isFirstLineIndented()
128+
{
129+
return $this->isFirstLineIndented;
130+
}
98131

99-
$indent = '';
100-
for($i=0;$i<$this->indent;$i++){
101-
$indent .= $this->indentString;
102-
}
103-
$firstIndent = $this->indentFirstLine ? $indent : '';
132+
/**
133+
* Sets the line length.
134+
*
135+
* Sets the length of each line in the serialization. Content will be
136+
* wrapped within this limit.
137+
*
138+
* @param int $lineLength The length of each line. NULL to disable line
139+
* wrapping altogether.
140+
*
141+
* @return $this This serializer object.
142+
*/
143+
public function setLineLength($lineLength)
144+
{
145+
$this->lineLength = null === $lineLength ? null : (int)$lineLength;
146+
return $this;
147+
}
148+
149+
/**
150+
* Gets the line length.
151+
*
152+
* @return int The length of each line or NULL if line wrapping is disabled.
153+
*/
154+
public function getLineLength()
155+
{
156+
return $this->lineLength;
157+
}
104158

105-
$description = $phpdoc->getText();
106-
if($this->lineLength){
107-
$description = wordwrap($description, $this->lineLength);
159+
/**
160+
* Generate a DocBlock comment.
161+
*
162+
* @param DocBlock The DocBlock to serialize.
163+
*
164+
* @return string The serialized doc block.
165+
*/
166+
public function getDocComment(DocBlock $docblock)
167+
{
168+
$indent = str_repeat($this->indentString, $this->indent);
169+
$firstIndent = $this->isFirstLineIndented ? $indent : '';
170+
171+
$text = $docblock->getText();
172+
if ($this->lineLength) {
173+
$text = wordwrap(
174+
$text,
175+
$this->lineLength - strlen($indent) - 3/*strlen(' * ')*/
176+
);
108177
}
109-
$description = str_replace("\n", "\n$indent * ", $description);
178+
$text = str_replace("\n", "\n$indent * ", $text);
110179

111-
$comment = "$firstIndent/**\n$indent * $description\n$indent *\n";
180+
$comment = "$firstIndent/**\n$indent * $text\n$indent *\n";
112181

113182
/** @var Tag $tag */
114-
foreach ($phpdoc->getTags() as $tag) {
115-
$comment .= $indent.' * @'. $tag->getName() . " " . $tag->getContent() . "\n";
183+
foreach ($docblock->getTags() as $tag) {
184+
$tagName = $tag->getName();
185+
$prefixLength = 1/*strlen('@')*/ + strlen($tagName);
186+
187+
//Added to take the first line of the tag into account.
188+
$tagContent = str_repeat(' ', $prefixLength) . $tag->getContent();
189+
190+
if ($this->lineLength) {
191+
$tagContent = wordwrap(
192+
$tagContent,
193+
$this->lineLength - strlen($indent) - 3/*strlen(' * ')*/
194+
);
195+
}
196+
197+
//Clean up the prefix.
198+
substr_replace($tagContent, '', 0, $prefixLength);
199+
200+
$tagContent = str_replace("\n", "\n$indent * ", $tagContent);
201+
202+
$comment .= "$indent * @{$tagName} {$tagContent}\n";
116203
}
117204

118-
$comment .= $indent.' */';
205+
$comment .= $indent . ' */';
119206

120207
return $comment;
121208
}

src/phpDocumentor/Reflection/DocBlock/Tag/ReturnTag.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public function setContent($content)
5353

5454
// any output is considered a type
5555
$this->type = $parts[0];
56+
$this->types = null;
5657

5758
$this->setDescription(isset($parts[1]) ? $parts[1] : '');
5859

0 commit comments

Comments
 (0)