Skip to content

Commit

Permalink
Fix README and some codes.
Browse files Browse the repository at this point in the history
  • Loading branch information
fivestar committed Oct 29, 2014
1 parent 6876726 commit 6e60a76
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 11 deletions.
20 changes: 18 additions & 2 deletions README.rst
Expand Up @@ -343,7 +343,7 @@ and directories that need to be analyzed:
;
You may also use a blacklist for the Fixers instead of the above shown whitelist approach.
The following example shows how to use all PSR-2 Fixers but the `psr0` fixer.
The following example shows how to use all ``symfony`` Fixers but the `psr0` fixer.
Note the additional ``-`` in front of the Fixer name.

.. code-block:: php
Expand All @@ -356,11 +356,27 @@ Note the additional ``-`` in front of the Fixer name.
;
return Symfony\CS\Config\Config::create()
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
->fixers(array('-psr0'))
->finder($finder)
;
The ``symfony`` level is set by default, you can also change default level:

.. code-block:: php
<?php
return Symfony\CS\Config\Config::create()
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
;
In combination with these config and command line options, you can choose various usage.

For example, default level is ``symfony``, but if you also don't want to use ``psr0`` fixer, you can specify ``--fixers="-psr0"`` option.

But if you use ``--fixers`` option with only exact fixers,
only those exact fixers are enabled whether or not level is set.

With the ``--config-file`` option you can specify the path to the
``.php_cs`` file.

Expand Down
1 change: 1 addition & 0 deletions Symfony/CS/Config/Config.php
Expand Up @@ -18,6 +18,7 @@

/**
* @author Fabien Potencier <fabien@symfony.com>
* @author Katsuhiro Ogawa <ko.fivestar@gmail.com>
*/
class Config implements ConfigInterface
{
Expand Down
20 changes: 18 additions & 2 deletions Symfony/CS/Console/Command/FixCommand.php
Expand Up @@ -185,7 +185,7 @@ protected function configure()
?>
You may also use a blacklist for the Fixers instead of the above shown whitelist approach.
The following example shows how to use all PSR-2 Fixers but the `psr0` fixer.
The following example shows how to use all ``symfony`` Fixers but the `psr0` fixer.
Note the additional <comment>-</comment> in front of the Fixer name.
<?php
Expand All @@ -196,13 +196,29 @@ protected function configure()
;
return Symfony\CS\Config\Config::create()
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
->fixers(array('-psr0'))
->finder(\$finder)
;
?>
The ``symfony`` level is set by default, you can also change default level:
<?php
return Symfony\CS\Config\Config::create()
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
;
?>
In combination with these config and command line options, you can choose various usage.
For example, default level is ``symfony``, but if you also don't want to use ``psr0`` fixer, you can specify ``--fixers="-psr0"`` option.
But if you use ``--fixers`` option with only exact fixers,
only those exact fixers are enabled whether or not level is set.
With the <comment>--config-file</comment> option you can specify the path to the
<comment>.php_cs</comment> file.
EOF
Expand Down
18 changes: 16 additions & 2 deletions Symfony/CS/FixersResolver.php
Expand Up @@ -12,23 +12,32 @@
namespace Symfony\CS;

/**
* The resolver that resolves fixers 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>
*/
class FixersResolver
{
protected $fixers = array();
protected $allFixers;
protected $fixers;
protected $config;

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

/**
* Resolves fixers.
*
* @param string $levelOption
* @param string $fixerOption
*
* @return array An array of FixerInterface
*/
public function resolve($levelOption, $fixerOption)
{
$this->resolveByLevel($levelOption, $fixerOption);
Expand All @@ -37,6 +46,11 @@ public function resolve($levelOption, $fixerOption)
return $this->getFixers();
}

/**
* Returns fixers.
*
* @return array An array of FixerInterface
*/
public function getFixers()
{
return $this->fixers;
Expand Down
86 changes: 81 additions & 5 deletions Symfony/CS/Tests/FixersResolverTest.php
Expand Up @@ -13,22 +13,95 @@

use Symfony\CS\Config\Config;
use Symfony\CS\Fixer;
use Symfony\CS\FixerInterface;
use Symfony\CS\FixersResolver;

class FixersResolverTest extends \PHPUnit_Framework_TestCase
{
protected $config;
protected $resolver;

protected function setUp()
{
$this->fixer = new Fixer();
$this->fixer->registerBuiltInFixers();
$this->fixer->registerBuiltInConfigs();
$fixer = new Fixer();
$fixer->registerBuiltInFixers();
$fixer->registerBuiltInConfigs();

$this->config = new Config();

$this->resolver = new FixersResolver($this->fixer->getFixers(), $this->config);
$this->resolver = new FixersResolver($fixer->getFixers(), $this->config);
}

public function testGetFixers()
{
$fixers = $this->resolver->resolve('psr1', null);

$this->assertEquals($fixers, $this->resolver->getFixers());
}

public function testGetFixersReturnsEmptyArrayByDefault()
{
$this->assertEquals(array(), $this->resolver->getFixers());
}

public function testResolveWithOptionLevel()
{
$fixers = $this->resolver->resolve('psr1', null);

foreach ($fixers as $fixer) {
$this->assertTrue(in_array($fixer->getLevel(), array(FixerInterface::PSR0_LEVEL, FixerInterface::PSR1_LEVEL), true));
}
}

public function testResolveWithConfigLevel()
{
$this->config->level(FixerInterface::PSR1_LEVEL);

$fixers = $this->resolver->resolve(null, null);

foreach ($fixers as $fixer) {
$this->assertTrue(in_array($fixer->getLevel(), array(FixerInterface::PSR0_LEVEL, FixerInterface::PSR1_LEVEL), true));
}
}

public function testResolveWithIncludeOptionFixers()
{
$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));
}
}

public function testResolveWithConfigFixers()
{
$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));
}
}

public function testResolveWithIncludeAndExcludeNames()
public function testResolveWithConfigLevelAndExcludeOptionFixers()
{
$this->config->level('psr1');

$fixers = $this->resolver->resolve(null, '-encoding');

$enabledEncoding = false;

foreach ($fixers as $fixer) {
$this->assertTrue(in_array($fixer->getLevel(), array(FixerInterface::PSR0_LEVEL, FixerInterface::PSR1_LEVEL), true));

if ($fixer->getName() === 'encoding') {
$this->fail();
}
}
}

public function testResolveWithOptionLevelAndIncludeAndExcludeOptionFixers()
{
$fixers = $this->resolver->resolve('psr1', '-encoding,php_closing_tag');

Expand All @@ -43,6 +116,9 @@ public function testResolveWithIncludeAndExcludeNames()
case 'php_closing_tag': // psr2
$enabledPhpClosingTag = true;
break;
default:
$this->assertTrue(in_array($fixer->getLevel(), array(FixerInterface::PSR0_LEVEL, FixerInterface::PSR1_LEVEL), true));
break;
}
}

Expand Down

0 comments on commit 6e60a76

Please sign in to comment.