Skip to content

Commit

Permalink
Fix php risky ruleset building (#40)
Browse files Browse the repository at this point in the history
* Fix php risky ruleset building

* Refactor the RuleSetFactory
  • Loading branch information
PedroTroller authored Aug 10, 2018
1 parent ba7194f commit 58f5ee6
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 23 deletions.
29 changes: 17 additions & 12 deletions doc/rule-set-factory.md
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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()
Expand All @@ -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()
;
Expand All @@ -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()
;
```
18 changes: 16 additions & 2 deletions spec/PedroTroller/CS/Fixer/RuleSetFactorySpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -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([
Expand All @@ -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'],
Expand All @@ -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'],
Expand All @@ -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'],
Expand All @@ -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'],
Expand All @@ -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()
Expand All @@ -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')
Expand Down
37 changes: 30 additions & 7 deletions src/PedroTroller/CS/Fixer/RuleSetFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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]
));
}

Expand Down
2 changes: 0 additions & 2 deletions src/PedroTroller/CS/Fixer/TokensAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public function getMethodArguments($index)

++$argumentName;
} while (!preg_match('/^\$.+/', $this->tokens[$argumentName]->getContent()));

}

$next = $this->tokens->getNextMeaningfulToken($argumentName);
Expand Down Expand Up @@ -240,7 +239,6 @@ public function getReturnedType($index)
: $return;
}
} while (false === $this->tokens[$index]->equals(['{', ';']));

}

/**
Expand Down

0 comments on commit 58f5ee6

Please sign in to comment.