composer package updates
[openemr.git] / vendor / doctrine / common / lib / Doctrine / Common / Reflection / StaticReflectionParser.php
blob17bcd7c9f0d12771d87ffffbe7995ada655288a2
1 <?php
2 /*
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\Common\Reflection;
22 use Doctrine\Common\Annotations\TokenParser;
23 use ReflectionException;
25 /**
26 * Parses a file for namespaces/use/class declarations.
28 * @author Karoly Negyesi <karoly@negyesi.net>
30 class StaticReflectionParser implements ReflectionProviderInterface
32 /**
33 * The fully qualified class name.
35 * @var string
37 protected $className;
39 /**
40 * The short class name.
42 * @var string
44 protected $shortClassName;
46 /**
47 * Whether the caller only wants class annotations.
49 * @var boolean.
51 protected $classAnnotationOptimize;
53 /**
54 * Whether the parser has run.
56 * @var boolean
58 protected $parsed = false;
60 /**
61 * The namespace of the class.
63 * @var string
65 protected $namespace = '';
67 /**
68 * The use statements of the class.
70 * @var array
72 protected $useStatements = [];
74 /**
75 * The docComment of the class.
77 * @var string
79 protected $docComment = [
80 'class' => '',
81 'property' => [],
82 'method' => []
85 /**
86 * The name of the class this class extends, if any.
88 * @var string
90 protected $parentClassName = '';
92 /**
93 * The parent PSR-0 Parser.
95 * @var \Doctrine\Common\Reflection\StaticReflectionParser
97 protected $parentStaticReflectionParser;
99 /**
100 * Parses a class residing in a PSR-0 hierarchy.
102 * @param string $className The full, namespaced class name.
103 * @param ClassFinderInterface $finder A ClassFinder object which finds the class.
104 * @param boolean $classAnnotationOptimize Only retrieve the class docComment.
105 * Presumes there is only one statement per line.
107 public function __construct($className, $finder, $classAnnotationOptimize = false)
109 $this->className = ltrim($className, '\\');
110 $lastNsPos = strrpos($this->className, '\\');
112 if ($lastNsPos !== false) {
113 $this->namespace = substr($this->className, 0, $lastNsPos);
114 $this->shortClassName = substr($this->className, $lastNsPos + 1);
115 } else {
116 $this->shortClassName = $this->className;
119 $this->finder = $finder;
120 $this->classAnnotationOptimize = $classAnnotationOptimize;
124 * @return void
126 protected function parse()
128 if ($this->parsed || !$fileName = $this->finder->findFile($this->className)) {
129 return;
131 $this->parsed = true;
132 $contents = file_get_contents($fileName);
133 if ($this->classAnnotationOptimize) {
134 if (preg_match("/\A.*^\s*((abstract|final)\s+)?class\s+{$this->shortClassName}\s+/sm", $contents, $matches)) {
135 $contents = $matches[0];
138 $tokenParser = new TokenParser($contents);
139 $docComment = '';
140 $last_token = false;
142 while ($token = $tokenParser->next(false)) {
143 if (is_array($token)) {switch ($token[0]) {
144 case T_USE:
145 $this->useStatements = array_merge($this->useStatements, $tokenParser->parseUseStatement());
146 break;
147 case T_DOC_COMMENT:
148 $docComment = $token[1];
149 break;
150 case T_CLASS:
151 if ($last_token !== T_PAAMAYIM_NEKUDOTAYIM) {$this->docComment['class'] = $docComment;
152 $docComment = '';}
153 break;
154 case T_VAR:
155 case T_PRIVATE:
156 case T_PROTECTED:
157 case T_PUBLIC:
158 $token = $tokenParser->next();
159 if ($token[0] === T_VARIABLE) {
160 $propertyName = substr($token[1], 1);
161 $this->docComment['property'][$propertyName] = $docComment;
162 continue 2;
164 if ($token[0] !== T_FUNCTION) {
165 // For example, it can be T_FINAL.
166 continue 2;
168 // No break.
169 case T_FUNCTION:
170 // The next string after function is the name, but
171 // there can be & before the function name so find the
172 // string.
173 while (($token = $tokenParser->next()) && $token[0] !== T_STRING);
174 $methodName = $token[1];
175 $this->docComment['method'][$methodName] = $docComment;
176 $docComment = '';
177 break;
178 case T_EXTENDS:
179 $this->parentClassName = $tokenParser->parseClass();
180 $nsPos = strpos($this->parentClassName, '\\');
181 $fullySpecified = false;
182 if ($nsPos === 0) {
183 $fullySpecified = true;
184 } else {
185 if ($nsPos) {
186 $prefix = strtolower(substr($this->parentClassName, 0, $nsPos));
187 $postfix = substr($this->parentClassName, $nsPos);
188 } else {
189 $prefix = strtolower($this->parentClassName);
190 $postfix = '';
192 foreach ($this->useStatements as $alias => $use) {
193 if ($alias == $prefix) {
194 $this->parentClassName = '\\' . $use . $postfix;
195 $fullySpecified = true;
199 if (!$fullySpecified) {
200 $this->parentClassName = '\\' . $this->namespace . '\\' . $this->parentClassName;
202 break;}
205 $last_token = $token[0];
210 * @return StaticReflectionParser
212 protected function getParentStaticReflectionParser()
214 if (empty($this->parentStaticReflectionParser)) {
215 $this->parentStaticReflectionParser = new static($this->parentClassName, $this->finder);
218 return $this->parentStaticReflectionParser;
222 * @return string
224 public function getClassName()
226 return $this->className;
230 * @return string
232 public function getNamespaceName()
234 return $this->namespace;
238 * {@inheritDoc}
240 public function getReflectionClass()
242 return new StaticReflectionClass($this);
246 * {@inheritDoc}
248 public function getReflectionMethod($methodName)
250 return new StaticReflectionMethod($this, $methodName);
254 * {@inheritDoc}
256 public function getReflectionProperty($propertyName)
258 return new StaticReflectionProperty($this, $propertyName);
262 * Gets the use statements from this file.
264 * @return array
266 public function getUseStatements()
268 $this->parse();
270 return $this->useStatements;
274 * Gets the doc comment.
276 * @param string $type The type: 'class', 'property' or 'method'.
277 * @param string $name The name of the property or method, not needed for 'class'.
279 * @return string The doc comment, empty string if none.
281 public function getDocComment($type = 'class', $name = '')
283 $this->parse();
285 return $name ? $this->docComment[$type][$name] : $this->docComment[$type];
289 * Gets the PSR-0 parser for the declaring class.
291 * @param string $type The type: 'property' or 'method'.
292 * @param string $name The name of the property or method.
294 * @return StaticReflectionParser A static reflection parser for the declaring class.
296 * @throws ReflectionException
298 public function getStaticReflectionParserForDeclaringClass($type, $name)
300 $this->parse();
301 if (isset($this->docComment[$type][$name])) {
302 return $this;
304 if (!empty($this->parentClassName)) {
305 return $this->getParentStaticReflectionParser()->getStaticReflectionParserForDeclaringClass($type, $name);
307 throw new ReflectionException('Invalid ' . $type . ' "' . $name . '"');