Added the zend framework 2 library, the path is specified in line no.26 in zend_modul...
[openemr.git] / interface / modules / zend_modules / library / Zend / Code / Reflection / DocBlockReflection.php
blob3b2257e927f2380843491d42633784b6f0c606ea
1 <?php
2 /**
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
8 */
10 namespace Zend\Code\Reflection;
12 use Reflector;
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
19 /**
20 * @var Reflector
22 protected $reflector = null;
24 /**
25 * @var string
27 protected $docComment = null;
29 /**
30 * @var DocBlockTagManager
32 protected $tagManager = null;
34 /**#@+
35 * @var int
37 protected $startLine = null;
38 protected $endLine = null;
39 /**#@-*/
41 /**
42 * @var string
44 protected $cleanDocComment = null;
46 /**
47 * @var string
49 protected $longDescription = null;
51 /**
52 * @var string
54 protected $shortDescription = null;
56 /**
57 * @var array
59 protected $tags = array();
61 /**
62 * @var bool
64 protected $isReflected = false;
66 /**
67 * Export reflection
69 * Required by the Reflector interface.
71 * @todo What should this do?
72 * @return void
74 public static function export()
79 /**
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;
104 } else {
105 throw new Exception\InvalidArgumentException(sprintf(
106 '%s must have a (string) DocComment or a Reflector in the constructor',
107 get_class($this)
111 if ($this->docComment == '') {
112 throw new Exception\InvalidArgumentException('DocComment cannot be empty');
115 $this->reflect();
119 * Retrieve contents of DocBlock
121 * @return string
123 public function getContents()
125 $this->reflect();
127 return $this->cleanDocComment;
131 * Get start line (position) of DocBlock
133 * @return int
135 public function getStartLine()
137 $this->reflect();
139 return $this->startLine;
143 * Get last line (position) of DocBlock
145 * @return int
147 public function getEndLine()
149 $this->reflect();
151 return $this->endLine;
155 * Get DocBlock short description
157 * @return string
159 public function getShortDescription()
161 $this->reflect();
163 return $this->shortDescription;
167 * Get DocBlock long description
169 * @return string
171 public function getLongDescription()
173 $this->reflect();
175 return $this->longDescription;
179 * Does the DocBlock contain the given annotation tag?
181 * @param string $name
182 * @return bool
184 public function hasTag($name)
186 $this->reflect();
187 foreach ($this->tags as $tag) {
188 if ($tag->getName() == $name) {
189 return true;
193 return false;
197 * Retrieve the given DocBlock tag
199 * @param string $name
200 * @return DocBlockTagInterface|false
202 public function getTag($name)
204 $this->reflect();
205 foreach ($this->tags as $tag) {
206 if ($tag->getName() == $name) {
207 return $tag;
211 return false;
215 * Get all DocBlock annotation tags
217 * @param string $filter
218 * @return DocBlockTagInterface[]
220 public function getTags($filter = null)
222 $this->reflect();
223 if ($filter === null || !is_string($filter)) {
224 return $this->tags;
227 $returnTags = array();
228 foreach ($this->tags as $tag) {
229 if ($tag->getName() == $filter) {
230 $returnTags[] = $tag;
234 return $returnTags;
238 * Parse the DocBlock
240 * @return void
242 protected function reflect()
244 if ($this->isReflected) {
245 return;
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) {
272 $str .= " " . $tag;
275 $str .= " }" . PHP_EOL;
276 $str .= "}" . PHP_EOL;
278 return $str;
282 * Serialize to string
284 * Required by the Reflector interface
286 * @return string
288 public function __toString()
290 return $this->toString();