diff --git a/doc/rule-set-factory.md b/doc/rule-set-factory.md index c6ac922..c706ef1 100644 --- a/doc/rule-set-factory.md +++ b/doc/rule-set-factory.md @@ -13,6 +13,7 @@ return PhpCsFixer\Config::create() ->php(5.6, true) // Activate php 5.6 risky rules ->pedrotroller(true) // Activate my own ruleset (with risky rules) ->enable('ordered_imports') // Add an other rule + ->disable('yoda_style') // Disable a rule ->getRules() ) ->registerCustomFixers(new Fixers()) @@ -26,29 +27,33 @@ return PhpCsFixer\Config::create() ### `->psr0()` -Activate the `@psr0` ruleset. +Activate the `@psr0` rule. ### `->psr1()` -Activate the `@psr0` ruleset. +Activate the `@psr0` rule. ### `->psr2()` -Activate the `@psr0` ruleset. +Activate the `@psr0` rule. ### `->psr4()` -Activate the `@psr0` ruleset. +Activate the `@psr0` rule. ### `->symfony([bool $risky = false])` -Activate the `@Symfony` or `@Symfony:risky` depending of the `$risky` argument. +Activate the `@Symfony` rule or `@Symfony:risky` rule depending of the `$risky` argument. + +### `->doctrineAnnotation()` + +Activate the `@DoctrineAnnotation` rule. ### `->php(float $version, [bool $risky = false])` -Activate fixers related to a PHP version including risky of not depending of the `$risky` argument. +Activate fixers and rules related to a PHP version including risky of not depending of the `$risky` argument. -Example: +Example: ```php RuleSetFactory::create() @@ -68,16 +73,16 @@ Example: Activate all rules of this library including risky of not depending of the `$risky` argument. -### `->enable(string $name, [$config = true])` +### `->enable(string $name, array $config = null)` Enable a rule. -Example: +Example: ```php RuleSetFactory::create() ->enable('ordered_class_elements') - ->enable('ordered_imports', true) // Same as "->enable('ordered_imports')" + ->enable('ordered_imports') ->enable('phpdoc_add_missing_param_annotation', ['only_untyped' => true]) ->getRules() ; @@ -87,11 +92,11 @@ Example: Disable a rule. -Example: +Example: ```php RuleSetFactory::create() - ->disable('ordered_class_elements') // Same as "->enable('ordered_class_elements', false)" + ->disable('ordered_class_elements') ->getRules() ; ``` diff --git a/spec/PedroTroller/CS/Fixer/RuleSetFactorySpec.php b/spec/PedroTroller/CS/Fixer/RuleSetFactorySpec.php index e2d3144..32ee748 100644 --- a/spec/PedroTroller/CS/Fixer/RuleSetFactorySpec.php +++ b/spec/PedroTroller/CS/Fixer/RuleSetFactorySpec.php @@ -38,6 +38,11 @@ function it_adds_a_symfony_set() $this->symfony()->getRules()->shouldReturn(['@Symfony' => true]); } + function it_adds_a_doctrine_annotation_set() + { + $this->doctrineAnnotation()->getRules()->shouldReturn(['@DoctrineAnnotation' => true]); + } + function it_adds_a_symfony_strict_set() { $this->symfony()->getRules()->shouldReturn([ @@ -59,6 +64,7 @@ function it_adds_a_php_version_support() ]); $this->php(5.6, true)->getRules()->shouldReturn([ + '@PHP56Migration' => true, '@PHP56Migration:risky' => true, 'array_syntax' => ['syntax' => 'short'], 'list_syntax' => ['syntax' => 'long'], @@ -72,7 +78,9 @@ function it_adds_a_php_version_support() ]); $this->php(7.0, true)->getRules()->shouldReturn([ + '@PHP56Migration' => true, '@PHP56Migration:risky' => true, + '@PHP70Migration' => true, '@PHP70Migration:risky' => true, 'array_syntax' => ['syntax' => 'short'], 'list_syntax' => ['syntax' => 'long'], @@ -87,8 +95,11 @@ function it_adds_a_php_version_support() ]); $this->php(7.1, true)->getRules()->shouldReturn([ + '@PHP56Migration' => true, '@PHP56Migration:risky' => true, + '@PHP70Migration' => true, '@PHP70Migration:risky' => true, + '@PHP71Migration' => true, '@PHP71Migration:risky' => true, 'array_syntax' => ['syntax' => 'short'], 'list_syntax' => ['syntax' => 'short'], @@ -103,8 +114,11 @@ function it_adds_a_php_version_support() ]); $this->php(7.2, true)->getRules()->shouldReturn([ + '@PHP56Migration' => true, '@PHP56Migration:risky' => true, + '@PHP70Migration' => true, '@PHP70Migration:risky' => true, + '@PHP71Migration' => true, '@PHP71Migration:risky' => true, 'array_syntax' => ['syntax' => 'short'], 'list_syntax' => ['syntax' => 'short'], @@ -131,7 +145,7 @@ function it_adds_my_own_fixer_set() function it_enables_a_rule() { $this - ->enable('no_useless_else', true) + ->enable('no_useless_else') ->enable('ordered_imports') ->enable('phpdoc_add_missing_param_annotation', ['only_untyped' => true]) ->getRules() @@ -146,7 +160,7 @@ function it_enables_a_rule() function it_disables_a_rule() { $this - ->enable('no_useless_else', true) + ->enable('no_useless_else') ->enable('ordered_imports') ->enable('phpdoc_add_missing_param_annotation', ['only_untyped' => true]) ->disable('phpdoc_add_missing_param_annotation') diff --git a/src/PedroTroller/CS/Fixer/RuleSetFactory.php b/src/PedroTroller/CS/Fixer/RuleSetFactory.php index 8f41a39..6380928 100644 --- a/src/PedroTroller/CS/Fixer/RuleSetFactory.php +++ b/src/PedroTroller/CS/Fixer/RuleSetFactory.php @@ -99,6 +99,17 @@ public function symfony($risky = false) )); } + /** + * @return RuleSetFactory + */ + public function doctrineAnnotation() + { + return self::create(array_merge( + $this->rules, + ['@DoctrineAnnotation' => true] + )); + } + /** * @param float $version * @param bool $risky @@ -112,13 +123,26 @@ public function php($version, $risky = false) switch (true) { case $version >= 7.1: $config = array_merge(['list_syntax' => ['syntax' => 'short']], $config); - $config = array_merge([($risky ? '@PHP71Migration:risky' : '@PHP71Migration') => true], $config); + + if ($risky) { + $config = array_merge(['@PHP71Migration:risky' => true], $config); + } + + $config = array_merge(['@PHP71Migration' => true], $config); // no break case $version >= 7.0: - $config = array_merge([($risky ? '@PHP70Migration:risky' : '@PHP70Migration') => true], $config); + if ($risky) { + $config = array_merge(['@PHP70Migration:risky' => true], $config); + } + + $config = array_merge(['@PHP70Migration' => true], $config); // no break case $version >= 5.6: - $config = array_merge([($risky ? '@PHP56Migration:risky' : '@PHP56Migration') => true], $config); + if ($risky) { + $config = array_merge(['@PHP56Migration:risky' => true], $config); + } + + $config = array_merge(['@PHP56Migration' => true], $config); // no break case $version >= 5.4: $config = array_merge(['array_syntax' => ['syntax' => 'short']], $config); @@ -159,16 +183,15 @@ public function pedrotroller($risky = false) } /** - * @param string $name - * @param array|bool $config + * @param string $name * * @return RuleSetFactory */ - public function enable($name, $config = true) + public function enable($name, array $config = null) { return self::create(array_merge( $this->rules, - [$name => $config] + [$name => is_array($config) ? $config : true] )); } diff --git a/src/PedroTroller/CS/Fixer/TokensAnalyzer.php b/src/PedroTroller/CS/Fixer/TokensAnalyzer.php index 2200fff..f865cd3 100644 --- a/src/PedroTroller/CS/Fixer/TokensAnalyzer.php +++ b/src/PedroTroller/CS/Fixer/TokensAnalyzer.php @@ -62,7 +62,6 @@ public function getMethodArguments($index) ++$argumentName; } while (!preg_match('/^\$.+/', $this->tokens[$argumentName]->getContent())); - } $next = $this->tokens->getNextMeaningfulToken($argumentName); @@ -240,7 +239,6 @@ public function getReturnedType($index) : $return; } } while (false === $this->tokens[$index]->equals(['{', ';'])); - } /**