Skip to content

Commit

Permalink
FixersResolver - make class more state aware and enhance it's tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keradus authored and fivestar committed Oct 29, 2014
1 parent 6e60a76 commit 6f0cb71
Show file tree
Hide file tree
Showing 3 changed files with 207 additions and 82 deletions.
8 changes: 6 additions & 2 deletions Symfony/CS/Console/Command/FixCommand.php
Expand Up @@ -296,8 +296,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
// register custom fixers from config
$this->fixer->registerCustomFixers($config->getCustomFixers());

$resolver = new FixersResolver($this->fixer->getFixers(), $config);
$resolver->resolve($input->getOption('level'), $input->getOption('fixers'));
$resolver = new FixersResolver($this->fixer->getFixers());
$resolver
->setConfig($config)
->setOption('level', $input->getOption('level'))
->setOption('fixers', $input->getOption('fixers'))
->resolve();

$config->fixers($resolver->getFixers());

Expand Down
56 changes: 37 additions & 19 deletions Symfony/CS/FixersResolver.php
Expand Up @@ -20,30 +20,44 @@
*/
class FixersResolver
{
protected $fixers = array();
protected $allFixers;
protected $config;
protected $fixers = array();
protected $options = array(
'fixers' => null,
'level' => null,
);

public function __construct(array $allFixers, ConfigInterface $config)
public function __construct(array $allFixers)
{
$this->allFixers = $allFixers;
}

public function setConfig(ConfigInterface $config)
{
$this->config = $config;

return $this;
}

public function setOption($name, $value)
{
$this->options[$name] = $value;

return $this;
}

/**
* Resolves fixers.
*
* @param string $levelOption
* @param string $fixerOption
*
* @return array An array of FixerInterface
* @return FixersResolver
*/
public function resolve($levelOption, $fixerOption)
public function resolve()
{
$this->resolveByLevel($levelOption, $fixerOption);
$this->resolveByNames($fixerOption);
$this->resolveByLevel();
$this->resolveByNames();

return $this->getFixers();
return $this;
}

/**
Expand All @@ -56,9 +70,9 @@ public function getFixers()
return $this->fixers;
}

protected function resolveByLevel($levelOption, $fixerOption)
protected function resolveByLevel()
{
$level = $this->parseLevelOption($levelOption, $fixerOption);
$level = $this->parseLevelOption();

if (null === $level) {
return;
Expand All @@ -75,9 +89,9 @@ protected function resolveByLevel($levelOption, $fixerOption)
$this->fixers = $fixers;
}

protected function resolveByNames($fixerOption)
protected function resolveByNames()
{
$names = $this->parseFixerOption($fixerOption);
$names = $this->parseFixersOption();

$addNames = array();
$removeNames = array();
Expand All @@ -102,7 +116,7 @@ protected function resolveByNames($fixerOption)
}
}

protected function parseLevelOption($levelOption, $fixerOption)
protected function parseLevelOption()
{
static $levelMap = array(
'psr0' => FixerInterface::PSR0_LEVEL,
Expand All @@ -111,6 +125,8 @@ protected function parseLevelOption($levelOption, $fixerOption)
'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));
Expand All @@ -119,7 +135,7 @@ protected function parseLevelOption($levelOption, $fixerOption)
return $levelMap[$levelOption];
}

$names = $this->parseFixerOption($fixerOption);
$names = $this->parseFixersOption();

if (empty($names)) {
return $this->config->getLevel();
Expand All @@ -134,12 +150,14 @@ protected function parseLevelOption($levelOption, $fixerOption)
return null;
}

protected function parseFixerOption($fixerOption)
protected function parseFixersOption()
{
if (null === $fixerOption) {
$fixersOption = $this->options['fixers'];

if (null === $fixersOption) {
return $this->config->getFixers();
}

return array_map('trim', explode(',', $fixerOption));
return array_map('trim', explode(',', $fixersOption));
}
}

0 comments on commit 6f0cb71

Please sign in to comment.