AnnotationReader.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  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. namespace Doctrine\Common\Annotations;
  20. use Doctrine\Common\Annotations\Annotation\IgnoreAnnotation;
  21. use Doctrine\Common\Annotations\Annotation\Target;
  22. use ReflectionClass;
  23. use ReflectionMethod;
  24. use ReflectionProperty;
  25. /**
  26. * A reader for docblock annotations.
  27. *
  28. * @author Benjamin Eberlei <kontakt@beberlei.de>
  29. * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
  30. * @author Jonathan Wage <jonwage@gmail.com>
  31. * @author Roman Borschel <roman@code-factory.org>
  32. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  33. */
  34. class AnnotationReader implements Reader
  35. {
  36. /**
  37. * Global map for imports.
  38. *
  39. * @var array
  40. */
  41. private static $globalImports = [
  42. 'ignoreannotation' => 'Doctrine\Common\Annotations\Annotation\IgnoreAnnotation',
  43. ];
  44. /**
  45. * A list with annotations that are not causing exceptions when not resolved to an annotation class.
  46. *
  47. * The names are case sensitive.
  48. *
  49. * @var array
  50. */
  51. private static $globalIgnoredNames = [
  52. // Annotation tags
  53. 'Annotation' => true, 'Attribute' => true, 'Attributes' => true,
  54. /* Can we enable this? 'Enum' => true, */
  55. 'Required' => true,
  56. 'Target' => true,
  57. // Widely used tags (but not existent in phpdoc)
  58. 'fix' => true , 'fixme' => true,
  59. 'override' => true,
  60. // PHPDocumentor 1 tags
  61. 'abstract'=> true, 'access'=> true,
  62. 'code' => true,
  63. 'deprec'=> true,
  64. 'endcode' => true, 'exception'=> true,
  65. 'final'=> true,
  66. 'ingroup' => true, 'inheritdoc'=> true, 'inheritDoc'=> true,
  67. 'magic' => true,
  68. 'name'=> true,
  69. 'toc' => true, 'tutorial'=> true,
  70. 'private' => true,
  71. 'static'=> true, 'staticvar'=> true, 'staticVar'=> true,
  72. 'throw' => true,
  73. // PHPDocumentor 2 tags.
  74. 'api' => true, 'author'=> true,
  75. 'category'=> true, 'copyright'=> true,
  76. 'deprecated'=> true,
  77. 'example'=> true,
  78. 'filesource'=> true,
  79. 'global'=> true,
  80. 'ignore'=> true, /* Can we enable this? 'index' => true, */ 'internal'=> true,
  81. 'license'=> true, 'link'=> true,
  82. 'method' => true,
  83. 'package'=> true, 'param'=> true, 'property' => true, 'property-read' => true, 'property-write' => true,
  84. 'return'=> true,
  85. 'see'=> true, 'since'=> true, 'source' => true, 'subpackage'=> true,
  86. 'throws'=> true, 'todo'=> true, 'TODO'=> true,
  87. 'usedby'=> true, 'uses' => true,
  88. 'var'=> true, 'version'=> true,
  89. // PHPUnit tags
  90. 'codeCoverageIgnore' => true, 'codeCoverageIgnoreStart' => true, 'codeCoverageIgnoreEnd' => true,
  91. // PHPCheckStyle
  92. 'SuppressWarnings' => true,
  93. // PHPStorm
  94. 'noinspection' => true,
  95. // PEAR
  96. 'package_version' => true,
  97. // PlantUML
  98. 'startuml' => true, 'enduml' => true,
  99. // Symfony 3.3 Cache Adapter
  100. 'experimental' => true,
  101. // Slevomat Coding Standard
  102. 'phpcsSuppress' => true,
  103. // PHP CodeSniffer
  104. 'codingStandardsIgnoreStart' => true,
  105. 'codingStandardsIgnoreEnd' => true,
  106. // PHPStan
  107. 'template' => true, 'implements' => true, 'extends' => true, 'use' => true,
  108. ];
  109. /**
  110. * A list with annotations that are not causing exceptions when not resolved to an annotation class.
  111. *
  112. * The names are case sensitive.
  113. *
  114. * @var array
  115. */
  116. private static $globalIgnoredNamespaces = [];
  117. /**
  118. * Add a new annotation to the globally ignored annotation names with regard to exception handling.
  119. *
  120. * @param string $name
  121. */
  122. static public function addGlobalIgnoredName($name)
  123. {
  124. self::$globalIgnoredNames[$name] = true;
  125. }
  126. /**
  127. * Add a new annotation to the globally ignored annotation namespaces with regard to exception handling.
  128. *
  129. * @param string $namespace
  130. */
  131. static public function addGlobalIgnoredNamespace($namespace)
  132. {
  133. self::$globalIgnoredNamespaces[$namespace] = true;
  134. }
  135. /**
  136. * Annotations parser.
  137. *
  138. * @var \Doctrine\Common\Annotations\DocParser
  139. */
  140. private $parser;
  141. /**
  142. * Annotations parser used to collect parsing metadata.
  143. *
  144. * @var \Doctrine\Common\Annotations\DocParser
  145. */
  146. private $preParser;
  147. /**
  148. * PHP parser used to collect imports.
  149. *
  150. * @var \Doctrine\Common\Annotations\PhpParser
  151. */
  152. private $phpParser;
  153. /**
  154. * In-memory cache mechanism to store imported annotations per class.
  155. *
  156. * @var array
  157. */
  158. private $imports = [];
  159. /**
  160. * In-memory cache mechanism to store ignored annotations per class.
  161. *
  162. * @var array
  163. */
  164. private $ignoredAnnotationNames = [];
  165. /**
  166. * Constructor.
  167. *
  168. * Initializes a new AnnotationReader.
  169. *
  170. * @param DocParser $parser
  171. *
  172. * @throws AnnotationException
  173. */
  174. public function __construct(DocParser $parser = null)
  175. {
  176. if (extension_loaded('Zend Optimizer+') && (ini_get('zend_optimizerplus.save_comments') === "0" || ini_get('opcache.save_comments') === "0")) {
  177. throw AnnotationException::optimizerPlusSaveComments();
  178. }
  179. if (extension_loaded('Zend OPcache') && ini_get('opcache.save_comments') == 0) {
  180. throw AnnotationException::optimizerPlusSaveComments();
  181. }
  182. // Make sure that the IgnoreAnnotation annotation is loaded
  183. class_exists(IgnoreAnnotation::class);
  184. $this->parser = $parser ?: new DocParser();
  185. $this->preParser = new DocParser;
  186. $this->preParser->setImports(self::$globalImports);
  187. $this->preParser->setIgnoreNotImportedAnnotations(true);
  188. $this->preParser->setIgnoredAnnotationNames(self::$globalIgnoredNames);
  189. $this->phpParser = new PhpParser;
  190. }
  191. /**
  192. * {@inheritDoc}
  193. */
  194. public function getClassAnnotations(ReflectionClass $class)
  195. {
  196. $this->parser->setTarget(Target::TARGET_CLASS);
  197. $this->parser->setImports($this->getClassImports($class));
  198. $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
  199. $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
  200. return $this->parser->parse($class->getDocComment(), 'class ' . $class->getName());
  201. }
  202. /**
  203. * {@inheritDoc}
  204. */
  205. public function getClassAnnotation(ReflectionClass $class, $annotationName)
  206. {
  207. $annotations = $this->getClassAnnotations($class);
  208. foreach ($annotations as $annotation) {
  209. if ($annotation instanceof $annotationName) {
  210. return $annotation;
  211. }
  212. }
  213. return null;
  214. }
  215. /**
  216. * {@inheritDoc}
  217. */
  218. public function getPropertyAnnotations(ReflectionProperty $property)
  219. {
  220. $class = $property->getDeclaringClass();
  221. $context = 'property ' . $class->getName() . "::\$" . $property->getName();
  222. $this->parser->setTarget(Target::TARGET_PROPERTY);
  223. $this->parser->setImports($this->getPropertyImports($property));
  224. $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
  225. $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
  226. return $this->parser->parse($property->getDocComment(), $context);
  227. }
  228. /**
  229. * {@inheritDoc}
  230. */
  231. public function getPropertyAnnotation(ReflectionProperty $property, $annotationName)
  232. {
  233. $annotations = $this->getPropertyAnnotations($property);
  234. foreach ($annotations as $annotation) {
  235. if ($annotation instanceof $annotationName) {
  236. return $annotation;
  237. }
  238. }
  239. return null;
  240. }
  241. /**
  242. * {@inheritDoc}
  243. */
  244. public function getMethodAnnotations(ReflectionMethod $method)
  245. {
  246. $class = $method->getDeclaringClass();
  247. $context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
  248. $this->parser->setTarget(Target::TARGET_METHOD);
  249. $this->parser->setImports($this->getMethodImports($method));
  250. $this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
  251. $this->parser->setIgnoredAnnotationNamespaces(self::$globalIgnoredNamespaces);
  252. return $this->parser->parse($method->getDocComment(), $context);
  253. }
  254. /**
  255. * {@inheritDoc}
  256. */
  257. public function getMethodAnnotation(ReflectionMethod $method, $annotationName)
  258. {
  259. $annotations = $this->getMethodAnnotations($method);
  260. foreach ($annotations as $annotation) {
  261. if ($annotation instanceof $annotationName) {
  262. return $annotation;
  263. }
  264. }
  265. return null;
  266. }
  267. /**
  268. * Returns the ignored annotations for the given class.
  269. *
  270. * @param \ReflectionClass $class
  271. *
  272. * @return array
  273. */
  274. private function getIgnoredAnnotationNames(ReflectionClass $class)
  275. {
  276. $name = $class->getName();
  277. if (isset($this->ignoredAnnotationNames[$name])) {
  278. return $this->ignoredAnnotationNames[$name];
  279. }
  280. $this->collectParsingMetadata($class);
  281. return $this->ignoredAnnotationNames[$name];
  282. }
  283. /**
  284. * Retrieves imports.
  285. *
  286. * @param \ReflectionClass $class
  287. *
  288. * @return array
  289. */
  290. private function getClassImports(ReflectionClass $class)
  291. {
  292. $name = $class->getName();
  293. if (isset($this->imports[$name])) {
  294. return $this->imports[$name];
  295. }
  296. $this->collectParsingMetadata($class);
  297. return $this->imports[$name];
  298. }
  299. /**
  300. * Retrieves imports for methods.
  301. *
  302. * @param \ReflectionMethod $method
  303. *
  304. * @return array
  305. */
  306. private function getMethodImports(ReflectionMethod $method)
  307. {
  308. $class = $method->getDeclaringClass();
  309. $classImports = $this->getClassImports($class);
  310. $traitImports = [];
  311. foreach ($class->getTraits() as $trait) {
  312. if ($trait->hasMethod($method->getName())
  313. && $trait->getFileName() === $method->getFileName()
  314. ) {
  315. $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait));
  316. }
  317. }
  318. return array_merge($classImports, $traitImports);
  319. }
  320. /**
  321. * Retrieves imports for properties.
  322. *
  323. * @param \ReflectionProperty $property
  324. *
  325. * @return array
  326. */
  327. private function getPropertyImports(ReflectionProperty $property)
  328. {
  329. $class = $property->getDeclaringClass();
  330. $classImports = $this->getClassImports($class);
  331. $traitImports = [];
  332. foreach ($class->getTraits() as $trait) {
  333. if ($trait->hasProperty($property->getName())) {
  334. $traitImports = array_merge($traitImports, $this->phpParser->parseClass($trait));
  335. }
  336. }
  337. return array_merge($classImports, $traitImports);
  338. }
  339. /**
  340. * Collects parsing metadata for a given class.
  341. *
  342. * @param \ReflectionClass $class
  343. */
  344. private function collectParsingMetadata(ReflectionClass $class)
  345. {
  346. $ignoredAnnotationNames = self::$globalIgnoredNames;
  347. $annotations = $this->preParser->parse($class->getDocComment(), 'class ' . $class->name);
  348. foreach ($annotations as $annotation) {
  349. if ($annotation instanceof IgnoreAnnotation) {
  350. foreach ($annotation->names AS $annot) {
  351. $ignoredAnnotationNames[$annot] = true;
  352. }
  353. }
  354. }
  355. $name = $class->getName();
  356. $this->imports[$name] = array_merge(
  357. self::$globalImports,
  358. $this->phpParser->parseClass($class),
  359. ['__NAMESPACE__' => $class->getNamespaceName()]
  360. );
  361. $this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;
  362. }
  363. }