17bcd7c9f0d12771d87ffffbe7995ada655288a2
[yaffs-website] / vendor / doctrine / common / lib / Doctrine / Common / Reflection / StaticReflectionParser.php
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.
14  *
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>.
18  */
19
20 namespace Doctrine\Common\Reflection;
21
22 use Doctrine\Common\Annotations\TokenParser;
23 use ReflectionException;
24
25 /**
26  * Parses a file for namespaces/use/class declarations.
27  *
28  * @author Karoly Negyesi <karoly@negyesi.net>
29  */
30 class StaticReflectionParser implements ReflectionProviderInterface
31 {
32     /**
33      * The fully qualified class name.
34      *
35      * @var string
36      */
37     protected $className;
38
39     /**
40      * The short class name.
41      *
42      * @var string
43      */
44     protected $shortClassName;
45
46     /**
47      * Whether the caller only wants class annotations.
48      *
49      * @var boolean.
50      */
51     protected $classAnnotationOptimize;
52
53     /**
54      * Whether the parser has run.
55      *
56      * @var boolean
57      */
58     protected $parsed = false;
59
60     /**
61      * The namespace of the class.
62      *
63      * @var string
64      */
65     protected $namespace = '';
66
67     /**
68      * The use statements of the class.
69      *
70      * @var array
71      */
72     protected $useStatements = [];
73
74     /**
75      * The docComment of the class.
76      *
77      * @var string
78      */
79     protected $docComment = [
80         'class' => '',
81         'property' => [],
82         'method' => []
83     ];
84
85     /**
86      * The name of the class this class extends, if any.
87      *
88      * @var string
89      */
90     protected $parentClassName = '';
91
92     /**
93      * The parent PSR-0 Parser.
94      *
95      * @var \Doctrine\Common\Reflection\StaticReflectionParser
96      */
97     protected $parentStaticReflectionParser;
98
99     /**
100      * Parses a class residing in a PSR-0 hierarchy.
101      *
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.
106      */
107     public function __construct($className, $finder, $classAnnotationOptimize = false)
108     {
109         $this->className = ltrim($className, '\\');
110         $lastNsPos = strrpos($this->className, '\\');
111
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;
117         }
118
119         $this->finder = $finder;
120         $this->classAnnotationOptimize = $classAnnotationOptimize;
121     }
122
123     /**
124      * @return void
125      */
126     protected function parse()
127     {
128         if ($this->parsed || !$fileName = $this->finder->findFile($this->className)) {
129             return;
130         }
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];
136             }
137         }
138         $tokenParser = new TokenParser($contents);
139         $docComment = '';
140         $last_token = false;
141
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;
163                     }
164                     if ($token[0] !== T_FUNCTION) {
165                         // For example, it can be T_FINAL.
166                         continue 2;
167                     }
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 = '';
191                         }
192                         foreach ($this->useStatements as $alias => $use) {
193                             if ($alias == $prefix) {
194                                 $this->parentClassName = '\\' . $use . $postfix;
195                                 $fullySpecified = true;
196                           }
197                         }
198                     }
199                     if (!$fullySpecified) {
200                         $this->parentClassName = '\\' . $this->namespace . '\\' . $this->parentClassName;
201                     }
202                     break;}
203             }
204
205             $last_token = $token[0];
206         }
207     }
208
209     /**
210      * @return StaticReflectionParser
211      */
212     protected function getParentStaticReflectionParser()
213     {
214         if (empty($this->parentStaticReflectionParser)) {
215             $this->parentStaticReflectionParser = new static($this->parentClassName, $this->finder);
216         }
217
218         return $this->parentStaticReflectionParser;
219     }
220
221     /**
222      * @return string
223      */
224     public function getClassName()
225     {
226         return $this->className;
227     }
228
229     /**
230      * @return string
231      */
232     public function getNamespaceName()
233     {
234         return $this->namespace;
235     }
236
237     /**
238      * {@inheritDoc}
239      */
240     public function getReflectionClass()
241     {
242         return new StaticReflectionClass($this);
243     }
244
245     /**
246      * {@inheritDoc}
247      */
248     public function getReflectionMethod($methodName)
249     {
250         return new StaticReflectionMethod($this, $methodName);
251     }
252
253     /**
254      * {@inheritDoc}
255      */
256     public function getReflectionProperty($propertyName)
257     {
258         return new StaticReflectionProperty($this, $propertyName);
259     }
260
261     /**
262      * Gets the use statements from this file.
263      *
264      * @return array
265      */
266     public function getUseStatements()
267     {
268         $this->parse();
269
270         return $this->useStatements;
271     }
272
273     /**
274      * Gets the doc comment.
275      *
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'.
278      *
279      * @return string The doc comment, empty string if none.
280      */
281     public function getDocComment($type = 'class', $name = '')
282     {
283         $this->parse();
284
285         return $name ? $this->docComment[$type][$name] : $this->docComment[$type];
286     }
287
288     /**
289      * Gets the PSR-0 parser for the declaring class.
290      *
291      * @param string $type The type: 'property' or 'method'.
292      * @param string $name The name of the property or method.
293      *
294      * @return StaticReflectionParser A static reflection parser for the declaring class.
295      *
296      * @throws ReflectionException
297      */
298     public function getStaticReflectionParserForDeclaringClass($type, $name)
299     {
300         $this->parse();
301         if (isset($this->docComment[$type][$name])) {
302             return $this;
303         }
304         if (!empty($this->parentClassName)) {
305             return $this->getParentStaticReflectionParser()->getStaticReflectionParserForDeclaringClass($type, $name);
306         }
307         throw new ReflectionException('Invalid ' . $type . ' "' . $name . '"');
308     }
309 }