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;
26 * Parses a file for namespaces/use/class declarations.
28 * @author Karoly Negyesi <karoly@negyesi.net>
30 class StaticReflectionParser implements ReflectionProviderInterface
33 * The fully qualified class name.
40 * The short class name.
44 protected $shortClassName;
47 * Whether the caller only wants class annotations.
51 protected $classAnnotationOptimize;
54 * A ClassFinder object which finds the class.
56 * @var ClassFinderInterface
61 * Whether the parser has run.
65 protected $parsed = false;
68 * The namespace of the class.
72 protected $namespace = '';
75 * The use statements of the class.
79 protected $useStatements = [];
82 * The docComment of the class.
86 protected $docComment = [
93 * The name of the class this class extends, if any.
97 protected $parentClassName = '';
100 * The parent PSR-0 Parser.
102 * @var \Doctrine\Common\Reflection\StaticReflectionParser
104 protected $parentStaticReflectionParser;
107 * Parses a class residing in a PSR-0 hierarchy.
109 * @param string $className The full, namespaced class name.
110 * @param ClassFinderInterface $finder A ClassFinder object which finds the class.
111 * @param boolean $classAnnotationOptimize Only retrieve the class docComment.
112 * Presumes there is only one statement per line.
114 public function __construct($className, $finder, $classAnnotationOptimize = false)
116 $this->className = ltrim($className, '\\');
117 $lastNsPos = strrpos($this->className, '\\');
119 if ($lastNsPos !== false) {
120 $this->namespace = substr($this->className, 0, $lastNsPos);
121 $this->shortClassName = substr($this->className, $lastNsPos + 1);
123 $this->shortClassName = $this->className;
126 $this->finder = $finder;
127 $this->classAnnotationOptimize = $classAnnotationOptimize;
133 protected function parse()
135 if ($this->parsed || !$fileName = $this->finder->findFile($this->className)) {
138 $this->parsed = true;
139 $contents = file_get_contents($fileName);
140 if ($this->classAnnotationOptimize) {
141 if (preg_match("/\A.*^\s*((abstract|final)\s+)?class\s+{$this->shortClassName}\s+/sm", $contents, $matches)) {
142 $contents = $matches[0];
145 $tokenParser = new TokenParser($contents);
149 while ($token = $tokenParser->next(false)) {
152 $this->useStatements = array_merge($this->useStatements, $tokenParser->parseUseStatement());
155 $docComment = $token[1];
158 if ($last_token !== T_PAAMAYIM_NEKUDOTAYIM) {
159 $this->docComment['class'] = $docComment;
167 $token = $tokenParser->next();
168 if ($token[0] === T_VARIABLE) {
169 $propertyName = substr($token[1], 1);
170 $this->docComment['property'][$propertyName] = $docComment;
173 if ($token[0] !== T_FUNCTION) {
174 // For example, it can be T_FINAL.
179 // The next string after function is the name, but
180 // there can be & before the function name so find the
182 while (($token = $tokenParser->next()) && $token[0] !== T_STRING);
183 $methodName = $token[1];
184 $this->docComment['method'][$methodName] = $docComment;
188 $this->parentClassName = $tokenParser->parseClass();
189 $nsPos = strpos($this->parentClassName, '\\');
190 $fullySpecified = false;
192 $fullySpecified = true;
195 $prefix = strtolower(substr($this->parentClassName, 0, $nsPos));
196 $postfix = substr($this->parentClassName, $nsPos);
198 $prefix = strtolower($this->parentClassName);
201 foreach ($this->useStatements as $alias => $use) {
202 if ($alias == $prefix) {
203 $this->parentClassName = '\\' . $use . $postfix;
204 $fullySpecified = true;
208 if (!$fullySpecified) {
209 $this->parentClassName = '\\' . $this->namespace . '\\' . $this->parentClassName;
214 $last_token = $token[0];
219 * @return StaticReflectionParser
221 protected function getParentStaticReflectionParser()
223 if (empty($this->parentStaticReflectionParser)) {
224 $this->parentStaticReflectionParser = new static($this->parentClassName, $this->finder);
227 return $this->parentStaticReflectionParser;
233 public function getClassName()
235 return $this->className;
241 public function getNamespaceName()
243 return $this->namespace;
249 public function getReflectionClass()
251 return new StaticReflectionClass($this);
257 public function getReflectionMethod($methodName)
259 return new StaticReflectionMethod($this, $methodName);
265 public function getReflectionProperty($propertyName)
267 return new StaticReflectionProperty($this, $propertyName);
271 * Gets the use statements from this file.
275 public function getUseStatements()
279 return $this->useStatements;
283 * Gets the doc comment.
285 * @param string $type The type: 'class', 'property' or 'method'.
286 * @param string $name The name of the property or method, not needed for 'class'.
288 * @return string The doc comment, empty string if none.
290 public function getDocComment($type = 'class', $name = '')
294 return $name ? $this->docComment[$type][$name] : $this->docComment[$type];
298 * Gets the PSR-0 parser for the declaring class.
300 * @param string $type The type: 'property' or 'method'.
301 * @param string $name The name of the property or method.
303 * @return StaticReflectionParser A static reflection parser for the declaring class.
305 * @throws ReflectionException
307 public function getStaticReflectionParserForDeclaringClass($type, $name)
310 if (isset($this->docComment[$type][$name])) {
313 if (!empty($this->parentClassName)) {
314 return $this->getParentStaticReflectionParser()->getStaticReflectionParserForDeclaringClass($type, $name);
316 throw new ReflectionException('Invalid ' . $type . ' "' . $name . '"');