Security update to Drupal 8.4.6
[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      * A ClassFinder object which finds the class.
55      *
56      * @var ClassFinderInterface
57      */
58     protected $finder;
59
60     /**
61      * Whether the parser has run.
62      *
63      * @var boolean
64      */
65     protected $parsed = false;
66
67     /**
68      * The namespace of the class.
69      *
70      * @var string
71      */
72     protected $namespace = '';
73
74     /**
75      * The use statements of the class.
76      *
77      * @var array
78      */
79     protected $useStatements = [];
80
81     /**
82      * The docComment of the class.
83      *
84      * @var mixed[]
85      */
86     protected $docComment = [
87         'class' => '',
88         'property' => [],
89         'method' => []
90     ];
91
92     /**
93      * The name of the class this class extends, if any.
94      *
95      * @var string
96      */
97     protected $parentClassName = '';
98
99     /**
100      * The parent PSR-0 Parser.
101      *
102      * @var \Doctrine\Common\Reflection\StaticReflectionParser
103      */
104     protected $parentStaticReflectionParser;
105
106     /**
107      * Parses a class residing in a PSR-0 hierarchy.
108      *
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.
113      */
114     public function __construct($className, $finder, $classAnnotationOptimize = false)
115     {
116         $this->className = ltrim($className, '\\');
117         $lastNsPos = strrpos($this->className, '\\');
118
119         if ($lastNsPos !== false) {
120             $this->namespace = substr($this->className, 0, $lastNsPos);
121             $this->shortClassName = substr($this->className, $lastNsPos + 1);
122         } else {
123             $this->shortClassName = $this->className;
124         }
125
126         $this->finder = $finder;
127         $this->classAnnotationOptimize = $classAnnotationOptimize;
128     }
129
130     /**
131      * @return void
132      */
133     protected function parse()
134     {
135         if ($this->parsed || !$fileName = $this->finder->findFile($this->className)) {
136             return;
137         }
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];
143             }
144         }
145         $tokenParser = new TokenParser($contents);
146         $docComment = '';
147         $last_token = false;
148
149         while ($token = $tokenParser->next(false)) {
150             switch ($token[0]) {
151                 case T_USE:
152                     $this->useStatements = array_merge($this->useStatements, $tokenParser->parseUseStatement());
153                     break;
154                 case T_DOC_COMMENT:
155                     $docComment = $token[1];
156                     break;
157                 case T_CLASS:
158                     if ($last_token !== T_PAAMAYIM_NEKUDOTAYIM) {
159                         $this->docComment['class'] = $docComment;
160                         $docComment = '';
161                     }
162                     break;
163                 case T_VAR:
164                 case T_PRIVATE:
165                 case T_PROTECTED:
166                 case T_PUBLIC:
167                     $token = $tokenParser->next();
168                     if ($token[0] === T_VARIABLE) {
169                         $propertyName = substr($token[1], 1);
170                         $this->docComment['property'][$propertyName] = $docComment;
171                         continue 2;
172                     }
173                     if ($token[0] !== T_FUNCTION) {
174                         // For example, it can be T_FINAL.
175                         continue 2;
176                     }
177                     // No break.
178                 case T_FUNCTION:
179                     // The next string after function is the name, but
180                     // there can be & before the function name so find the
181                     // string.
182                     while (($token = $tokenParser->next()) && $token[0] !== T_STRING);
183                     $methodName = $token[1];
184                     $this->docComment['method'][$methodName] = $docComment;
185                     $docComment = '';
186                     break;
187                 case T_EXTENDS:
188                     $this->parentClassName = $tokenParser->parseClass();
189                     $nsPos = strpos($this->parentClassName, '\\');
190                     $fullySpecified = false;
191                     if ($nsPos === 0) {
192                         $fullySpecified = true;
193                     } else {
194                         if ($nsPos) {
195                             $prefix = strtolower(substr($this->parentClassName, 0, $nsPos));
196                             $postfix = substr($this->parentClassName, $nsPos);
197                         } else {
198                             $prefix = strtolower($this->parentClassName);
199                             $postfix = '';
200                         }
201                         foreach ($this->useStatements as $alias => $use) {
202                             if ($alias == $prefix) {
203                                 $this->parentClassName = '\\' . $use . $postfix;
204                                 $fullySpecified = true;
205                           }
206                         }
207                     }
208                     if (!$fullySpecified) {
209                         $this->parentClassName = '\\' . $this->namespace . '\\' . $this->parentClassName;
210                     }
211                     break;
212             }
213
214             $last_token = $token[0];
215         }
216     }
217
218     /**
219      * @return StaticReflectionParser
220      */
221     protected function getParentStaticReflectionParser()
222     {
223         if (empty($this->parentStaticReflectionParser)) {
224             $this->parentStaticReflectionParser = new static($this->parentClassName, $this->finder);
225         }
226
227         return $this->parentStaticReflectionParser;
228     }
229
230     /**
231      * @return string
232      */
233     public function getClassName()
234     {
235         return $this->className;
236     }
237
238     /**
239      * @return string
240      */
241     public function getNamespaceName()
242     {
243         return $this->namespace;
244     }
245
246     /**
247      * {@inheritDoc}
248      */
249     public function getReflectionClass()
250     {
251         return new StaticReflectionClass($this);
252     }
253
254     /**
255      * {@inheritDoc}
256      */
257     public function getReflectionMethod($methodName)
258     {
259         return new StaticReflectionMethod($this, $methodName);
260     }
261
262     /**
263      * {@inheritDoc}
264      */
265     public function getReflectionProperty($propertyName)
266     {
267         return new StaticReflectionProperty($this, $propertyName);
268     }
269
270     /**
271      * Gets the use statements from this file.
272      *
273      * @return array
274      */
275     public function getUseStatements()
276     {
277         $this->parse();
278
279         return $this->useStatements;
280     }
281
282     /**
283      * Gets the doc comment.
284      *
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'.
287      *
288      * @return string The doc comment, empty string if none.
289      */
290     public function getDocComment($type = 'class', $name = '')
291     {
292         $this->parse();
293
294         return $name ? $this->docComment[$type][$name] : $this->docComment[$type];
295     }
296
297     /**
298      * Gets the PSR-0 parser for the declaring class.
299      *
300      * @param string $type The type: 'property' or 'method'.
301      * @param string $name The name of the property or method.
302      *
303      * @return StaticReflectionParser A static reflection parser for the declaring class.
304      *
305      * @throws ReflectionException
306      */
307     public function getStaticReflectionParserForDeclaringClass($type, $name)
308     {
309         $this->parse();
310         if (isset($this->docComment[$type][$name])) {
311             return $this;
312         }
313         if (!empty($this->parentClassName)) {
314             return $this->getParentStaticReflectionParser()->getStaticReflectionParserForDeclaringClass($type, $name);
315         }
316         throw new ReflectionException('Invalid ' . $type . ' "' . $name . '"');
317     }
318 }