123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187 |
- <?php
- /*
- * This file is part of the PHP CS utility.
- *
- * (c) Fabien Potencier <fabien@symfony.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- namespace Symfony\CS;
- /**
- * The resolver that resolves configuration to use by command line options and config.
- *
- * @author Fabien Potencier <fabien@symfony.com>
- * @author Katsuhiro Ogawa <ko.fivestar@gmail.com>
- * @author Dariusz Rumiński <dariusz.ruminski@gmail.com>
- *
- * @internal
- */
- class ConfigurationResolver
- {
- protected $allFixers;
- protected $config;
- protected $fixers = array();
- protected $options = array(
- 'fixers' => null,
- 'level' => null,
- 'progress' => null,
- );
- public function setAllFixers(array $allFixers)
- {
- $this->allFixers = $allFixers;
- return $this;
- }
- public function setConfig(ConfigInterface $config)
- {
- $this->config = $config;
- return $this;
- }
- public function setOption($name, $value)
- {
- $this->options[$name] = $value;
- return $this;
- }
- public function setOptions(array $options)
- {
- foreach ($options as $name => $value) {
- $this->setOption($name, $value);
- }
- return $this;
- }
- /**
- * Resolves fixers.
- *
- * @return ConfigurationResolver
- */
- public function resolve()
- {
- $this->resolveByLevel();
- $this->resolveByNames();
- return $this;
- }
- /**
- * Returns fixers.
- *
- * @return FixerInterface[] An array of FixerInterface
- */
- public function getFixers()
- {
- return $this->fixers;
- }
- public function getProgress()
- {
- return $this->options['progress'] && !$this->config->getHideProgress();
- }
- protected function resolveByLevel()
- {
- $level = $this->parseLevel();
- if (null === $level) {
- return;
- }
- $fixers = array();
- foreach ($this->allFixers as $fixer) {
- if ($fixer->getLevel() === ($fixer->getLevel() & $level)) {
- $fixers[] = $fixer;
- }
- }
- $this->fixers = $fixers;
- }
- protected function resolveByNames()
- {
- $names = $this->parseFixers();
- if (null === $names) {
- return;
- }
- $addNames = array();
- $removeNames = array();
- foreach ($names as $name) {
- if (0 === strpos($name, '-')) {
- $removeNames[ltrim($name, '-')] = true;
- } else {
- $addNames[$name] = true;
- }
- }
- foreach ($this->fixers as $key => $fixer) {
- if (isset($removeNames[$fixer->getName()])) {
- unset($this->fixers[$key]);
- }
- }
- foreach ($this->allFixers as $fixer) {
- if (isset($addNames[$fixer->getName()]) && !in_array($fixer, $this->fixers, true)) {
- $this->fixers[] = $fixer;
- }
- }
- }
- protected function parseLevel()
- {
- static $levelMap = array(
- 'none' => FixerInterface::NONE_LEVEL,
- 'psr0' => FixerInterface::PSR0_LEVEL,
- 'psr1' => FixerInterface::PSR1_LEVEL,
- 'psr2' => FixerInterface::PSR2_LEVEL,
- 'symfony' => FixerInterface::SYMFONY_LEVEL,
- );
- $levelOption = $this->options['level'];
- if (null !== $levelOption) {
- if (!isset($levelMap[$levelOption])) {
- throw new \InvalidArgumentException(sprintf('The level "%s" is not defined.', $levelOption));
- }
- return $levelMap[$levelOption];
- }
- if (null === $this->options['fixers']) {
- return $this->config->getLevel();
- }
- foreach ($this->parseFixers() as $fixer) {
- if (0 === strpos($fixer, '-')) {
- return $this->config->getLevel();
- }
- }
- return;
- }
- protected function parseFixers()
- {
- if (null !== $this->options['fixers']) {
- return array_map('trim', explode(',', $this->options['fixers']));
- }
- if (null === $this->options['level']) {
- return $this->config->getFixers();
- }
- return;
- }
- }
|