SingleTraitInsertPerStatementFixer.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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\Fixer\ClassNotation;
  13. use PhpCsFixer\AbstractFixer;
  14. use PhpCsFixer\FixerDefinition\CodeSample;
  15. use PhpCsFixer\FixerDefinition\FixerDefinition;
  16. use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
  17. use PhpCsFixer\Preg;
  18. use PhpCsFixer\Tokenizer\CT;
  19. use PhpCsFixer\Tokenizer\Token;
  20. use PhpCsFixer\Tokenizer\Tokens;
  21. final class SingleTraitInsertPerStatementFixer extends AbstractFixer
  22. {
  23. public function getDefinition(): FixerDefinitionInterface
  24. {
  25. return new FixerDefinition(
  26. 'Each trait `use` must be done as single statement.',
  27. [
  28. new CodeSample(
  29. '<?php
  30. final class Example
  31. {
  32. use Foo, Bar;
  33. }
  34. '
  35. ),
  36. ]
  37. );
  38. }
  39. /**
  40. * {@inheritdoc}
  41. *
  42. * Must run before BracesFixer, SpaceAfterSemicolonFixer.
  43. */
  44. public function getPriority(): int
  45. {
  46. return 36;
  47. }
  48. public function isCandidate(Tokens $tokens): bool
  49. {
  50. return $tokens->isTokenKindFound(CT::T_USE_TRAIT);
  51. }
  52. protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
  53. {
  54. for ($index = \count($tokens) - 1; 1 < $index; --$index) {
  55. if ($tokens[$index]->isGivenKind(CT::T_USE_TRAIT)) {
  56. $candidates = $this->getCandidates($tokens, $index);
  57. if (\count($candidates) > 0) {
  58. $this->fixTraitUse($tokens, $index, $candidates);
  59. }
  60. }
  61. }
  62. }
  63. /**
  64. * @param list<int> $candidates ',' indices to fix
  65. */
  66. private function fixTraitUse(Tokens $tokens, int $useTraitIndex, array $candidates): void
  67. {
  68. foreach ($candidates as $commaIndex) {
  69. $inserts = [
  70. new Token([CT::T_USE_TRAIT, 'use']),
  71. new Token([T_WHITESPACE, ' ']),
  72. ];
  73. $nextImportStartIndex = $tokens->getNextMeaningfulToken($commaIndex);
  74. if ($tokens[$nextImportStartIndex - 1]->isWhitespace()) {
  75. if (Preg::match('/\R/', $tokens[$nextImportStartIndex - 1]->getContent())) {
  76. array_unshift($inserts, clone $tokens[$useTraitIndex - 1]);
  77. }
  78. $tokens->clearAt($nextImportStartIndex - 1);
  79. }
  80. $tokens[$commaIndex] = new Token(';');
  81. $tokens->insertAt($nextImportStartIndex, $inserts);
  82. }
  83. }
  84. /**
  85. * @return list<int>
  86. */
  87. private function getCandidates(Tokens $tokens, int $index): array
  88. {
  89. $indices = [];
  90. $index = $tokens->getNextTokenOfKind($index, [',', ';', '{']);
  91. while (!$tokens[$index]->equals(';')) {
  92. if ($tokens[$index]->equals('{')) {
  93. return []; // do not fix use cases with grouping
  94. }
  95. $indices[] = $index;
  96. $index = $tokens->getNextTokenOfKind($index, [',', ';', '{']);
  97. }
  98. return array_reverse($indices);
  99. }
  100. }