Skip to content

Commit cbb14ba

Browse files
committed
Move serializer to seperate class
1 parent d57128e commit cbb14ba

File tree

2 files changed

+132
-30
lines changed

2 files changed

+132
-30
lines changed

src/phpDocumentor/Reflection/DocBlock.php

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

232-
public function getDescription(){
232+
public function getText(){
233233
$short = $this->getShortDescription();
234234
$long = $this->getLongDescription()->getContents();
235235

236236
if($long){
237-
return $short . "\n" . $long;
237+
return $short . "\n\n" . $long;
238238
}else{
239239
return $short;
240240
}
@@ -243,11 +243,11 @@ public function getDescription(){
243243
/**
244244
* Set the short and long description.
245245
*
246-
* @param $docblock
246+
* @param string $docblock
247247
* @return $this
248248
*/
249-
public function setDescription($docblock){
250-
list($short, $long) = $this->splitDocBlock($docblock);
249+
public function setText($comment){
250+
list($short, $long) = $this->splitDocBlock($comment);
251251
$this->short_description = $short;
252252
$this->long_description = new DocBlock\Description($long, $this);
253253
return $this;
@@ -344,22 +344,22 @@ public function hasTag($name)
344344

345345
return false;
346346
}
347-
347+
348348
/**
349349
* Appends a tag at the end of the list of tags.
350-
*
350+
*
351351
* @param Tag $tag The tag to add.
352-
*
352+
*
353353
* @return Tag The newly added tag.
354-
*
354+
*
355355
* @throws \LogicException When the tag belongs to a different DocBlock.
356356
*/
357357
public function appendTag(Tag $tag)
358358
{
359359
if (null === $tag->getDocBlock()) {
360360
$tag->setDocBlock($this);
361361
}
362-
362+
363363
if ($tag->getDocBlock() === $this) {
364364
$this->tags[] = $tag;
365365
} else {
@@ -371,26 +371,6 @@ public function appendTag(Tag $tag)
371371
return $tag;
372372
}
373373

374-
/**
375-
* Generate a DocBlock Comment
376-
*
377-
* @return string
378-
*/
379-
public function getDocComment($indentation = ''){
380-
381-
$description = str_replace("\n", "\n$indentation * ", $this->getDescription());
382-
383-
$comment = "$indentation/**\n$indentation * $description\n$indentation *\n";
384-
385-
/** @var Tag $tag */
386-
foreach ($this->getTags() as $tag) {
387-
$comment .= $indentation.' * @'. $tag->getName() . " " . $tag->getContent() . "\n";
388-
}
389-
390-
$comment .= $indentation.' */';
391-
392-
return $comment;
393-
}
394374

395375
/**
396376
* Builds a string representation of this object.
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
<?php
2+
/**
3+
* phpDocumentor
4+
*
5+
* PHP Version 5.3
6+
*
7+
* @author Mike van Riel <[email protected]>
8+
* @copyright 2010-2011 Mike van Riel / Naenius (http://www.naenius.com)
9+
* @license http://www.opensource.org/licenses/mit-license.php MIT
10+
* @link http://phpdoc.org
11+
*/
12+
13+
namespace phpDocumentor\Reflection\DocBlock;
14+
15+
use phpDocumentor\Reflection\DocBlock;
16+
17+
/**
18+
* Serializes a DocBlock instance
19+
*
20+
* @author Barry vd. Heuvel <[email protected]>
21+
* @license http://www.opensource.org/licenses/mit-license.php MIT
22+
* @link http://phpdoc.org
23+
*/
24+
class Serializer
25+
{
26+
27+
/** @var string The string to indent the comment with */
28+
protected $indentString;
29+
30+
/** @var int The number of times $indentString is repeated */
31+
protected $indent;
32+
33+
/** @var bool Indent the first line */
34+
protected $indentFirstLine;
35+
36+
/** @var int The max length of a description line. */
37+
protected $lineLength = null;
38+
39+
/**
40+
* Create a Serializer instance.
41+
*
42+
* @param string $indentString
43+
* @param int $indent
44+
* @param bool $indentFirstLine
45+
* @internal param string $indentationString The indentation string.
46+
*/
47+
public function __construct($indentString = ' ', $indent = 4, $indentFirstLine = true)
48+
{
49+
$this->indentString = $indentString;
50+
$this->indent = $indent;
51+
$this->indentFirstLine = $indentFirstLine;
52+
}
53+
54+
/**
55+
* @param $indentationString
56+
* @return $this
57+
*/
58+
public function setIndentationString($indentationString)
59+
{
60+
$this->indentation = $indentationString;
61+
return $this;
62+
}
63+
64+
/**
65+
* @param $indent
66+
* @return $this
67+
*/
68+
public function setIndent($indent){
69+
$this->indent = $indent;
70+
return $this;
71+
}
72+
73+
/**
74+
* @param $indentFirstLine
75+
* @return $this
76+
*/
77+
public function setIndentFirstLine($indentFirstLine){
78+
$this->indentFirstLine = $indentFirstLine;
79+
return $this;
80+
}
81+
82+
/**
83+
* @param $lineLength
84+
* @return $this
85+
*/
86+
public function setLineLength($lineLength){
87+
$this->lineLength = $lineLength;
88+
return $this;
89+
}
90+
91+
/**
92+
* Generate a DocBlock Comment
93+
*
94+
* @param DocBlock The DocBlock to serialize
95+
* @return string
96+
*/
97+
public function getDocComment($phpdoc){
98+
99+
$indent = '';
100+
for($i=0;$i<$this->indent;$i++){
101+
$indent .= $this->indentString;
102+
}
103+
$firstIndent = $this->indentFirstLine ? $indent : '';
104+
105+
$description = $phpdoc->getText();
106+
if($this->lineLength){
107+
$description = wordwrap($description, $this->lineLength);
108+
}
109+
$description = str_replace("\n", "\n$indent * ", $description);
110+
111+
$comment = "$firstIndent/**\n$indent * $description\n$indent *\n";
112+
113+
/** @var Tag $tag */
114+
foreach ($phpdoc->getTags() as $tag) {
115+
$comment .= $indent.' * @'. $tag->getName() . " " . $tag->getContent() . "\n";
116+
}
117+
118+
$comment .= $indent.' */';
119+
120+
return $comment;
121+
}
122+
}

0 commit comments

Comments
 (0)