AbstractDoctrineAnnotationFixer.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4. * This file is part of PHP CS Fixer.
  5. *
  6. * (c) Fabien Potencier <fabien@symfony.com>
  7. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  8. *
  9. * This source file is subject to the MIT license that is bundled
  10. * with this source code in the file LICENSE.
  11. */
  12. namespace PhpCsFixer;
  13. use PhpCsFixer\Doctrine\Annotation\Tokens as DoctrineAnnotationTokens;
  14. use PhpCsFixer\Fixer\ConfigurableFixerInterface;
  15. use PhpCsFixer\FixerConfiguration\FixerConfigurationResolver;
  16. use PhpCsFixer\FixerConfiguration\FixerConfigurationResolverInterface;
  17. use PhpCsFixer\FixerConfiguration\FixerOptionBuilder;
  18. use PhpCsFixer\Tokenizer\CT;
  19. use PhpCsFixer\Tokenizer\Token;
  20. use PhpCsFixer\Tokenizer\Tokens;
  21. use PhpCsFixer\Tokenizer\TokensAnalyzer;
  22. /**
  23. * @internal
  24. *
  25. * @phpstan-type _AutogeneratedInputConfiguration array{
  26. * scalar_types?: bool,
  27. * union_types?: bool
  28. * }
  29. * @phpstan-type _AutogeneratedComputedConfiguration array{
  30. * scalar_types: bool,
  31. * union_types: bool
  32. * }
  33. *
  34. * @implements ConfigurableFixerInterface<_AutogeneratedInputConfiguration, _AutogeneratedComputedConfiguration>
  35. */
  36. abstract class AbstractDoctrineAnnotationFixer extends AbstractFixer implements ConfigurableFixerInterface
  37. {
  38. /**
  39. * @var array<int, array{classIndex: int, token: Token, type: string}>
  40. */
  41. private array $classyElements;
  42. public function isCandidate(Tokens $tokens): bool
  43. {
  44. return $tokens->isTokenKindFound(T_DOC_COMMENT);
  45. }
  46. protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
  47. {
  48. // fetch indices one time, this is safe as we never add or remove a token during fixing
  49. $analyzer = new TokensAnalyzer($tokens);
  50. $this->classyElements = $analyzer->getClassyElements();
  51. /** @var Token $docCommentToken */
  52. foreach ($tokens->findGivenKind(T_DOC_COMMENT) as $index => $docCommentToken) {
  53. if (!$this->nextElementAcceptsDoctrineAnnotations($tokens, $index)) {
  54. continue;
  55. }
  56. $doctrineAnnotationTokens = DoctrineAnnotationTokens::createFromDocComment(
  57. $docCommentToken,
  58. $this->configuration['ignored_tags'] // @phpstan-ignore-line
  59. );
  60. $this->fixAnnotations($doctrineAnnotationTokens);
  61. $tokens[$index] = new Token([T_DOC_COMMENT, $doctrineAnnotationTokens->getCode()]);
  62. }
  63. }
  64. /**
  65. * Fixes Doctrine annotations from the given PHPDoc style comment.
  66. */
  67. abstract protected function fixAnnotations(DoctrineAnnotationTokens $doctrineAnnotationTokens): void;
  68. protected function createConfigurationDefinition(): FixerConfigurationResolverInterface
  69. {
  70. return new FixerConfigurationResolver([
  71. (new FixerOptionBuilder('ignored_tags', 'List of tags that must not be treated as Doctrine Annotations.'))
  72. ->setAllowedTypes(['string[]'])
  73. ->setDefault([
  74. // PHPDocumentor 1
  75. 'abstract',
  76. 'access',
  77. 'code',
  78. 'deprec',
  79. 'encode',
  80. 'exception',
  81. 'final',
  82. 'ingroup',
  83. 'inheritdoc',
  84. 'inheritDoc',
  85. 'magic',
  86. 'name',
  87. 'toc',
  88. 'tutorial',
  89. 'private',
  90. 'static',
  91. 'staticvar',
  92. 'staticVar',
  93. 'throw',
  94. // PHPDocumentor 2
  95. 'api',
  96. 'author',
  97. 'category',
  98. 'copyright',
  99. 'deprecated',
  100. 'example',
  101. 'filesource',
  102. 'global',
  103. 'ignore',
  104. 'internal',
  105. 'license',
  106. 'link',
  107. 'method',
  108. 'package',
  109. 'param',
  110. 'property',
  111. 'property-read',
  112. 'property-write',
  113. 'return',
  114. 'see',
  115. 'since',
  116. 'source',
  117. 'subpackage',
  118. 'throws',
  119. 'todo',
  120. 'TODO',
  121. 'usedBy',
  122. 'uses',
  123. 'var',
  124. 'version',
  125. // PHPUnit
  126. 'after',
  127. 'afterClass',
  128. 'backupGlobals',
  129. 'backupStaticAttributes',
  130. 'before',
  131. 'beforeClass',
  132. 'codeCoverageIgnore',
  133. 'codeCoverageIgnoreStart',
  134. 'codeCoverageIgnoreEnd',
  135. 'covers',
  136. 'coversDefaultClass',
  137. 'coversNothing',
  138. 'dataProvider',
  139. 'depends',
  140. 'expectedException',
  141. 'expectedExceptionCode',
  142. 'expectedExceptionMessage',
  143. 'expectedExceptionMessageRegExp',
  144. 'group',
  145. 'large',
  146. 'medium',
  147. 'preserveGlobalState',
  148. 'requires',
  149. 'runTestsInSeparateProcesses',
  150. 'runInSeparateProcess',
  151. 'small',
  152. 'test',
  153. 'testdox',
  154. 'ticket',
  155. 'uses',
  156. // PHPCheckStyle
  157. 'SuppressWarnings',
  158. // PHPStorm
  159. 'noinspection',
  160. // PEAR
  161. 'package_version',
  162. // PlantUML
  163. 'enduml',
  164. 'startuml',
  165. // Psalm
  166. 'psalm',
  167. // PHPStan
  168. 'phpstan',
  169. 'template',
  170. // other
  171. 'fix',
  172. 'FIXME',
  173. 'fixme',
  174. 'override',
  175. ])
  176. ->getOption(),
  177. ]);
  178. }
  179. private function nextElementAcceptsDoctrineAnnotations(Tokens $tokens, int $index): bool
  180. {
  181. $classModifiers = [T_ABSTRACT, T_FINAL];
  182. if (\defined('T_READONLY')) { // @TODO: drop condition when PHP 8.2+ is required
  183. $classModifiers[] = T_READONLY;
  184. }
  185. do {
  186. $index = $tokens->getNextMeaningfulToken($index);
  187. if (null === $index) {
  188. return false;
  189. }
  190. } while ($tokens[$index]->isGivenKind($classModifiers));
  191. if ($tokens[$index]->isGivenKind(T_CLASS)) {
  192. return true;
  193. }
  194. $modifierKinds = [T_PUBLIC, T_PROTECTED, T_PRIVATE, T_FINAL, T_ABSTRACT, T_NS_SEPARATOR, T_STRING, CT::T_NULLABLE_TYPE];
  195. if (\defined('T_READONLY')) { // @TODO: drop condition when PHP 8.1+ is required
  196. $modifierKinds[] = T_READONLY;
  197. }
  198. while ($tokens[$index]->isGivenKind($modifierKinds)) {
  199. $index = $tokens->getNextMeaningfulToken($index);
  200. }
  201. if (!isset($this->classyElements[$index])) {
  202. return false;
  203. }
  204. return $tokens[$this->classyElements[$index]['classIndex']]->isGivenKind(T_CLASS); // interface, enums and traits cannot have doctrine annotations
  205. }
  206. }