Config.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. /*
  3. * This file is part of PHP CS Fixer.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. * Dariusz Rumiński <dariusz.ruminski@gmail.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. namespace PhpCsFixer;
  12. use PhpCsFixer\Fixer\FixerInterface;
  13. /**
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. * @author Katsuhiro Ogawa <ko.fivestar@gmail.com>
  16. * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
  17. */
  18. class Config implements ConfigInterface
  19. {
  20. private $cacheFile = '.php_cs.cache';
  21. private $customFixers = array();
  22. private $finder;
  23. private $format = 'txt';
  24. private $hideProgress = false;
  25. private $indent = ' ';
  26. private $isRiskyAllowed = false;
  27. private $lineEnding = "\n";
  28. private $name;
  29. private $phpExecutable;
  30. private $rules = array('@PSR2' => true);
  31. private $usingCache = true;
  32. public function __construct($name = 'default')
  33. {
  34. $this->name = $name;
  35. }
  36. /**
  37. * @return static
  38. */
  39. public static function create()
  40. {
  41. return new static();
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function getCacheFile()
  47. {
  48. return $this->cacheFile;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getCustomFixers()
  54. {
  55. return $this->customFixers;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function getFinder()
  61. {
  62. if (null === $this->finder) {
  63. $this->finder = new Finder();
  64. }
  65. return $this->finder;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. */
  70. public function getFormat()
  71. {
  72. return $this->format;
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function getHideProgress()
  78. {
  79. return $this->hideProgress;
  80. }
  81. /**
  82. * {@inheritdoc}
  83. */
  84. public function getIndent()
  85. {
  86. return $this->indent;
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function getLineEnding()
  92. {
  93. return $this->lineEnding;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function getName()
  99. {
  100. return $this->name;
  101. }
  102. /**
  103. * {@inheritdoc}
  104. */
  105. public function getPhpExecutable()
  106. {
  107. return $this->phpExecutable;
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function getRiskyAllowed()
  113. {
  114. return $this->isRiskyAllowed;
  115. }
  116. /**
  117. * {@inheritdoc}
  118. */
  119. public function getRules()
  120. {
  121. return $this->rules;
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function getUsingCache()
  127. {
  128. return $this->usingCache;
  129. }
  130. /**
  131. * {@inheritdoc}
  132. */
  133. public function registerCustomFixers($fixers)
  134. {
  135. if (false === is_array($fixers) && false === $fixers instanceof \Traversable) {
  136. throw new \InvalidArgumentException(sprintf(
  137. 'Argument must be an array or a Traversable, got "%s".',
  138. is_object($fixers) ? get_class($fixers) : gettype($fixers)
  139. ));
  140. }
  141. foreach ($fixers as $fixer) {
  142. $this->addCustomFixer($fixer);
  143. }
  144. return $this;
  145. }
  146. /**
  147. * {@inheritdoc}
  148. */
  149. public function setCacheFile($cacheFile)
  150. {
  151. $this->cacheFile = $cacheFile;
  152. return $this;
  153. }
  154. /**
  155. * {@inheritdoc}
  156. */
  157. public function setFinder($finder)
  158. {
  159. if (false === is_array($finder) && false === $finder instanceof \Traversable) {
  160. throw new \InvalidArgumentException(sprintf(
  161. 'Argument must be an array or a Traversable, got "%s".',
  162. is_object($finder) ? get_class($finder) : gettype($finder)
  163. ));
  164. }
  165. $this->finder = $finder;
  166. return $this;
  167. }
  168. /**
  169. * {@inheritdoc}
  170. */
  171. public function setFormat($format)
  172. {
  173. $this->format = $format;
  174. return $this;
  175. }
  176. /**
  177. * {@inheritdoc}
  178. */
  179. public function setHideProgress($hideProgress)
  180. {
  181. $this->hideProgress = $hideProgress;
  182. return $this;
  183. }
  184. /**
  185. * {@inheritdoc}
  186. */
  187. public function setIndent($indent)
  188. {
  189. $this->indent = $indent;
  190. return $this;
  191. }
  192. /**
  193. * {@inheritdoc}
  194. */
  195. public function setLineEnding($lineEnding)
  196. {
  197. $this->lineEnding = $lineEnding;
  198. return $this;
  199. }
  200. /**
  201. * {@inheritdoc}
  202. */
  203. public function setPhpExecutable($phpExecutable)
  204. {
  205. $this->phpExecutable = $phpExecutable;
  206. return $this;
  207. }
  208. /**
  209. * {@inheritdoc}
  210. */
  211. public function setRiskyAllowed($isRiskyAllowed)
  212. {
  213. $this->isRiskyAllowed = $isRiskyAllowed;
  214. return $this;
  215. }
  216. /**
  217. * {@inheritdoc}
  218. */
  219. public function setRules(array $rules)
  220. {
  221. $this->rules = $rules;
  222. return $this;
  223. }
  224. /**
  225. * {@inheritdoc}
  226. */
  227. public function setUsingCache($usingCache)
  228. {
  229. $this->usingCache = $usingCache;
  230. return $this;
  231. }
  232. private function addCustomFixer(FixerInterface $fixer)
  233. {
  234. $this->customFixers[] = $fixer;
  235. }
  236. }