Config.php 5.0 KB

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