ConfigurationResolver.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /*
  3. * This file is part of the PHP CS utility.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Symfony\CS;
  11. /**
  12. * The resolver that resolves configuration to use by command line options and config.
  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. * @internal
  19. */
  20. class ConfigurationResolver
  21. {
  22. protected $allFixers;
  23. protected $config;
  24. protected $fixers = array();
  25. protected $options = array(
  26. 'fixers' => null,
  27. 'level' => null,
  28. 'progress' => null,
  29. );
  30. public function setAllFixers(array $allFixers)
  31. {
  32. $this->allFixers = $allFixers;
  33. return $this;
  34. }
  35. public function setConfig(ConfigInterface $config)
  36. {
  37. $this->config = $config;
  38. return $this;
  39. }
  40. public function setOption($name, $value)
  41. {
  42. $this->options[$name] = $value;
  43. return $this;
  44. }
  45. public function setOptions(array $options)
  46. {
  47. foreach ($options as $name => $value) {
  48. $this->setOption($name, $value);
  49. }
  50. return $this;
  51. }
  52. /**
  53. * Resolves fixers.
  54. *
  55. * @return ConfigurationResolver
  56. */
  57. public function resolve()
  58. {
  59. $this->resolveByLevel();
  60. $this->resolveByNames();
  61. return $this;
  62. }
  63. /**
  64. * Returns fixers.
  65. *
  66. * @return FixerInterface[] An array of FixerInterface
  67. */
  68. public function getFixers()
  69. {
  70. return $this->fixers;
  71. }
  72. public function getProgress()
  73. {
  74. return $this->options['progress'] && !$this->config->getHideProgress();
  75. }
  76. protected function resolveByLevel()
  77. {
  78. $level = $this->parseLevel();
  79. if (null === $level) {
  80. return;
  81. }
  82. $fixers = array();
  83. foreach ($this->allFixers as $fixer) {
  84. if ($fixer->getLevel() === ($fixer->getLevel() & $level)) {
  85. $fixers[] = $fixer;
  86. }
  87. }
  88. $this->fixers = $fixers;
  89. }
  90. protected function resolveByNames()
  91. {
  92. $names = $this->parseFixers();
  93. if (null === $names) {
  94. return;
  95. }
  96. $addNames = array();
  97. $removeNames = array();
  98. foreach ($names as $name) {
  99. if (0 === strpos($name, '-')) {
  100. $removeNames[ltrim($name, '-')] = true;
  101. } else {
  102. $addNames[$name] = true;
  103. }
  104. }
  105. foreach ($this->fixers as $key => $fixer) {
  106. if (isset($removeNames[$fixer->getName()])) {
  107. unset($this->fixers[$key]);
  108. }
  109. }
  110. foreach ($this->allFixers as $fixer) {
  111. if (isset($addNames[$fixer->getName()]) && !in_array($fixer, $this->fixers, true)) {
  112. $this->fixers[] = $fixer;
  113. }
  114. }
  115. }
  116. protected function parseLevel()
  117. {
  118. static $levelMap = array(
  119. 'none' => FixerInterface::NONE_LEVEL,
  120. 'psr0' => FixerInterface::PSR0_LEVEL,
  121. 'psr1' => FixerInterface::PSR1_LEVEL,
  122. 'psr2' => FixerInterface::PSR2_LEVEL,
  123. 'symfony' => FixerInterface::SYMFONY_LEVEL,
  124. );
  125. $levelOption = $this->options['level'];
  126. if (null !== $levelOption) {
  127. if (!isset($levelMap[$levelOption])) {
  128. throw new \InvalidArgumentException(sprintf('The level "%s" is not defined.', $levelOption));
  129. }
  130. return $levelMap[$levelOption];
  131. }
  132. if (null === $this->options['fixers']) {
  133. return $this->config->getLevel();
  134. }
  135. foreach ($this->parseFixers() as $fixer) {
  136. if (0 === strpos($fixer, '-')) {
  137. return $this->config->getLevel();
  138. }
  139. }
  140. return;
  141. }
  142. protected function parseFixers()
  143. {
  144. if (null !== $this->options['fixers']) {
  145. return array_map('trim', explode(',', $this->options['fixers']));
  146. }
  147. if (null === $this->options['level']) {
  148. return $this->config->getFixers();
  149. }
  150. return;
  151. }
  152. }