3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Code\Reflection
;
13 use Zend\Code\Reflection\DocBlock\Tag\TagInterface
as DocBlockTagInterface
;
14 use Zend\Code\Reflection\DocBlock\TagManager
as DocBlockTagManager
;
15 use Zend\Code\Scanner\DocBlockScanner
;
17 class DocBlockReflection
implements ReflectionInterface
22 protected $reflector = null;
27 protected $docComment = null;
30 * @var DocBlockTagManager
32 protected $tagManager = null;
37 protected $startLine = null;
38 protected $endLine = null;
44 protected $cleanDocComment = null;
49 protected $longDescription = null;
54 protected $shortDescription = null;
59 protected $tags = array();
64 protected $isReflected = false;
69 * Required by the Reflector interface.
71 * @todo What should this do?
74 public static function export()
80 * @param Reflector|string $commentOrReflector
81 * @param null|DocBlockTagManager $tagManager
82 * @throws Exception\InvalidArgumentException
83 * @return DocBlockReflection
85 public function __construct($commentOrReflector, DocBlockTagManager
$tagManager = null)
87 $this->tagManager
= $tagManager ?
: new DocBlockTagManager(DocBlockTagManager
::USE_DEFAULT_PROTOTYPES
);
89 if ($commentOrReflector instanceof Reflector
) {
90 $this->reflector
= $commentOrReflector;
91 if (!method_exists($commentOrReflector, 'getDocComment')) {
92 throw new Exception\
InvalidArgumentException('Reflector must contain method "getDocComment"');
94 /* @var MethodReflection $commentOrReflector */
95 $this->docComment
= $commentOrReflector->getDocComment();
97 // determine line numbers
98 $lineCount = substr_count($this->docComment
, "\n");
99 $this->startLine
= $this->reflector
->getStartLine() - $lineCount - 1;
100 $this->endLine
= $this->reflector
->getStartLine() - 1;
102 } elseif (is_string($commentOrReflector)) {
103 $this->docComment
= $commentOrReflector;
105 throw new Exception\
InvalidArgumentException(sprintf(
106 '%s must have a (string) DocComment or a Reflector in the constructor',
111 if ($this->docComment
== '') {
112 throw new Exception\
InvalidArgumentException('DocComment cannot be empty');
119 * Retrieve contents of DocBlock
123 public function getContents()
127 return $this->cleanDocComment
;
131 * Get start line (position) of DocBlock
135 public function getStartLine()
139 return $this->startLine
;
143 * Get last line (position) of DocBlock
147 public function getEndLine()
151 return $this->endLine
;
155 * Get DocBlock short description
159 public function getShortDescription()
163 return $this->shortDescription
;
167 * Get DocBlock long description
171 public function getLongDescription()
175 return $this->longDescription
;
179 * Does the DocBlock contain the given annotation tag?
181 * @param string $name
184 public function hasTag($name)
187 foreach ($this->tags
as $tag) {
188 if ($tag->getName() == $name) {
197 * Retrieve the given DocBlock tag
199 * @param string $name
200 * @return DocBlockTagInterface|false
202 public function getTag($name)
205 foreach ($this->tags
as $tag) {
206 if ($tag->getName() == $name) {
215 * Get all DocBlock annotation tags
217 * @param string $filter
218 * @return DocBlockTagInterface[]
220 public function getTags($filter = null)
223 if ($filter === null ||
!is_string($filter)) {
227 $returnTags = array();
228 foreach ($this->tags
as $tag) {
229 if ($tag->getName() == $filter) {
230 $returnTags[] = $tag;
242 protected function reflect()
244 if ($this->isReflected
) {
248 $docComment = $this->docComment
; // localize variable
250 // create a clean docComment
251 $this->cleanDocComment
= preg_replace("#[ \t]*(?:/\*\*|\*/|\*)[ ]{0,1}(.*)?#", '$1', $docComment);
252 $this->cleanDocComment
= ltrim($this->cleanDocComment
,
253 "\r\n"); // @todo should be changed to remove first and last empty line
255 $scanner = new DocBlockScanner($docComment);
256 $this->shortDescription
= ltrim($scanner->getShortDescription());
257 $this->longDescription
= ltrim($scanner->getLongDescription());
259 foreach ($scanner->getTags() as $tag) {
260 $this->tags
[] = $this->tagManager
->createTag(ltrim($tag['name'], '@'), ltrim($tag['value']));
263 $this->isReflected
= true;
266 public function toString()
268 $str = "DocBlock [ /* DocBlock */ ] {" . PHP_EOL
. PHP_EOL
;
269 $str .= " - Tags [" . count($this->tags
) . "] {" . PHP_EOL
;
271 foreach ($this->tags
AS $tag) {
275 $str .= " }" . PHP_EOL
;
276 $str .= "}" . PHP_EOL
;
282 * Serialize to string
284 * Required by the Reflector interface
288 public function __toString()
290 return $this->toString();