AbstractFixer.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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\ConfigurationException\RequiredFixerConfigurationException;
  14. use PhpCsFixer\Fixer\ConfigurableFixerInterface;
  15. use PhpCsFixer\Fixer\FixerInterface;
  16. use PhpCsFixer\Fixer\WhitespacesAwareFixerInterface;
  17. use PhpCsFixer\Tokenizer\Tokens;
  18. /**
  19. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  20. *
  21. * @internal
  22. */
  23. abstract class AbstractFixer implements FixerInterface
  24. {
  25. /**
  26. * @var WhitespacesFixerConfig
  27. */
  28. protected $whitespacesConfig;
  29. public function __construct()
  30. {
  31. if ($this instanceof ConfigurableFixerInterface) {
  32. try {
  33. $this->configure([]);
  34. } catch (RequiredFixerConfigurationException $e) {
  35. // ignore
  36. }
  37. }
  38. if ($this instanceof WhitespacesAwareFixerInterface) {
  39. $this->whitespacesConfig = $this->getDefaultWhitespacesFixerConfig();
  40. }
  41. }
  42. final public function fix(\SplFileInfo $file, Tokens $tokens): void
  43. {
  44. if ($this instanceof ConfigurableFixerInterface && property_exists($this, 'configuration') && null === $this->configuration) {
  45. throw new RequiredFixerConfigurationException($this->getName(), 'Configuration is required.');
  46. }
  47. if (0 < $tokens->count() && $this->isCandidate($tokens) && $this->supports($file)) {
  48. $this->applyFix($file, $tokens);
  49. }
  50. }
  51. public function isRisky(): bool
  52. {
  53. return false;
  54. }
  55. public function getName(): string
  56. {
  57. $nameParts = explode('\\', static::class);
  58. $name = substr(end($nameParts), 0, -\strlen('Fixer'));
  59. return Utils::camelCaseToUnderscore($name);
  60. }
  61. public function getPriority(): int
  62. {
  63. return 0;
  64. }
  65. public function supports(\SplFileInfo $file): bool
  66. {
  67. return true;
  68. }
  69. public function setWhitespacesConfig(WhitespacesFixerConfig $config): void
  70. {
  71. if (!$this instanceof WhitespacesAwareFixerInterface) {
  72. throw new \LogicException('Cannot run method for class not implementing "PhpCsFixer\Fixer\WhitespacesAwareFixerInterface".');
  73. }
  74. $this->whitespacesConfig = $config;
  75. }
  76. abstract protected function applyFix(\SplFileInfo $file, Tokens $tokens): void;
  77. private function getDefaultWhitespacesFixerConfig(): WhitespacesFixerConfig
  78. {
  79. static $defaultWhitespacesFixerConfig = null;
  80. if (null === $defaultWhitespacesFixerConfig) {
  81. $defaultWhitespacesFixerConfig = new WhitespacesFixerConfig(' ', "\n");
  82. }
  83. return $defaultWhitespacesFixerConfig;
  84. }
  85. }