diff --git a/Symfony/CS/Console/Command/FixCommand.php b/Symfony/CS/Console/Command/FixCommand.php index 06cd9821f11..433af1c5555 100644 --- a/Symfony/CS/Console/Command/FixCommand.php +++ b/Symfony/CS/Console/Command/FixCommand.php @@ -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()); diff --git a/Symfony/CS/FixersResolver.php b/Symfony/CS/FixersResolver.php index d7abd5c847a..be7ace4d3c0 100644 --- a/Symfony/CS/FixersResolver.php +++ b/Symfony/CS/FixersResolver.php @@ -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; } /** @@ -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; @@ -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(); @@ -102,7 +116,7 @@ protected function resolveByNames($fixerOption) } } - protected function parseLevelOption($levelOption, $fixerOption) + protected function parseLevelOption() { static $levelMap = array( 'psr0' => FixerInterface::PSR0_LEVEL, @@ -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)); @@ -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(); @@ -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)); } } diff --git a/Symfony/CS/Tests/FixersResolverTest.php b/Symfony/CS/Tests/FixersResolverTest.php index 961375538a6..64cb548a9af 100644 --- a/Symfony/CS/Tests/FixersResolverTest.php +++ b/Symfony/CS/Tests/FixersResolverTest.php @@ -16,10 +16,15 @@ use Symfony\CS\FixerInterface; use Symfony\CS\FixersResolver; +/** + * @author Katsuhiro Ogawa + * @author Dariusz RumiƄski + */ class FixersResolverTest extends \PHPUnit_Framework_TestCase { protected $config; protected $resolver; + protected $fixersMap; protected function setUp() { @@ -27,102 +32,200 @@ protected function setUp() $fixer->registerBuiltInFixers(); $fixer->registerBuiltInConfigs(); - $this->config = new Config(); + $fixersMap = array(); - $this->resolver = new FixersResolver($fixer->getFixers(), $this->config); - } + foreach ($fixer->getFixers() as $singleFixer) { + $level = $singleFixer->getLevel(); - public function testGetFixers() - { - $fixers = $this->resolver->resolve('psr1', null); + if (!isset($fixersMap[$level])) { + $fixersMap[$level] = array(); + } - $this->assertEquals($fixers, $this->resolver->getFixers()); - } + $fixersMap[$level][$singleFixer->getName()] = $singleFixer; + } - public function testGetFixersReturnsEmptyArrayByDefault() - { - $this->assertEquals(array(), $this->resolver->getFixers()); + $this->fixersMap = $fixersMap; + + $this->config = new Config(); + $this->resolver = new FixersResolver($fixer->getFixers()); + $this->resolver->setConfig($this->config); } - public function testResolveWithOptionLevel() + protected function makeTest($expectedFixers, $resolvedFixers) { - $fixers = $this->resolver->resolve('psr1', null); + $this->assertCount(count($expectedFixers), $resolvedFixers); - foreach ($fixers as $fixer) { - $this->assertTrue(in_array($fixer->getLevel(), array(FixerInterface::PSR0_LEVEL, FixerInterface::PSR1_LEVEL), true)); + foreach ($expectedFixers as $fixer) { + $this->assertContains($fixer, $resolvedFixers); } } - public function testResolveWithConfigLevel() + public function testResolveFixersReturnsEmptyArrayByDefault() + { + $this->makeTest(array(), $this->resolver->getFixers()); + } + + public function testResolveFixersWithLevelConfig() { $this->config->level(FixerInterface::PSR1_LEVEL); - $fixers = $this->resolver->resolve(null, null); + $this->resolver->resolve(); - foreach ($fixers as $fixer) { - $this->assertTrue(in_array($fixer->getLevel(), array(FixerInterface::PSR0_LEVEL, FixerInterface::PSR1_LEVEL), true)); - } + $this->makeTest( + array_merge($this->fixersMap[FixerInterface::PSR0_LEVEL], $this->fixersMap[FixerInterface::PSR1_LEVEL]), + $this->resolver->getFixers() + ); } - public function testResolveWithIncludeOptionFixers() + public function testResolveFixersWithPositiveFixersConfig() { - $fixers = $this->resolver->resolve(null, 'encoding, php_closing_tag'); - - foreach ($fixers as $fixer) { - $this->assertTrue(in_array($fixer->getName(), array('encoding', 'php_closing_tag'), true)); - } + $this->config->level(FixerInterface::SYMFONY_LEVEL); + $this->config->fixers(array('strict', 'strict_param')); + + $this->resolver->resolve(); + + $expectedFixers = array_merge( + $this->fixersMap[FixerInterface::PSR0_LEVEL], + $this->fixersMap[FixerInterface::PSR1_LEVEL], + $this->fixersMap[FixerInterface::PSR2_LEVEL], + $this->fixersMap[FixerInterface::SYMFONY_LEVEL], + array($this->fixersMap[FixerInterface::CONTRIB_LEVEL]['strict'], $this->fixersMap[FixerInterface::CONTRIB_LEVEL]['strict_param']) + ); + + $this->makeTest( + $expectedFixers, + $this->resolver->getFixers() + ); } - public function testResolveWithConfigFixers() + public function testResolveFixersWithNegativeFixersConfig() { - $this->config->fixers(array('encoding', 'php_closing_tag')); - - $fixers = $this->resolver->resolve(null, null); - - foreach ($fixers as $fixer) { - $this->assertTrue(in_array($fixer->getName(), array('encoding', 'php_closing_tag'), true)); + $this->config->level(FixerInterface::SYMFONY_LEVEL); + $this->config->fixers(array('strict', '-include', 'strict_param')); + + $this->resolver->resolve(); + + $expectedFixers = array_merge( + $this->fixersMap[FixerInterface::PSR0_LEVEL], + $this->fixersMap[FixerInterface::PSR1_LEVEL], + $this->fixersMap[FixerInterface::PSR2_LEVEL], + $this->fixersMap[FixerInterface::SYMFONY_LEVEL], + array($this->fixersMap[FixerInterface::CONTRIB_LEVEL]['strict'], $this->fixersMap[FixerInterface::CONTRIB_LEVEL]['strict_param']) + ); + + foreach ($expectedFixers as $key => $fixer) { + if ($fixer->getName() === 'include') { + unset($expectedFixers[$key]); + break; + } } + + $this->makeTest( + $expectedFixers, + $this->resolver->getFixers() + ); } - public function testResolveWithConfigLevelAndExcludeOptionFixers() + public function testResolveFixersWithLevelOption() { - $this->config->level('psr1'); - - $fixers = $this->resolver->resolve(null, '-encoding'); + $this->resolver + ->setOption('level', 'psr1') + ->resolve(); + + $this->makeTest( + array_merge($this->fixersMap[FixerInterface::PSR0_LEVEL], $this->fixersMap[FixerInterface::PSR1_LEVEL]), + $this->resolver->getFixers() + ); + } - $enabledEncoding = false; + public function testResolveFixersWithLevelConfigAndFixersConfigAndLevelOption() + { + $this->config + ->level(FixerInterface::PSR2_LEVEL) + ->fixers(array('strict')); + $this->resolver + ->setOption('level', 'psr1') + ->resolve(); + + $this->makeTest( + array_merge($this->fixersMap[FixerInterface::PSR0_LEVEL], $this->fixersMap[FixerInterface::PSR1_LEVEL]), + $this->resolver->getFixers() + ); + } - foreach ($fixers as $fixer) { - $this->assertTrue(in_array($fixer->getLevel(), array(FixerInterface::PSR0_LEVEL, FixerInterface::PSR1_LEVEL), true)); + public function testResolveFixersWithLevelConfigAndFixersConfigAndPositiveFixersOption() + { + $this->config + ->level(FixerInterface::PSR2_LEVEL) + ->fixers(array('strict')); + $this->resolver + ->setOption('fixers', 'psr0') + ->resolve(); + + $this->makeTest( + array($this->fixersMap[FixerInterface::PSR0_LEVEL]['psr0']), + $this->resolver->getFixers() + ); + } - if ($fixer->getName() === 'encoding') { - $this->fail(); + public function testResolveFixersWithLevelConfigAndFixersConfigAndNegativeFixersOption() + { + $this->config + ->level(FixerInterface::SYMFONY_LEVEL) + ->fixers(array('strict')); + $this->resolver + ->setOption('fixers', 'strict, -include,strict_param ') + ->resolve(); + + $expectedFixers = array_merge( + $this->fixersMap[FixerInterface::PSR0_LEVEL], + $this->fixersMap[FixerInterface::PSR1_LEVEL], + $this->fixersMap[FixerInterface::PSR2_LEVEL], + $this->fixersMap[FixerInterface::SYMFONY_LEVEL], + array($this->fixersMap[FixerInterface::CONTRIB_LEVEL]['strict'], $this->fixersMap[FixerInterface::CONTRIB_LEVEL]['strict_param']) + ); + + foreach ($expectedFixers as $key => $fixer) { + if ($fixer->getName() === 'include') { + unset($expectedFixers[$key]); + break; } } + + $this->makeTest( + $expectedFixers, + $this->resolver->getFixers() + ); } - public function testResolveWithOptionLevelAndIncludeAndExcludeOptionFixers() + public function testResolveFixersWithLevelConfigAndFixersConfigAndLevelOptionsAndFixersOption() { - $fixers = $this->resolver->resolve('psr1', '-encoding,php_closing_tag'); - - $enabledEncoding = false; - $enabledPhpClosingTag = false; - - foreach ($fixers as $fixer) { - switch ($fixer->getName()) { - case 'encoding': // psr1 - $enabledEncoding = true; - break; - case 'php_closing_tag': // psr2 - $enabledPhpClosingTag = true; - break; - default: - $this->assertTrue(in_array($fixer->getLevel(), array(FixerInterface::PSR0_LEVEL, FixerInterface::PSR1_LEVEL), true)); - break; + $this->config + ->level(FixerInterface::PSR2_LEVEL) + ->fixers(array('concat_with_spaces')); + $this->resolver + ->setOption('level', 'symfony') + ->setOption('fixers', 'strict, -include,strict_param ') + ->resolve(); + + $expectedFixers = array_merge( + $this->fixersMap[FixerInterface::PSR0_LEVEL], + $this->fixersMap[FixerInterface::PSR1_LEVEL], + $this->fixersMap[FixerInterface::PSR2_LEVEL], + $this->fixersMap[FixerInterface::SYMFONY_LEVEL], + array($this->fixersMap[FixerInterface::CONTRIB_LEVEL]['strict'], $this->fixersMap[FixerInterface::CONTRIB_LEVEL]['strict_param']) + ); + + foreach ($expectedFixers as $key => $fixer) { + if ($fixer->getName() === 'include') { + unset($expectedFixers[$key]); + break; } } - $this->assertFalse($enabledEncoding); - $this->assertTrue($enabledPhpClosingTag); + $this->makeTest( + $expectedFixers, + $this->resolver->getFixers() + ); } }