7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
16 * @package Zend_Reflection
17 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
23 * @see Zend_Reflection_Docblock_Tag
25 require_once 'Zend/Reflection/Docblock/Tag.php';
29 * @package Zend_Reflection
30 * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
31 * @license http://framework.zend.com/license/new-bsd New BSD License
33 class Zend_Reflection_Docblock
implements Reflector
38 protected $_reflector = null;
43 protected $_startLine = null;
44 protected $_endLine = null;
50 protected $_docComment = null;
55 protected $_cleanDocComment = null;
60 protected $_longDescription = null;
65 protected $_shortDescription = null;
70 protected $_tags = array();
75 * Reqired by the Reflector interface.
77 * @todo What should this do?
80 public static function export()
88 * Required by the Reflector interface
90 * @todo What should this return?
93 public function __toString()
95 $str = "Docblock [ /* Docblock */ ] {".PHP_EOL
.PHP_EOL
;
96 $str .= " - Tags [".count($this->_tags
)."] {".PHP_EOL
;
98 foreach($this->_tags
AS $tag) {
102 $str .= " }".PHP_EOL
;
111 * @param Reflector|string $commentOrReflector
113 public function __construct($commentOrReflector)
115 if ($commentOrReflector instanceof Reflector
) {
116 $this->_reflector
= $commentOrReflector;
117 if (!method_exists($commentOrReflector, 'getDocComment')) {
118 require_once 'Zend/Reflection/Exception.php';
119 throw new Zend_Reflection_Exception('Reflector must contain method "getDocComment"');
121 $docComment = $commentOrReflector->getDocComment();
123 $lineCount = substr_count($docComment, "\n");
125 $this->_startLine
= $this->_reflector
->getStartLine() - $lineCount - 1;
126 $this->_endLine
= $this->_reflector
->getStartLine() - 1;
128 } elseif (is_string($commentOrReflector)) {
129 $docComment = $commentOrReflector;
131 require_once 'Zend/Reflection/Exception.php';
132 throw new Zend_Reflection_Exception(get_class($this) . ' must have a (string) DocComment or a Reflector in the constructor');
135 if ($docComment == '') {
136 require_once 'Zend/Reflection/Exception.php';
137 throw new Zend_Reflection_Exception('DocComment cannot be empty');
140 $this->_docComment
= $docComment;
145 * Retrieve contents of docblock
149 public function getContents()
151 return $this->_cleanDocComment
;
155 * Get start line (position) of docblock
159 public function getStartLine()
161 return $this->_startLine
;
165 * Get last line (position) of docblock
169 public function getEndLine()
171 return $this->_endLine
;
175 * Get docblock short description
179 public function getShortDescription()
181 return $this->_shortDescription
;
185 * Get docblock long description
189 public function getLongDescription()
191 return $this->_longDescription
;
195 * Does the docblock contain the given annotation tag?
197 * @param string $name
200 public function hasTag($name)
202 foreach ($this->_tags
as $tag) {
203 if ($tag->getName() == $name) {
211 * Retrieve the given docblock tag
213 * @param string $name
214 * @return Zend_Reflection_Docblock_Tag|false
216 public function getTag($name)
218 foreach ($this->_tags
as $tag) {
219 if ($tag->getName() == $name) {
228 * Get all docblock annotation tags
230 * @param string $filter
231 * @return array Array of Zend_Reflection_Docblock_Tag
233 public function getTags($filter = null)
235 if ($filter === null ||
!is_string($filter)) {
239 $returnTags = array();
240 foreach ($this->_tags
as $tag) {
241 if ($tag->getName() == $filter) {
242 $returnTags[] = $tag;
253 protected function _parse()
255 $docComment = $this->_docComment
;
257 // First remove doc block line starters
258 $docComment = preg_replace('#[ \t]*(?:\/\*\*|\*\/|\*)?[ ]{0,1}(.*)?#', '$1', $docComment);
259 $docComment = ltrim($docComment, "\r\n"); // @todo should be changed to remove first and last empty line
261 $this->_cleanDocComment
= $docComment;
263 // Next parse out the tags and descriptions
264 $parsedDocComment = $docComment;
265 $lineNumber = $firstBlandLineEncountered = 0;
266 while (($newlinePos = strpos($parsedDocComment, "\n")) !== false) {
268 $line = substr($parsedDocComment, 0, $newlinePos);
272 if ((strpos($line, '@') === 0) && (preg_match('#^(@\w+.*?)(\n)(?:@|\r?\n|$)#s', $parsedDocComment, $matches))) {
273 $this->_tags
[] = Zend_Reflection_Docblock_Tag
::factory($matches[1]);
274 $parsedDocComment = str_replace($matches[1] . $matches[2], '', $parsedDocComment);
276 if ($lineNumber < 3 && !$firstBlandLineEncountered) {
277 $this->_shortDescription
.= $line . "\n";
279 $this->_longDescription
.= $line . "\n";
283 $firstBlandLineEncountered = true;
286 $parsedDocComment = substr($parsedDocComment, $newlinePos +
1);
291 $this->_shortDescription
= rtrim($this->_shortDescription
);
292 $this->_longDescription
= rtrim($this->_longDescription
);