Skip to content

Commit 80d6e8e

Browse files
sebastianbergmannondrejmirtes
authored andcommitted
Allow rules to be enabled and disabled individually
Add `phpunit.allRules` and one parameter per rule, following the convention established by phpstan/phpstan-strict-rules. All rules in rules.neon are now services tagged through conditionalTags, so `allRules: false` disables every rule and single rules can be turned back on by name. Default behaviour is unchanged: the Bleeding Edge and strictRulesInstalled conditions moved into the parameter defaults.
1 parent 0f627f8 commit 80d6e8e

5 files changed

Lines changed: 350 additions & 27 deletions

File tree

‎CLAUDE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ stubs/ # PHPUnit stub files for type definitions
7272
## Configuration Files
7373

7474
- **`extension.neon`** — Main extension configuration registered via phpstan/extension-installer. Defines parameters, services (type extensions, helpers), and stub files.
75-
- **`rules.neon`** — Strict PHPUnit-specific rules. Loaded separately; users opt in by including this file.
75+
- **`rules.neon`** — Strict PHPUnit-specific rules. Loaded separately; users opt in by including this file. Every rule is registered as a service and tagged through `conditionalTags` with its own `phpunit.<ruleName>` parameter (defined in `extension.neon`, defaulting to `%phpunit.allRules%`). A new rule needs a service, a conditional tag, a parameter, a schema entry and a README table row; `RuleTogglesTest` fails if the toggle is missing.
7676
- **`phpstan.neon`** — Self-analysis configuration (level 8, with strict rules and deprecation rules).
7777
- **`phpstan-baseline.neon`** — Baseline for known PHPStan errors in the project itself.
7878
- **`phpunit.xml`** — PHPUnit configuration for running the test suite.

‎README.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,45 @@ To perform framework-specific checks, include also this file:
105105
```
106106

107107
</details>
108+
109+
## Enabling rules one-by-one
110+
111+
All rules from `rules.neon` are enabled by default. Each of them can be disabled individually:
112+
113+
```neon
114+
parameters:
115+
phpunit:
116+
assertSameWithCount: false
117+
```
118+
119+
To enable only the rules you want, disable all of them and then turn the ones you want back on:
120+
121+
```neon
122+
parameters:
123+
phpunit:
124+
allRules: false
125+
classCoversExists: true
126+
```
127+
128+
Rules added in future releases are covered by `allRules` as well, so with `allRules: false` no new rule is enabled until you opt in.
129+
130+
| Parameter | Rule | Enabled by default |
131+
|---|---|---|
132+
| `assertSameBooleanExpected` | `AssertSameBooleanExpectedRule` | yes |
133+
| `assertSameNullExpected` | `AssertSameNullExpectedRule` | yes |
134+
| `assertSameWithCount` | `AssertSameWithCountRule` | yes |
135+
| `classCoversExists` | `ClassCoversExistsRule` | yes |
136+
| `classMethodCoversExists` | `ClassMethodCoversExistsRule` | yes |
137+
| `mockMethodCall` | `MockMethodCallRule` | yes |
138+
| `noMissingSpaceInClassAnnotation` | `NoMissingSpaceInClassAnnotationRule` | yes |
139+
| `noMissingSpaceInMethodAnnotation` | `NoMissingSpaceInMethodAnnotationRule` | yes |
140+
| `shouldCallParentMethods` | `ShouldCallParentMethodsRule` | yes |
141+
| `dataProviderDeclaration` | `DataProviderDeclarationRule` | yes |
142+
| `attributeRequiresPhpVersion` | `AttributeRequiresPhpVersionRule` | yes |
143+
| `dataProviderData` | `DataProviderDataRule` | with [Bleeding Edge](https://phpstan.org/blog/what-is-bleeding-edge) |
144+
| `classAttributeRequiresPhpVersion` | `ClassAttributeRequiresPhpVersionRule` | with Bleeding Edge |
145+
| `emptyDataProvider` | `EmptyDataProviderRule` | with Bleeding Edge |
146+
| `assertEqualsIsDiscouraged` | `AssertEqualsIsDiscouragedRule` | with Bleeding Edge and [phpstan-strict-rules](https://github.com/phpstan/phpstan-strict-rules) |
147+
| `assertEmptyIsDiscouraged` | `AssertEmptyIsDiscouragedRule` | with Bleeding Edge and phpstan-strict-rules |
148+
149+
Setting a parameter to `true` enables the rule regardless of Bleeding Edge and phpstan-strict-rules.

‎extension.neon‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,23 @@ parameters:
22
phpunit:
33
convertUnionToIntersectionType: true
44
reportMissingDataProviderReturnType: false
5+
allRules: true
6+
assertSameBooleanExpected: %phpunit.allRules%
7+
assertSameNullExpected: %phpunit.allRules%
8+
assertSameWithCount: %phpunit.allRules%
9+
classCoversExists: %phpunit.allRules%
10+
classMethodCoversExists: %phpunit.allRules%
11+
mockMethodCall: %phpunit.allRules%
12+
noMissingSpaceInClassAnnotation: %phpunit.allRules%
13+
noMissingSpaceInMethodAnnotation: %phpunit.allRules%
14+
shouldCallParentMethods: %phpunit.allRules%
15+
dataProviderDeclaration: %phpunit.allRules%
16+
attributeRequiresPhpVersion: %phpunit.allRules%
17+
assertEqualsIsDiscouraged: [%phpunit.allRules%, %strictRulesInstalled%, %featureToggles.bleedingEdge%]
18+
assertEmptyIsDiscouraged: [%phpunit.allRules%, %strictRulesInstalled%, %featureToggles.bleedingEdge%]
19+
dataProviderData: [%phpunit.allRules%, %featureToggles.bleedingEdge%]
20+
classAttributeRequiresPhpVersion: [%phpunit.allRules%, %featureToggles.bleedingEdge%]
21+
emptyDataProvider: [%phpunit.allRules%, %featureToggles.bleedingEdge%]
522
additionalConstructors:
623
- PHPUnit\Framework\TestCase::setUp
724
earlyTerminatingMethodCalls:
@@ -26,6 +43,23 @@ parametersSchema:
2643
phpunit: structure([
2744
convertUnionToIntersectionType: bool(),
2845
reportMissingDataProviderReturnType: bool(),
46+
allRules: bool(),
47+
assertSameBooleanExpected: anyOf(bool(), arrayOf(bool())),
48+
assertSameNullExpected: anyOf(bool(), arrayOf(bool())),
49+
assertSameWithCount: anyOf(bool(), arrayOf(bool())),
50+
classCoversExists: anyOf(bool(), arrayOf(bool())),
51+
classMethodCoversExists: anyOf(bool(), arrayOf(bool())),
52+
mockMethodCall: anyOf(bool(), arrayOf(bool())),
53+
noMissingSpaceInClassAnnotation: anyOf(bool(), arrayOf(bool())),
54+
noMissingSpaceInMethodAnnotation: anyOf(bool(), arrayOf(bool())),
55+
shouldCallParentMethods: anyOf(bool(), arrayOf(bool())),
56+
dataProviderDeclaration: anyOf(bool(), arrayOf(bool())),
57+
attributeRequiresPhpVersion: anyOf(bool(), arrayOf(bool())),
58+
assertEqualsIsDiscouraged: anyOf(bool(), arrayOf(bool())),
59+
assertEmptyIsDiscouraged: anyOf(bool(), arrayOf(bool())),
60+
dataProviderData: anyOf(bool(), arrayOf(bool())),
61+
classAttributeRequiresPhpVersion: anyOf(bool(), arrayOf(bool())),
62+
emptyDataProvider: anyOf(bool(), arrayOf(bool())),
2963
])
3064

3165
services:

‎rules.neon‎

Lines changed: 57 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,73 @@
1-
rules:
2-
- PHPStan\Rules\PHPUnit\AssertSameBooleanExpectedRule
3-
- PHPStan\Rules\PHPUnit\AssertSameNullExpectedRule
4-
- PHPStan\Rules\PHPUnit\AssertSameWithCountRule
5-
- PHPStan\Rules\PHPUnit\ClassCoversExistsRule
6-
- PHPStan\Rules\PHPUnit\ClassMethodCoversExistsRule
7-
- PHPStan\Rules\PHPUnit\MockMethodCallRule
8-
- PHPStan\Rules\PHPUnit\NoMissingSpaceInClassAnnotationRule
9-
- PHPStan\Rules\PHPUnit\NoMissingSpaceInMethodAnnotationRule
10-
- PHPStan\Rules\PHPUnit\ShouldCallParentMethodsRule
11-
121
conditionalTags:
2+
PHPStan\Rules\PHPUnit\AssertSameBooleanExpectedRule:
3+
phpstan.rules.rule: %phpunit.assertSameBooleanExpected%
4+
PHPStan\Rules\PHPUnit\AssertSameNullExpectedRule:
5+
phpstan.rules.rule: %phpunit.assertSameNullExpected%
6+
PHPStan\Rules\PHPUnit\AssertSameWithCountRule:
7+
phpstan.rules.rule: %phpunit.assertSameWithCount%
8+
PHPStan\Rules\PHPUnit\ClassCoversExistsRule:
9+
phpstan.rules.rule: %phpunit.classCoversExists%
10+
PHPStan\Rules\PHPUnit\ClassMethodCoversExistsRule:
11+
phpstan.rules.rule: %phpunit.classMethodCoversExists%
12+
PHPStan\Rules\PHPUnit\MockMethodCallRule:
13+
phpstan.rules.rule: %phpunit.mockMethodCall%
14+
PHPStan\Rules\PHPUnit\NoMissingSpaceInClassAnnotationRule:
15+
phpstan.rules.rule: %phpunit.noMissingSpaceInClassAnnotation%
16+
PHPStan\Rules\PHPUnit\NoMissingSpaceInMethodAnnotationRule:
17+
phpstan.rules.rule: %phpunit.noMissingSpaceInMethodAnnotation%
18+
PHPStan\Rules\PHPUnit\ShouldCallParentMethodsRule:
19+
phpstan.rules.rule: %phpunit.shouldCallParentMethods%
20+
PHPStan\Rules\PHPUnit\DataProviderDeclarationRule:
21+
phpstan.rules.rule: %phpunit.dataProviderDeclaration%
22+
PHPStan\Rules\PHPUnit\AttributeRequiresPhpVersionRule:
23+
phpstan.rules.rule: %phpunit.attributeRequiresPhpVersion%
1324
PHPStan\Rules\PHPUnit\AssertEqualsIsDiscouragedRule:
14-
phpstan.rules.rule: [%strictRulesInstalled%, %featureToggles.bleedingEdge%]
25+
phpstan.rules.rule: %phpunit.assertEqualsIsDiscouraged%
1526
PHPStan\Rules\PHPUnit\AssertEmptyIsDiscouragedRule:
16-
phpstan.rules.rule: [%strictRulesInstalled%, %featureToggles.bleedingEdge%]
17-
27+
phpstan.rules.rule: %phpunit.assertEmptyIsDiscouraged%
1828
PHPStan\Rules\PHPUnit\DataProviderDataRule:
19-
phpstan.rules.rule: %featureToggles.bleedingEdge%
20-
29+
phpstan.rules.rule: %phpunit.dataProviderData%
2130
PHPStan\Rules\PHPUnit\ClassAttributeRequiresPhpVersionRule:
22-
phpstan.rules.rule: %featureToggles.bleedingEdge%
23-
31+
phpstan.rules.rule: %phpunit.classAttributeRequiresPhpVersion%
2432
PHPStan\Rules\PHPUnit\EmptyDataProviderRule:
25-
phpstan.rules.rule: %featureToggles.bleedingEdge%
33+
phpstan.rules.rule: %phpunit.emptyDataProvider%
2634

2735
services:
36+
-
37+
class: PHPStan\Rules\PHPUnit\AssertSameBooleanExpectedRule
38+
39+
-
40+
class: PHPStan\Rules\PHPUnit\AssertSameNullExpectedRule
41+
42+
-
43+
class: PHPStan\Rules\PHPUnit\AssertSameWithCountRule
44+
45+
-
46+
class: PHPStan\Rules\PHPUnit\ClassCoversExistsRule
47+
48+
-
49+
class: PHPStan\Rules\PHPUnit\ClassMethodCoversExistsRule
50+
51+
-
52+
class: PHPStan\Rules\PHPUnit\MockMethodCallRule
53+
54+
-
55+
class: PHPStan\Rules\PHPUnit\NoMissingSpaceInClassAnnotationRule
56+
57+
-
58+
class: PHPStan\Rules\PHPUnit\NoMissingSpaceInMethodAnnotationRule
59+
60+
-
61+
class: PHPStan\Rules\PHPUnit\ShouldCallParentMethodsRule
62+
2863
-
2964
class: PHPStan\Rules\PHPUnit\DataProviderDeclarationRule
3065
arguments:
3166
checkFunctionNameCase: %checkFunctionNameCase%
3267
deprecationRulesInstalled: %deprecationRulesInstalled%
33-
tags:
34-
- phpstan.rules.rule
3568

3669
-
3770
class: PHPStan\Rules\PHPUnit\AttributeRequiresPhpVersionRule
38-
tags:
39-
- phpstan.rules.rule
40-
41-
-
42-
class: PHPStan\Rules\PHPUnit\ClassAttributeRequiresPhpVersionRule
4371

4472
-
4573
class: PHPStan\Rules\PHPUnit\AssertEqualsIsDiscouragedRule
@@ -50,5 +78,8 @@ services:
5078
-
5179
class: PHPStan\Rules\PHPUnit\DataProviderDataRule
5280

81+
-
82+
class: PHPStan\Rules\PHPUnit\ClassAttributeRequiresPhpVersionRule
83+
5384
-
5485
class: PHPStan\Rules\PHPUnit\EmptyDataProviderRule

0 commit comments

Comments
 (0)