Skip to content

Commit 8b52963

Browse files
committed
Merge pull request #20 from barryvdh/master
Export docblock comment
2 parents 63c9de4 + 2e9fd6a commit 8b52963

File tree

2 files changed

+151
-5
lines changed

2 files changed

+151
-5
lines changed

src/phpDocumentor/Reflection/DocBlock.php

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

232+
public function getText(){
233+
$short = $this->getShortDescription();
234+
$long = $this->getLongDescription()->getContents();
235+
236+
if($long){
237+
return $short . "\n\n" . $long;
238+
}else{
239+
return $short;
240+
}
241+
}
242+
243+
/**
244+
* Set the short and long description.
245+
*
246+
* @param string $docblock
247+
* @return $this
248+
*/
249+
public function setText($comment){
250+
list($short, $long) = $this->splitDocBlock($comment);
251+
$this->short_description = $short;
252+
$this->long_description = new DocBlock\Description($long, $this);
253+
return $this;
254+
}
232255
/**
233256
* Returns the opening line or also known as short description.
234257
*
@@ -321,22 +344,22 @@ public function hasTag($name)
321344

322345
return false;
323346
}
324-
347+
325348
/**
326349
* Appends a tag at the end of the list of tags.
327-
*
350+
*
328351
* @param Tag $tag The tag to add.
329-
*
352+
*
330353
* @return Tag The newly added tag.
331-
*
354+
*
332355
* @throws \LogicException When the tag belongs to a different DocBlock.
333356
*/
334357
public function appendTag(Tag $tag)
335358
{
336359
if (null === $tag->getDocBlock()) {
337360
$tag->setDocBlock($this);
338361
}
339-
362+
340363
if ($tag->getDocBlock() === $this) {
341364
$this->tags[] = $tag;
342365
} else {
@@ -348,6 +371,7 @@ public function appendTag(Tag $tag)
348371
return $tag;
349372
}
350373

374+
351375
/**
352376
* Builds a string representation of this object.
353377
*
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)