Config.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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\Runner\Parallel\ParallelConfig;
  15. use PhpCsFixer\Runner\Parallel\ParallelConfigFactory;
  16. /**
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. * @author Katsuhiro Ogawa <ko.fivestar@gmail.com>
  19. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  20. */
  21. class Config implements ConfigInterface, ParallelAwareConfigInterface
  22. {
  23. private string $cacheFile = '.php-cs-fixer.cache';
  24. /**
  25. * @var list<FixerInterface>
  26. */
  27. private array $customFixers = [];
  28. /**
  29. * @var null|iterable<\SplFileInfo>
  30. */
  31. private ?iterable $finder = null;
  32. private string $format = 'txt';
  33. private bool $hideProgress = false;
  34. private string $indent = ' ';
  35. private bool $isRiskyAllowed = false;
  36. private string $lineEnding = "\n";
  37. private string $name;
  38. private ParallelConfig $parallelConfig;
  39. /**
  40. * @var null|string
  41. */
  42. private $phpExecutable;
  43. /**
  44. * @TODO: 4.0 - update to @PER
  45. *
  46. * @var array<string, array<string, mixed>|bool>
  47. */
  48. private array $rules;
  49. private bool $usingCache = true;
  50. public function __construct(string $name = 'default')
  51. {
  52. // @TODO 4.0 cleanup
  53. if (Utils::isFutureModeEnabled()) {
  54. $this->name = $name.' (future mode)';
  55. $this->rules = ['@PER-CS' => true];
  56. } else {
  57. $this->name = $name;
  58. $this->rules = ['@PSR12' => true];
  59. }
  60. // @TODO 4.0 cleanup
  61. if (Utils::isFutureModeEnabled() || filter_var(getenv('PHP_CS_FIXER_PARALLEL'), FILTER_VALIDATE_BOOL)) {
  62. $this->parallelConfig = ParallelConfigFactory::detect();
  63. } else {
  64. $this->parallelConfig = ParallelConfigFactory::sequential();
  65. }
  66. }
  67. public function getCacheFile(): string
  68. {
  69. return $this->cacheFile;
  70. }
  71. public function getCustomFixers(): array
  72. {
  73. return $this->customFixers;
  74. }
  75. /**
  76. * @return Finder
  77. */
  78. public function getFinder(): iterable
  79. {
  80. $this->finder ??= new Finder();
  81. return $this->finder;
  82. }
  83. public function getFormat(): string
  84. {
  85. return $this->format;
  86. }
  87. public function getHideProgress(): bool
  88. {
  89. return $this->hideProgress;
  90. }
  91. public function getIndent(): string
  92. {
  93. return $this->indent;
  94. }
  95. public function getLineEnding(): string
  96. {
  97. return $this->lineEnding;
  98. }
  99. public function getName(): string
  100. {
  101. return $this->name;
  102. }
  103. public function getParallelConfig(): ParallelConfig
  104. {
  105. return $this->parallelConfig;
  106. }
  107. public function getPhpExecutable(): ?string
  108. {
  109. return $this->phpExecutable;
  110. }
  111. public function getRiskyAllowed(): bool
  112. {
  113. return $this->isRiskyAllowed;
  114. }
  115. public function getRules(): array
  116. {
  117. return $this->rules;
  118. }
  119. public function getUsingCache(): bool
  120. {
  121. return $this->usingCache;
  122. }
  123. public function registerCustomFixers(iterable $fixers): ConfigInterface
  124. {
  125. foreach ($fixers as $fixer) {
  126. $this->addCustomFixer($fixer);
  127. }
  128. return $this;
  129. }
  130. public function setCacheFile(string $cacheFile): ConfigInterface
  131. {
  132. $this->cacheFile = $cacheFile;
  133. return $this;
  134. }
  135. public function setFinder(iterable $finder): ConfigInterface
  136. {
  137. $this->finder = $finder;
  138. return $this;
  139. }
  140. public function setFormat(string $format): ConfigInterface
  141. {
  142. $this->format = $format;
  143. return $this;
  144. }
  145. public function setHideProgress(bool $hideProgress): ConfigInterface
  146. {
  147. $this->hideProgress = $hideProgress;
  148. return $this;
  149. }
  150. public function setIndent(string $indent): ConfigInterface
  151. {
  152. $this->indent = $indent;
  153. return $this;
  154. }
  155. public function setLineEnding(string $lineEnding): ConfigInterface
  156. {
  157. $this->lineEnding = $lineEnding;
  158. return $this;
  159. }
  160. public function setParallelConfig(ParallelConfig $config): ConfigInterface
  161. {
  162. $this->parallelConfig = $config;
  163. return $this;
  164. }
  165. public function setPhpExecutable(?string $phpExecutable): ConfigInterface
  166. {
  167. $this->phpExecutable = $phpExecutable;
  168. return $this;
  169. }
  170. public function setRiskyAllowed(bool $isRiskyAllowed): ConfigInterface
  171. {
  172. $this->isRiskyAllowed = $isRiskyAllowed;
  173. return $this;
  174. }
  175. public function setRules(array $rules): ConfigInterface
  176. {
  177. $this->rules = $rules;
  178. return $this;
  179. }
  180. public function setUsingCache(bool $usingCache): ConfigInterface
  181. {
  182. $this->usingCache = $usingCache;
  183. return $this;
  184. }
  185. private function addCustomFixer(FixerInterface $fixer): void
  186. {
  187. $this->customFixers[] = $fixer;
  188. }
  189. }