diff -r 000000000000 -r 4eba9c11703f web/Zend/Reflection/Docblock.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/Zend/Reflection/Docblock.php Mon Dec 13 18:29:26 2010 +0100 @@ -0,0 +1,294 @@ +_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); + } +}