AbstractProxyFixer.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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\Fixer\FixerInterface;
  14. use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
  15. use PhpCsFixer\Tokenizer\Tokens;
  16. /**
  17. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  18. *
  19. * @internal
  20. */
  21. abstract class AbstractProxyFixer extends AbstractFixer
  22. {
  23. /**
  24. * @var array<string, FixerInterface>
  25. */
  26. protected array $proxyFixers = [];
  27. public function __construct()
  28. {
  29. foreach (Utils::sortFixers($this->createProxyFixers()) as $proxyFixer) {
  30. $this->proxyFixers[$proxyFixer->getName()] = $proxyFixer;
  31. }
  32. parent::__construct();
  33. }
  34. public function isCandidate(Tokens $tokens): bool
  35. {
  36. foreach ($this->proxyFixers as $fixer) {
  37. if ($fixer->isCandidate($tokens)) {
  38. return true;
  39. }
  40. }
  41. return false;
  42. }
  43. public function isRisky(): bool
  44. {
  45. foreach ($this->proxyFixers as $fixer) {
  46. if ($fixer->isRisky()) {
  47. return true;
  48. }
  49. }
  50. return false;
  51. }
  52. public function getPriority(): int
  53. {
  54. if (\count($this->proxyFixers) > 1) {
  55. throw new \LogicException('You need to override this method to provide the priority of combined fixers.');
  56. }
  57. return reset($this->proxyFixers)->getPriority();
  58. }
  59. public function supports(\SplFileInfo $file): bool
  60. {
  61. foreach ($this->proxyFixers as $fixer) {
  62. if ($fixer->supports($file)) {
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68. public function setWhitespacesConfig(WhitespacesFixerConfig $config): void
  69. {
  70. parent::setWhitespacesConfig($config);
  71. foreach ($this->proxyFixers as $fixer) {
  72. if ($fixer instanceof WhitespacesAwareFixerInterface) {
  73. $fixer->setWhitespacesConfig($config);
  74. }
  75. }
  76. }
  77. protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
  78. {
  79. foreach ($this->proxyFixers as $fixer) {
  80. $fixer->fix($file, $tokens);
  81. }
  82. }
  83. /**
  84. * @return list<FixerInterface>
  85. */
  86. abstract protected function createProxyFixers(): array;
  87. }