annotate vendor/doctrine/common/lib/Doctrine/Common/Reflection/StaticReflectionParser.php @ 8:50b0d041100e

Further files for download
author Chris Cannam
date Mon, 05 Feb 2018 10:56:40 +0000
parents 4c8ae668cc8c
children 7a779792577d
rev   line source
Chris@0 1 <?php
Chris@0 2 /*
Chris@0 3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
Chris@0 4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
Chris@0 5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
Chris@0 6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
Chris@0 7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
Chris@0 8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Chris@0 9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Chris@0 10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Chris@0 11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Chris@0 12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
Chris@0 13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Chris@0 14 *
Chris@0 15 * This software consists of voluntary contributions made by many individuals
Chris@0 16 * and is licensed under the MIT license. For more information, see
Chris@0 17 * <http://www.doctrine-project.org>.
Chris@0 18 */
Chris@0 19
Chris@0 20 namespace Doctrine\Common\Reflection;
Chris@0 21
Chris@0 22 use Doctrine\Common\Annotations\TokenParser;
Chris@0 23 use ReflectionException;
Chris@0 24
Chris@0 25 /**
Chris@0 26 * Parses a file for namespaces/use/class declarations.
Chris@0 27 *
Chris@0 28 * @author Karoly Negyesi <karoly@negyesi.net>
Chris@0 29 */
Chris@0 30 class StaticReflectionParser implements ReflectionProviderInterface
Chris@0 31 {
Chris@0 32 /**
Chris@0 33 * The fully qualified class name.
Chris@0 34 *
Chris@0 35 * @var string
Chris@0 36 */
Chris@0 37 protected $className;
Chris@0 38
Chris@0 39 /**
Chris@0 40 * The short class name.
Chris@0 41 *
Chris@0 42 * @var string
Chris@0 43 */
Chris@0 44 protected $shortClassName;
Chris@0 45
Chris@0 46 /**
Chris@0 47 * Whether the caller only wants class annotations.
Chris@0 48 *
Chris@0 49 * @var boolean.
Chris@0 50 */
Chris@0 51 protected $classAnnotationOptimize;
Chris@0 52
Chris@0 53 /**
Chris@0 54 * Whether the parser has run.
Chris@0 55 *
Chris@0 56 * @var boolean
Chris@0 57 */
Chris@0 58 protected $parsed = false;
Chris@0 59
Chris@0 60 /**
Chris@0 61 * The namespace of the class.
Chris@0 62 *
Chris@0 63 * @var string
Chris@0 64 */
Chris@0 65 protected $namespace = '';
Chris@0 66
Chris@0 67 /**
Chris@0 68 * The use statements of the class.
Chris@0 69 *
Chris@0 70 * @var array
Chris@0 71 */
Chris@0 72 protected $useStatements = [];
Chris@0 73
Chris@0 74 /**
Chris@0 75 * The docComment of the class.
Chris@0 76 *
Chris@0 77 * @var string
Chris@0 78 */
Chris@0 79 protected $docComment = [
Chris@0 80 'class' => '',
Chris@0 81 'property' => [],
Chris@0 82 'method' => []
Chris@0 83 ];
Chris@0 84
Chris@0 85 /**
Chris@0 86 * The name of the class this class extends, if any.
Chris@0 87 *
Chris@0 88 * @var string
Chris@0 89 */
Chris@0 90 protected $parentClassName = '';
Chris@0 91
Chris@0 92 /**
Chris@0 93 * The parent PSR-0 Parser.
Chris@0 94 *
Chris@0 95 * @var \Doctrine\Common\Reflection\StaticReflectionParser
Chris@0 96 */
Chris@0 97 protected $parentStaticReflectionParser;
Chris@0 98
Chris@0 99 /**
Chris@0 100 * Parses a class residing in a PSR-0 hierarchy.
Chris@0 101 *
Chris@0 102 * @param string $className The full, namespaced class name.
Chris@0 103 * @param ClassFinderInterface $finder A ClassFinder object which finds the class.
Chris@0 104 * @param boolean $classAnnotationOptimize Only retrieve the class docComment.
Chris@0 105 * Presumes there is only one statement per line.
Chris@0 106 */
Chris@0 107 public function __construct($className, $finder, $classAnnotationOptimize = false)
Chris@0 108 {
Chris@0 109 $this->className = ltrim($className, '\\');
Chris@0 110 $lastNsPos = strrpos($this->className, '\\');
Chris@0 111
Chris@0 112 if ($lastNsPos !== false) {
Chris@0 113 $this->namespace = substr($this->className, 0, $lastNsPos);
Chris@0 114 $this->shortClassName = substr($this->className, $lastNsPos + 1);
Chris@0 115 } else {
Chris@0 116 $this->shortClassName = $this->className;
Chris@0 117 }
Chris@0 118
Chris@0 119 $this->finder = $finder;
Chris@0 120 $this->classAnnotationOptimize = $classAnnotationOptimize;
Chris@0 121 }
Chris@0 122
Chris@0 123 /**
Chris@0 124 * @return void
Chris@0 125 */
Chris@0 126 protected function parse()
Chris@0 127 {
Chris@0 128 if ($this->parsed || !$fileName = $this->finder->findFile($this->className)) {
Chris@0 129 return;
Chris@0 130 }
Chris@0 131 $this->parsed = true;
Chris@0 132 $contents = file_get_contents($fileName);
Chris@0 133 if ($this->classAnnotationOptimize) {
Chris@0 134 if (preg_match("/\A.*^\s*((abstract|final)\s+)?class\s+{$this->shortClassName}\s+/sm", $contents, $matches)) {
Chris@0 135 $contents = $matches[0];
Chris@0 136 }
Chris@0 137 }
Chris@0 138 $tokenParser = new TokenParser($contents);
Chris@0 139 $docComment = '';
Chris@0 140 while ($token = $tokenParser->next(false)) {
Chris@0 141 if (is_array($token)) {
Chris@0 142 switch ($token[0]) {
Chris@0 143 case T_USE:
Chris@0 144 $this->useStatements = array_merge($this->useStatements, $tokenParser->parseUseStatement());
Chris@0 145 break;
Chris@0 146 case T_DOC_COMMENT:
Chris@0 147 $docComment = $token[1];
Chris@0 148 break;
Chris@0 149 case T_CLASS:
Chris@0 150 $this->docComment['class'] = $docComment;
Chris@0 151 $docComment = '';
Chris@0 152 break;
Chris@0 153 case T_VAR:
Chris@0 154 case T_PRIVATE:
Chris@0 155 case T_PROTECTED:
Chris@0 156 case T_PUBLIC:
Chris@0 157 $token = $tokenParser->next();
Chris@0 158 if ($token[0] === T_VARIABLE) {
Chris@0 159 $propertyName = substr($token[1], 1);
Chris@0 160 $this->docComment['property'][$propertyName] = $docComment;
Chris@0 161 continue 2;
Chris@0 162 }
Chris@0 163 if ($token[0] !== T_FUNCTION) {
Chris@0 164 // For example, it can be T_FINAL.
Chris@0 165 continue 2;
Chris@0 166 }
Chris@0 167 // No break.
Chris@0 168 case T_FUNCTION:
Chris@0 169 // The next string after function is the name, but
Chris@0 170 // there can be & before the function name so find the
Chris@0 171 // string.
Chris@0 172 while (($token = $tokenParser->next()) && $token[0] !== T_STRING);
Chris@0 173 $methodName = $token[1];
Chris@0 174 $this->docComment['method'][$methodName] = $docComment;
Chris@0 175 $docComment = '';
Chris@0 176 break;
Chris@0 177 case T_EXTENDS:
Chris@0 178 $this->parentClassName = $tokenParser->parseClass();
Chris@0 179 $nsPos = strpos($this->parentClassName, '\\');
Chris@0 180 $fullySpecified = false;
Chris@0 181 if ($nsPos === 0) {
Chris@0 182 $fullySpecified = true;
Chris@0 183 } else {
Chris@0 184 if ($nsPos) {
Chris@0 185 $prefix = strtolower(substr($this->parentClassName, 0, $nsPos));
Chris@0 186 $postfix = substr($this->parentClassName, $nsPos);
Chris@0 187 } else {
Chris@0 188 $prefix = strtolower($this->parentClassName);
Chris@0 189 $postfix = '';
Chris@0 190 }
Chris@0 191 foreach ($this->useStatements as $alias => $use) {
Chris@0 192 if ($alias == $prefix) {
Chris@0 193 $this->parentClassName = '\\' . $use . $postfix;
Chris@0 194 $fullySpecified = true;
Chris@0 195 }
Chris@0 196 }
Chris@0 197 }
Chris@0 198 if (!$fullySpecified) {
Chris@0 199 $this->parentClassName = '\\' . $this->namespace . '\\' . $this->parentClassName;
Chris@0 200 }
Chris@0 201 break;
Chris@0 202 }
Chris@0 203 }
Chris@0 204 }
Chris@0 205 }
Chris@0 206
Chris@0 207 /**
Chris@0 208 * @return StaticReflectionParser
Chris@0 209 */
Chris@0 210 protected function getParentStaticReflectionParser()
Chris@0 211 {
Chris@0 212 if (empty($this->parentStaticReflectionParser)) {
Chris@0 213 $this->parentStaticReflectionParser = new static($this->parentClassName, $this->finder);
Chris@0 214 }
Chris@0 215
Chris@0 216 return $this->parentStaticReflectionParser;
Chris@0 217 }
Chris@0 218
Chris@0 219 /**
Chris@0 220 * @return string
Chris@0 221 */
Chris@0 222 public function getClassName()
Chris@0 223 {
Chris@0 224 return $this->className;
Chris@0 225 }
Chris@0 226
Chris@0 227 /**
Chris@0 228 * @return string
Chris@0 229 */
Chris@0 230 public function getNamespaceName()
Chris@0 231 {
Chris@0 232 return $this->namespace;
Chris@0 233 }
Chris@0 234
Chris@0 235 /**
Chris@0 236 * {@inheritDoc}
Chris@0 237 */
Chris@0 238 public function getReflectionClass()
Chris@0 239 {
Chris@0 240 return new StaticReflectionClass($this);
Chris@0 241 }
Chris@0 242
Chris@0 243 /**
Chris@0 244 * {@inheritDoc}
Chris@0 245 */
Chris@0 246 public function getReflectionMethod($methodName)
Chris@0 247 {
Chris@0 248 return new StaticReflectionMethod($this, $methodName);
Chris@0 249 }
Chris@0 250
Chris@0 251 /**
Chris@0 252 * {@inheritDoc}
Chris@0 253 */
Chris@0 254 public function getReflectionProperty($propertyName)
Chris@0 255 {
Chris@0 256 return new StaticReflectionProperty($this, $propertyName);
Chris@0 257 }
Chris@0 258
Chris@0 259 /**
Chris@0 260 * Gets the use statements from this file.
Chris@0 261 *
Chris@0 262 * @return array
Chris@0 263 */
Chris@0 264 public function getUseStatements()
Chris@0 265 {
Chris@0 266 $this->parse();
Chris@0 267
Chris@0 268 return $this->useStatements;
Chris@0 269 }
Chris@0 270
Chris@0 271 /**
Chris@0 272 * Gets the doc comment.
Chris@0 273 *
Chris@0 274 * @param string $type The type: 'class', 'property' or 'method'.
Chris@0 275 * @param string $name The name of the property or method, not needed for 'class'.
Chris@0 276 *
Chris@0 277 * @return string The doc comment, empty string if none.
Chris@0 278 */
Chris@0 279 public function getDocComment($type = 'class', $name = '')
Chris@0 280 {
Chris@0 281 $this->parse();
Chris@0 282
Chris@0 283 return $name ? $this->docComment[$type][$name] : $this->docComment[$type];
Chris@0 284 }
Chris@0 285
Chris@0 286 /**
Chris@0 287 * Gets the PSR-0 parser for the declaring class.
Chris@0 288 *
Chris@0 289 * @param string $type The type: 'property' or 'method'.
Chris@0 290 * @param string $name The name of the property or method.
Chris@0 291 *
Chris@0 292 * @return StaticReflectionParser A static reflection parser for the declaring class.
Chris@0 293 *
Chris@0 294 * @throws ReflectionException
Chris@0 295 */
Chris@0 296 public function getStaticReflectionParserForDeclaringClass($type, $name)
Chris@0 297 {
Chris@0 298 $this->parse();
Chris@0 299 if (isset($this->docComment[$type][$name])) {
Chris@0 300 return $this;
Chris@0 301 }
Chris@0 302 if (!empty($this->parentClassName)) {
Chris@0 303 return $this->getParentStaticReflectionParser()->getStaticReflectionParserForDeclaringClass($type, $name);
Chris@0 304 }
Chris@0 305 throw new ReflectionException('Invalid ' . $type . ' "' . $name . '"');
Chris@0 306 }
Chris@0 307 }