|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of phpDocumentor. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright 2010-2015 Mike van Riel<[email protected]> |
|
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
|
|
|
use Webmozart\Assert\Assert; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Converts a DocBlock back from an object to a complete DocComment including Asterisks. |
|
20
|
|
|
*/ |
|
21
|
|
|
class Serializer |
|
22
|
|
|
{ |
|
23
|
|
|
/** @var string The string to indent the comment with. */ |
|
24
|
|
|
protected $indentString = ' '; |
|
25
|
|
|
|
|
26
|
|
|
/** @var int The number of times the indent string is repeated. */ |
|
27
|
|
|
protected $indent = 0; |
|
28
|
|
|
|
|
29
|
|
|
/** @var bool Whether to indent the first line with the given indent amount and string. */ |
|
30
|
|
|
protected $isFirstLineIndented = true; |
|
31
|
|
|
|
|
32
|
|
|
/** @var int|null The max length of a line. */ |
|
33
|
|
|
protected $lineLength = null; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Create a Serializer instance. |
|
37
|
|
|
* |
|
38
|
|
|
* @param int $indent The number of times the indent string is repeated. |
|
39
|
|
|
* @param string $indentString The string to indent the comment with. |
|
40
|
|
|
* @param bool $indentFirstLine Whether to indent the first line. |
|
41
|
|
|
* @param int|null $lineLength The max length of a line or NULL to disable line wrapping. |
|
42
|
|
|
*/ |
|
43
|
8 |
|
public function __construct($indent = 0, $indentString = ' ', $indentFirstLine = true, $lineLength = null) |
|
44
|
|
|
{ |
|
45
|
8 |
|
Assert::integer($indent); |
|
46
|
7 |
|
Assert::string($indentString); |
|
47
|
6 |
|
Assert::boolean($indentFirstLine); |
|
48
|
5 |
|
Assert::nullOrInteger($lineLength); |
|
49
|
|
|
|
|
50
|
4 |
|
$this->indent = $indent; |
|
51
|
4 |
|
$this->indentString = $indentString; |
|
52
|
4 |
|
$this->isFirstLineIndented = $indentFirstLine; |
|
53
|
4 |
|
$this->lineLength = $lineLength; |
|
54
|
4 |
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Generate a DocBlock comment. |
|
58
|
|
|
* |
|
59
|
|
|
* @param DocBlock $docblock The DocBlock to serialize. |
|
60
|
|
|
* |
|
61
|
|
|
* @return string The serialized doc block. |
|
62
|
|
|
*/ |
|
63
|
4 |
|
public function getDocComment(DocBlock $docblock) |
|
64
|
|
|
{ |
|
65
|
4 |
|
$indent = str_repeat($this->indentString, $this->indent); |
|
66
|
4 |
|
$firstIndent = $this->isFirstLineIndented ? $indent : ''; |
|
67
|
|
|
// 3 === strlen(' * ') |
|
68
|
4 |
|
$wrapLength = $this->lineLength ? $this->lineLength - strlen($indent) - 3 : null; |
|
69
|
|
|
|
|
70
|
4 |
|
$text = $this->removeTrailingSpaces( |
|
71
|
|
|
$indent, |
|
72
|
4 |
|
$this->addAsterisksForEachLine( |
|
73
|
|
|
$indent, |
|
74
|
4 |
|
$this->getSummaryAndDescriptionTextBlock($docblock, $wrapLength) |
|
75
|
|
|
) |
|
76
|
|
|
); |
|
77
|
|
|
|
|
78
|
4 |
|
$comment = "{$firstIndent}/**\n{$indent} * {$text}\n{$indent} *\n"; |
|
79
|
4 |
|
$comment = $this->addTagBlock($docblock, $wrapLength, $indent, $comment); |
|
80
|
4 |
|
$comment .= $indent . ' */'; |
|
81
|
|
|
|
|
82
|
4 |
|
return $comment; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param $indent |
|
87
|
|
|
* @param $text |
|
88
|
|
|
* @return mixed |
|
89
|
|
|
*/ |
|
90
|
4 |
|
private function removeTrailingSpaces($indent, $text) |
|
91
|
|
|
{ |
|
92
|
4 |
|
return str_replace("\n{$indent} * \n", "\n{$indent} *\n", $text); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @param $indent |
|
97
|
|
|
* @param $text |
|
98
|
|
|
* @return mixed |
|
99
|
|
|
*/ |
|
100
|
4 |
|
private function addAsterisksForEachLine($indent, $text) |
|
101
|
|
|
{ |
|
102
|
4 |
|
return str_replace("\n", "\n{$indent} * ", $text); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* @param DocBlock $docblock |
|
107
|
|
|
* @param $wrapLength |
|
108
|
|
|
* @return string |
|
109
|
|
|
*/ |
|
110
|
4 |
|
private function getSummaryAndDescriptionTextBlock(DocBlock $docblock, $wrapLength) |
|
111
|
|
|
{ |
|
112
|
4 |
|
$text = $docblock->getSummary() . ((string)$docblock->getDescription() ? "\n\n" . $docblock->getDescription() |
|
113
|
4 |
|
: ''); |
|
114
|
4 |
|
if ($wrapLength !== null) { |
|
115
|
1 |
|
$text = wordwrap($text, $wrapLength); |
|
116
|
1 |
|
return $text; |
|
117
|
|
|
} |
|
118
|
3 |
|
return $text; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param DocBlock $docblock |
|
123
|
|
|
* @param $wrapLength |
|
124
|
|
|
* @param $indent |
|
125
|
|
|
* @param $comment |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
4 |
|
private function addTagBlock(DocBlock $docblock, $wrapLength, $indent, $comment) |
|
129
|
|
|
{ |
|
130
|
4 |
|
foreach ($docblock->getTags() as $tag) { |
|
131
|
4 |
|
$formatter = new DocBlock\Tags\Formatter\PassthroughFormatter(); |
|
132
|
4 |
|
$tagText = $formatter->format($tag); |
|
133
|
4 |
|
if ($wrapLength !== null) { |
|
134
|
1 |
|
$tagText = wordwrap($tagText, $wrapLength); |
|
135
|
|
|
} |
|
136
|
4 |
|
$tagText = str_replace("\n", "\n{$indent} * ", $tagText); |
|
137
|
|
|
|
|
138
|
4 |
|
$comment .= "{$indent} * {$tagText}\n"; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
4 |
|
return $comment; |
|
142
|
|
|
} |
|
143
|
|
|
} |
|
144
|
|
|
|