Skip to content

Commit 58f5ee6

Browse files
authored
Fix php risky ruleset building (#40)
* Fix php risky ruleset building * Refactor the RuleSetFactory
1 parent ba7194f commit 58f5ee6

File tree

4 files changed

+63
-23
lines changed

4 files changed

+63
-23
lines changed

doc/rule-set-factory.md

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ return PhpCsFixer\Config::create()
1313
->php(5.6, true) // Activate php 5.6 risky rules
1414
->pedrotroller(true) // Activate my own ruleset (with risky rules)
1515
->enable('ordered_imports') // Add an other rule
16+
->disable('yoda_style') // Disable a rule
1617
->getRules()
1718
)
1819
->registerCustomFixers(new Fixers())
@@ -26,29 +27,33 @@ return PhpCsFixer\Config::create()
2627

2728
### `->psr0()`
2829

29-
Activate the `@psr0` ruleset.
30+
Activate the `@psr0` rule.
3031

3132
### `->psr1()`
3233

33-
Activate the `@psr0` ruleset.
34+
Activate the `@psr0` rule.
3435

3536
### `->psr2()`
3637

37-
Activate the `@psr0` ruleset.
38+
Activate the `@psr0` rule.
3839

3940
### `->psr4()`
4041

41-
Activate the `@psr0` ruleset.
42+
Activate the `@psr0` rule.
4243

4344
### `->symfony([bool $risky = false])`
4445

45-
Activate the `@Symfony` or `@Symfony:risky` depending of the `$risky` argument.
46+
Activate the `@Symfony` rule or `@Symfony:risky` rule depending of the `$risky` argument.
47+
48+
### `->doctrineAnnotation()`
49+
50+
Activate the `@DoctrineAnnotation` rule.
4651

4752
### `->php(float $version, [bool $risky = false])`
4853

49-
Activate fixers related to a PHP version including risky of not depending of the `$risky` argument.
54+
Activate fixers and rules related to a PHP version including risky of not depending of the `$risky` argument.
5055

51-
Example:
56+
Example:
5257

5358
```php
5459
RuleSetFactory::create()
@@ -68,16 +73,16 @@ Example:
6873

6974
Activate all rules of this library including risky of not depending of the `$risky` argument.
7075

71-
### `->enable(string $name, [$config = true])`
76+
### `->enable(string $name, array $config = null)`
7277

7378
Enable a rule.
7479

75-
Example:
80+
Example:
7681

7782
```php
7883
RuleSetFactory::create()
7984
->enable('ordered_class_elements')
80-
->enable('ordered_imports', true) // Same as "->enable('ordered_imports')"
85+
->enable('ordered_imports')
8186
->enable('phpdoc_add_missing_param_annotation', ['only_untyped' => true])
8287
->getRules()
8388
;
@@ -87,11 +92,11 @@ Example:
8792

8893
Disable a rule.
8994

90-
Example:
95+
Example:
9196

9297
```php
9398
RuleSetFactory::create()
94-
->disable('ordered_class_elements') // Same as "->enable('ordered_class_elements', false)"
99+
->disable('ordered_class_elements')
95100
->getRules()
96101
;
97102
```

spec/PedroTroller/CS/Fixer/RuleSetFactorySpec.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ function it_adds_a_symfony_set()
3838
$this->symfony()->getRules()->shouldReturn(['@Symfony' => true]);
3939
}
4040

41+
function it_adds_a_doctrine_annotation_set()
42+
{
43+
$this->doctrineAnnotation()->getRules()->shouldReturn(['@DoctrineAnnotation' => true]);
44+
}
45+
4146
function it_adds_a_symfony_strict_set()
4247
{
4348
$this->symfony()->getRules()->shouldReturn([
@@ -59,6 +64,7 @@ function it_adds_a_php_version_support()
5964
]);
6065

6166
$this->php(5.6, true)->getRules()->shouldReturn([
67+
'@PHP56Migration' => true,
6268
'@PHP56Migration:risky' => true,
6369
'array_syntax' => ['syntax' => 'short'],
6470
'list_syntax' => ['syntax' => 'long'],
@@ -72,7 +78,9 @@ function it_adds_a_php_version_support()
7278
]);
7379

7480
$this->php(7.0, true)->getRules()->shouldReturn([
81+
'@PHP56Migration' => true,
7582
'@PHP56Migration:risky' => true,
83+
'@PHP70Migration' => true,
7684
'@PHP70Migration:risky' => true,
7785
'array_syntax' => ['syntax' => 'short'],
7886
'list_syntax' => ['syntax' => 'long'],
@@ -87,8 +95,11 @@ function it_adds_a_php_version_support()
8795
]);
8896

8997
$this->php(7.1, true)->getRules()->shouldReturn([
98+
'@PHP56Migration' => true,
9099
'@PHP56Migration:risky' => true,
100+
'@PHP70Migration' => true,
91101
'@PHP70Migration:risky' => true,
102+
'@PHP71Migration' => true,
92103
'@PHP71Migration:risky' => true,
93104
'array_syntax' => ['syntax' => 'short'],
94105
'list_syntax' => ['syntax' => 'short'],
@@ -103,8 +114,11 @@ function it_adds_a_php_version_support()
103114
]);
104115

105116
$this->php(7.2, true)->getRules()->shouldReturn([
117+
'@PHP56Migration' => true,
106118
'@PHP56Migration:risky' => true,
119+
'@PHP70Migration' => true,
107120
'@PHP70Migration:risky' => true,
121+
'@PHP71Migration' => true,
108122
'@PHP71Migration:risky' => true,
109123
'array_syntax' => ['syntax' => 'short'],
110124
'list_syntax' => ['syntax' => 'short'],
@@ -131,7 +145,7 @@ function it_adds_my_own_fixer_set()
131145
function it_enables_a_rule()
132146
{
133147
$this
134-
->enable('no_useless_else', true)
148+
->enable('no_useless_else')
135149
->enable('ordered_imports')
136150
->enable('phpdoc_add_missing_param_annotation', ['only_untyped' => true])
137151
->getRules()
@@ -146,7 +160,7 @@ function it_enables_a_rule()
146160
function it_disables_a_rule()
147161
{
148162
$this
149-
->enable('no_useless_else', true)
163+
->enable('no_useless_else')
150164
->enable('ordered_imports')
151165
->enable('phpdoc_add_missing_param_annotation', ['only_untyped' => true])
152166
->disable('phpdoc_add_missing_param_annotation')

src/PedroTroller/CS/Fixer/RuleSetFactory.php

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,17 @@ public function symfony($risky = false)
9999
));
100100
}
101101

102+
/**
103+
* @return RuleSetFactory
104+
*/
105+
public function doctrineAnnotation()
106+
{
107+
return self::create(array_merge(
108+
$this->rules,
109+
['@DoctrineAnnotation' => true]
110+
));
111+
}
112+
102113
/**
103114
* @param float $version
104115
* @param bool $risky
@@ -112,13 +123,26 @@ public function php($version, $risky = false)
112123
switch (true) {
113124
case $version >= 7.1:
114125
$config = array_merge(['list_syntax' => ['syntax' => 'short']], $config);
115-
$config = array_merge([($risky ? '@PHP71Migration:risky' : '@PHP71Migration') => true], $config);
126+
127+
if ($risky) {
128+
$config = array_merge(['@PHP71Migration:risky' => true], $config);
129+
}
130+
131+
$config = array_merge(['@PHP71Migration' => true], $config);
116132
// no break
117133
case $version >= 7.0:
118-
$config = array_merge([($risky ? '@PHP70Migration:risky' : '@PHP70Migration') => true], $config);
134+
if ($risky) {
135+
$config = array_merge(['@PHP70Migration:risky' => true], $config);
136+
}
137+
138+
$config = array_merge(['@PHP70Migration' => true], $config);
119139
// no break
120140
case $version >= 5.6:
121-
$config = array_merge([($risky ? '@PHP56Migration:risky' : '@PHP56Migration') => true], $config);
141+
if ($risky) {
142+
$config = array_merge(['@PHP56Migration:risky' => true], $config);
143+
}
144+
145+
$config = array_merge(['@PHP56Migration' => true], $config);
122146
// no break
123147
case $version >= 5.4:
124148
$config = array_merge(['array_syntax' => ['syntax' => 'short']], $config);
@@ -159,16 +183,15 @@ public function pedrotroller($risky = false)
159183
}
160184

161185
/**
162-
* @param string $name
163-
* @param array|bool $config
186+
* @param string $name
164187
*
165188
* @return RuleSetFactory
166189
*/
167-
public function enable($name, $config = true)
190+
public function enable($name, array $config = null)
168191
{
169192
return self::create(array_merge(
170193
$this->rules,
171-
[$name => $config]
194+
[$name => is_array($config) ? $config : true]
172195
));
173196
}
174197

src/PedroTroller/CS/Fixer/TokensAnalyzer.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public function getMethodArguments($index)
6262

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

6867
$next = $this->tokens->getNextMeaningfulToken($argumentName);
@@ -240,7 +239,6 @@ public function getReturnedType($index)
240239
: $return;
241240
}
242241
} while (false === $this->tokens[$index]->equals(['{', ';']));
243-
244242
}
245243

246244
/**

0 commit comments

Comments
 (0)