Docblock.php
7.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Reflection
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id: Docblock.php 23775 2011-03-01 17:25:24Z ralph $
*/
/**
* @see Zend_Reflection_Docblock_Tag
*/
require_once 'Zend/Reflection/Docblock/Tag.php';
/**
* @category Zend
* @package Zend_Reflection
* @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Reflection_Docblock implements Reflector
{
/**
* @var Reflector
*/
protected $_reflector = null;
/**#@+
* @var int
*/
protected $_startLine = null;
protected $_endLine = null;
/**#@-*/
/**
* @var string
*/
protected $_docComment = null;
/**
* @var string
*/
protected $_cleanDocComment = null;
/**
* @var string
*/
protected $_longDescription = null;
/**
* @var string
*/
protected $_shortDescription = null;
/**
* @var array
*/
protected $_tags = array();
/**
* Export reflection
*
* Reqired by the Reflector interface.
*
* @todo What should this do?
* @return void
*/
public static function export()
{
}
/**
* Serialize to string
*
* Required by the Reflector interface
*
* @todo What should this return?
* @return string
*/
public function __toString()
{
$str = "Docblock [ /* Docblock */ ] {".PHP_EOL.PHP_EOL;
$str .= " - Tags [".count($this->_tags)."] {".PHP_EOL;
foreach($this->_tags AS $tag) {
$str .= " ".$tag;
}
$str .= " }".PHP_EOL;
$str .= "}".PHP_EOL;
return $str;
}
/**
* Constructor
*
* @param Reflector|string $commentOrReflector
*/
public function __construct($commentOrReflector)
{
if ($commentOrReflector instanceof Reflector) {
$this->_reflector = $commentOrReflector;
if (!method_exists($commentOrReflector, 'getDocComment')) {
require_once 'Zend/Reflection/Exception.php';
throw new Zend_Reflection_Exception('Reflector must contain method "getDocComment"');
}
$docComment = $commentOrReflector->getDocComment();
$lineCount = substr_count($docComment, "\n");
$this->_startLine = $this->_reflector->getStartLine() - $lineCount - 1;
$this->_endLine = $this->_reflector->getStartLine() - 1;
} elseif (is_string($commentOrReflector)) {
$docComment = $commentOrReflector;
} else {
require_once 'Zend/Reflection/Exception.php';
throw new Zend_Reflection_Exception(get_class($this) . ' must have a (string) DocComment or a Reflector in the constructor');
}
if ($docComment == '') {
require_once 'Zend/Reflection/Exception.php';
throw new Zend_Reflection_Exception('DocComment cannot be empty');
}
$this->_docComment = $docComment;
$this->_parse();
}
/**
* Retrieve contents of docblock
*
* @return string
*/
public function getContents()
{
return $this->_cleanDocComment;
}
/**
* Get start line (position) of docblock
*
* @return int
*/
public function getStartLine()
{
return $this->_startLine;
}
/**
* Get last line (position) of docblock
*
* @return int
*/
public function getEndLine()
{
return $this->_endLine;
}
/**
* Get docblock short description
*
* @return string
*/
public function getShortDescription()
{
return $this->_shortDescription;
}
/**
* Get docblock long description
*
* @return string
*/
public function getLongDescription()
{
return $this->_longDescription;
}
/**
* Does the docblock contain the given annotation tag?
*
* @param string $name
* @return bool
*/
public function hasTag($name)
{
foreach ($this->_tags as $tag) {
if ($tag->getName() == $name) {
return true;
}
}
return false;
}
/**
* Retrieve the given docblock tag
*
* @param string $name
* @return Zend_Reflection_Docblock_Tag|false
*/
public function getTag($name)
{
foreach ($this->_tags as $tag) {
if ($tag->getName() == $name) {
return $tag;
}
}
return false;
}
/**
* Get all docblock annotation tags
*
* @param string $filter
* @return array Array of Zend_Reflection_Docblock_Tag
*/
public function getTags($filter = null)
{
if ($filter === null || !is_string($filter)) {
return $this->_tags;
}
$returnTags = array();
foreach ($this->_tags as $tag) {
if ($tag->getName() == $filter) {
$returnTags[] = $tag;
}
}
return $returnTags;
}
/**
* Parse the docblock
*
* @return void
*/
protected function _parse()
{
$docComment = $this->_docComment;
// First remove doc block line starters
$docComment = preg_replace('#[ \t]*(?:\/\*\*|\*\/|\*)?[ ]{0,1}(.*)?#', '$1', $docComment);
$docComment = ltrim($docComment, "\r\n"); // @todo should be changed to remove first and last empty line
$this->_cleanDocComment = $docComment;
// Next parse out the tags and descriptions
$parsedDocComment = $docComment;
$lineNumber = $firstBlandLineEncountered = 0;
while (($newlinePos = strpos($parsedDocComment, "\n")) !== false) {
$lineNumber++;
$line = substr($parsedDocComment, 0, $newlinePos);
$matches = array();
if ((strpos($line, '@') === 0) && (preg_match('#^(@\w+.*?)(\n)(?:@|\r?\n|$)#s', $parsedDocComment, $matches))) {
$this->_tags[] = Zend_Reflection_Docblock_Tag::factory($matches[1]);
$parsedDocComment = str_replace($matches[1] . $matches[2], '', $parsedDocComment);
} else {
if ($lineNumber < 3 && !$firstBlandLineEncountered) {
$this->_shortDescription .= $line . "\n";
} else {
$this->_longDescription .= $line . "\n";
}
if ($line == '') {
$firstBlandLineEncountered = true;
}
$parsedDocComment = substr($parsedDocComment, $newlinePos + 1);
}
}
$this->_shortDescription = rtrim($this->_shortDescription);
$this->_longDescription = rtrim($this->_longDescription);
}
}