From 9c30fbc82ba030fcece34e32415bb91ce003cdd0 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Mon, 30 Nov 2026 17:51:59 +0100 Subject: [PATCH] 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. --- CLAUDE.md | 2 +- README.md | 42 +++++ extension.neon | 34 ++++ rules.neon | 83 ++++++--- tests/Rules/PHPUnit/RuleTogglesTest.php | 216 ++++++++++++++++++++++++ 5 files changed, 350 insertions(+), 27 deletions(-) create mode 100644 tests/Rules/PHPUnit/RuleTogglesTest.php diff --git a/CLAUDE.md b/CLAUDE.md index cd7088fa..3fc82018 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -72,7 +72,7 @@ stubs/ # PHPUnit stub files for type definitions ## Configuration Files - **`extension.neon`** — Main extension configuration registered via phpstan/extension-installer. Defines parameters, services (type extensions, helpers), and stub files. -- **`rules.neon`** — Strict PHPUnit-specific rules. Loaded separately; users opt in by including this file. +- **`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.` 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. - **`phpstan.neon`** — Self-analysis configuration (level 8, with strict rules and deprecation rules). - **`phpstan-baseline.neon`** — Baseline for known PHPStan errors in the project itself. - **`phpunit.xml`** — PHPUnit configuration for running the test suite. diff --git a/README.md b/README.md index e0813e44..aad624a2 100644 --- a/README.md +++ b/README.md @@ -105,3 +105,45 @@ To perform framework-specific checks, include also this file: ``` + +## Enabling rules one-by-one + +All rules from `rules.neon` are enabled by default. Each of them can be disabled individually: + +```neon +parameters: + phpunit: + assertSameWithCount: false +``` + +To enable only the rules you want, disable all of them and then turn the ones you want back on: + +```neon +parameters: + phpunit: + allRules: false + classCoversExists: true +``` + +Rules added in future releases are covered by `allRules` as well, so with `allRules: false` no new rule is enabled until you opt in. + +| Parameter | Rule | Enabled by default | +|---|---|---| +| `assertSameBooleanExpected` | `AssertSameBooleanExpectedRule` | yes | +| `assertSameNullExpected` | `AssertSameNullExpectedRule` | yes | +| `assertSameWithCount` | `AssertSameWithCountRule` | yes | +| `classCoversExists` | `ClassCoversExistsRule` | yes | +| `classMethodCoversExists` | `ClassMethodCoversExistsRule` | yes | +| `mockMethodCall` | `MockMethodCallRule` | yes | +| `noMissingSpaceInClassAnnotation` | `NoMissingSpaceInClassAnnotationRule` | yes | +| `noMissingSpaceInMethodAnnotation` | `NoMissingSpaceInMethodAnnotationRule` | yes | +| `shouldCallParentMethods` | `ShouldCallParentMethodsRule` | yes | +| `dataProviderDeclaration` | `DataProviderDeclarationRule` | yes | +| `attributeRequiresPhpVersion` | `AttributeRequiresPhpVersionRule` | yes | +| `dataProviderData` | `DataProviderDataRule` | with [Bleeding Edge](https://phpstan.org/blog/what-is-bleeding-edge) | +| `classAttributeRequiresPhpVersion` | `ClassAttributeRequiresPhpVersionRule` | with Bleeding Edge | +| `emptyDataProvider` | `EmptyDataProviderRule` | with Bleeding Edge | +| `assertEqualsIsDiscouraged` | `AssertEqualsIsDiscouragedRule` | with Bleeding Edge and [phpstan-strict-rules](https://github.com/phpstan/phpstan-strict-rules) | +| `assertEmptyIsDiscouraged` | `AssertEmptyIsDiscouragedRule` | with Bleeding Edge and phpstan-strict-rules | + +Setting a parameter to `true` enables the rule regardless of Bleeding Edge and phpstan-strict-rules. diff --git a/extension.neon b/extension.neon index ffa03655..bbe9d891 100644 --- a/extension.neon +++ b/extension.neon @@ -2,6 +2,23 @@ parameters: phpunit: convertUnionToIntersectionType: true reportMissingDataProviderReturnType: false + allRules: true + assertSameBooleanExpected: %phpunit.allRules% + assertSameNullExpected: %phpunit.allRules% + assertSameWithCount: %phpunit.allRules% + classCoversExists: %phpunit.allRules% + classMethodCoversExists: %phpunit.allRules% + mockMethodCall: %phpunit.allRules% + noMissingSpaceInClassAnnotation: %phpunit.allRules% + noMissingSpaceInMethodAnnotation: %phpunit.allRules% + shouldCallParentMethods: %phpunit.allRules% + dataProviderDeclaration: %phpunit.allRules% + attributeRequiresPhpVersion: %phpunit.allRules% + assertEqualsIsDiscouraged: [%phpunit.allRules%, %strictRulesInstalled%, %featureToggles.bleedingEdge%] + assertEmptyIsDiscouraged: [%phpunit.allRules%, %strictRulesInstalled%, %featureToggles.bleedingEdge%] + dataProviderData: [%phpunit.allRules%, %featureToggles.bleedingEdge%] + classAttributeRequiresPhpVersion: [%phpunit.allRules%, %featureToggles.bleedingEdge%] + emptyDataProvider: [%phpunit.allRules%, %featureToggles.bleedingEdge%] additionalConstructors: - PHPUnit\Framework\TestCase::setUp earlyTerminatingMethodCalls: @@ -26,6 +43,23 @@ parametersSchema: phpunit: structure([ convertUnionToIntersectionType: bool(), reportMissingDataProviderReturnType: bool(), + allRules: bool(), + assertSameBooleanExpected: anyOf(bool(), arrayOf(bool())), + assertSameNullExpected: anyOf(bool(), arrayOf(bool())), + assertSameWithCount: anyOf(bool(), arrayOf(bool())), + classCoversExists: anyOf(bool(), arrayOf(bool())), + classMethodCoversExists: anyOf(bool(), arrayOf(bool())), + mockMethodCall: anyOf(bool(), arrayOf(bool())), + noMissingSpaceInClassAnnotation: anyOf(bool(), arrayOf(bool())), + noMissingSpaceInMethodAnnotation: anyOf(bool(), arrayOf(bool())), + shouldCallParentMethods: anyOf(bool(), arrayOf(bool())), + dataProviderDeclaration: anyOf(bool(), arrayOf(bool())), + attributeRequiresPhpVersion: anyOf(bool(), arrayOf(bool())), + assertEqualsIsDiscouraged: anyOf(bool(), arrayOf(bool())), + assertEmptyIsDiscouraged: anyOf(bool(), arrayOf(bool())), + dataProviderData: anyOf(bool(), arrayOf(bool())), + classAttributeRequiresPhpVersion: anyOf(bool(), arrayOf(bool())), + emptyDataProvider: anyOf(bool(), arrayOf(bool())), ]) services: diff --git a/rules.neon b/rules.neon index ce535a9b..07ca0ecb 100644 --- a/rules.neon +++ b/rules.neon @@ -1,45 +1,73 @@ -rules: - - PHPStan\Rules\PHPUnit\AssertSameBooleanExpectedRule - - PHPStan\Rules\PHPUnit\AssertSameNullExpectedRule - - PHPStan\Rules\PHPUnit\AssertSameWithCountRule - - PHPStan\Rules\PHPUnit\ClassCoversExistsRule - - PHPStan\Rules\PHPUnit\ClassMethodCoversExistsRule - - PHPStan\Rules\PHPUnit\MockMethodCallRule - - PHPStan\Rules\PHPUnit\NoMissingSpaceInClassAnnotationRule - - PHPStan\Rules\PHPUnit\NoMissingSpaceInMethodAnnotationRule - - PHPStan\Rules\PHPUnit\ShouldCallParentMethodsRule - conditionalTags: + PHPStan\Rules\PHPUnit\AssertSameBooleanExpectedRule: + phpstan.rules.rule: %phpunit.assertSameBooleanExpected% + PHPStan\Rules\PHPUnit\AssertSameNullExpectedRule: + phpstan.rules.rule: %phpunit.assertSameNullExpected% + PHPStan\Rules\PHPUnit\AssertSameWithCountRule: + phpstan.rules.rule: %phpunit.assertSameWithCount% + PHPStan\Rules\PHPUnit\ClassCoversExistsRule: + phpstan.rules.rule: %phpunit.classCoversExists% + PHPStan\Rules\PHPUnit\ClassMethodCoversExistsRule: + phpstan.rules.rule: %phpunit.classMethodCoversExists% + PHPStan\Rules\PHPUnit\MockMethodCallRule: + phpstan.rules.rule: %phpunit.mockMethodCall% + PHPStan\Rules\PHPUnit\NoMissingSpaceInClassAnnotationRule: + phpstan.rules.rule: %phpunit.noMissingSpaceInClassAnnotation% + PHPStan\Rules\PHPUnit\NoMissingSpaceInMethodAnnotationRule: + phpstan.rules.rule: %phpunit.noMissingSpaceInMethodAnnotation% + PHPStan\Rules\PHPUnit\ShouldCallParentMethodsRule: + phpstan.rules.rule: %phpunit.shouldCallParentMethods% + PHPStan\Rules\PHPUnit\DataProviderDeclarationRule: + phpstan.rules.rule: %phpunit.dataProviderDeclaration% + PHPStan\Rules\PHPUnit\AttributeRequiresPhpVersionRule: + phpstan.rules.rule: %phpunit.attributeRequiresPhpVersion% PHPStan\Rules\PHPUnit\AssertEqualsIsDiscouragedRule: - phpstan.rules.rule: [%strictRulesInstalled%, %featureToggles.bleedingEdge%] + phpstan.rules.rule: %phpunit.assertEqualsIsDiscouraged% PHPStan\Rules\PHPUnit\AssertEmptyIsDiscouragedRule: - phpstan.rules.rule: [%strictRulesInstalled%, %featureToggles.bleedingEdge%] - + phpstan.rules.rule: %phpunit.assertEmptyIsDiscouraged% PHPStan\Rules\PHPUnit\DataProviderDataRule: - phpstan.rules.rule: %featureToggles.bleedingEdge% - + phpstan.rules.rule: %phpunit.dataProviderData% PHPStan\Rules\PHPUnit\ClassAttributeRequiresPhpVersionRule: - phpstan.rules.rule: %featureToggles.bleedingEdge% - + phpstan.rules.rule: %phpunit.classAttributeRequiresPhpVersion% PHPStan\Rules\PHPUnit\EmptyDataProviderRule: - phpstan.rules.rule: %featureToggles.bleedingEdge% + phpstan.rules.rule: %phpunit.emptyDataProvider% services: + - + class: PHPStan\Rules\PHPUnit\AssertSameBooleanExpectedRule + + - + class: PHPStan\Rules\PHPUnit\AssertSameNullExpectedRule + + - + class: PHPStan\Rules\PHPUnit\AssertSameWithCountRule + + - + class: PHPStan\Rules\PHPUnit\ClassCoversExistsRule + + - + class: PHPStan\Rules\PHPUnit\ClassMethodCoversExistsRule + + - + class: PHPStan\Rules\PHPUnit\MockMethodCallRule + + - + class: PHPStan\Rules\PHPUnit\NoMissingSpaceInClassAnnotationRule + + - + class: PHPStan\Rules\PHPUnit\NoMissingSpaceInMethodAnnotationRule + + - + class: PHPStan\Rules\PHPUnit\ShouldCallParentMethodsRule + - class: PHPStan\Rules\PHPUnit\DataProviderDeclarationRule arguments: checkFunctionNameCase: %checkFunctionNameCase% deprecationRulesInstalled: %deprecationRulesInstalled% - tags: - - phpstan.rules.rule - class: PHPStan\Rules\PHPUnit\AttributeRequiresPhpVersionRule - tags: - - phpstan.rules.rule - - - - class: PHPStan\Rules\PHPUnit\ClassAttributeRequiresPhpVersionRule - class: PHPStan\Rules\PHPUnit\AssertEqualsIsDiscouragedRule @@ -50,5 +78,8 @@ services: - class: PHPStan\Rules\PHPUnit\DataProviderDataRule + - + class: PHPStan\Rules\PHPUnit\ClassAttributeRequiresPhpVersionRule + - class: PHPStan\Rules\PHPUnit\EmptyDataProviderRule diff --git a/tests/Rules/PHPUnit/RuleTogglesTest.php b/tests/Rules/PHPUnit/RuleTogglesTest.php new file mode 100644 index 00000000..12cd4725 --- /dev/null +++ b/tests/Rules/PHPUnit/RuleTogglesTest.php @@ -0,0 +1,216 @@ + false], true, true), + ); + } + + /** + * @dataProvider provideRules + * @param class-string $rule + */ + #[DataProvider('provideRules')] + public function testRuleCanBeEnabledIndividually(string $parameter, string $rule): void + { + self::assertSame( + [$rule], + self::enabledRules(['allRules' => false, $parameter => true], false, false), + ); + } + + public function testRuleCanBeDisabledIndividually(): void + { + self::assertSame( + self::sorted(array_diff(self::allRules(), [ClassCoversExistsRule::class])), + self::enabledRules(['classCoversExists' => false], true, true), + ); + } + + /** + * @return iterable}> + */ + public static function provideRules(): iterable + { + foreach (self::allRules() as $rule) { + $parameter = lcfirst(substr($rule, strlen(self::RULES_NAMESPACE), -strlen('Rule'))); + + yield $parameter => [$parameter, $rule]; + } + } + + /** + * @return string[] + */ + public static function getAdditionalConfigFiles(): array + { + $configFiles = [ + __DIR__ . '/../../../extension.neon', + __DIR__ . '/../../../rules.neon', + ]; + + if (self::$configFile !== null) { + $configFiles[] = self::$configFile; + } + + return $configFiles; + } + + /** + * Every rule this extension ships, discovered from the source directory so + * that a rule added without a toggle of its own fails the tests above. + * + * @return list> + */ + private static function allRules(): array + { + $files = glob(__DIR__ . '/../../../src/Rules/PHPUnit/*Rule.php'); + self::assertNotFalse($files); + + $rules = []; + foreach ($files as $file) { + /** @var class-string $rule */ + $rule = self::RULES_NAMESPACE . basename($file, '.php'); + $rules[] = $rule; + } + + return self::sorted($rules); + } + + /** + * @param array $phpunitParameters + * @return list> + */ + private static function enabledRules(array $phpunitParameters, bool $bleedingEdge, bool $strictRulesInstalled): array + { + $config = "parameters:\n"; + $config .= sprintf("\tstrictRulesInstalled: %s\n", self::bool($strictRulesInstalled)); + $config .= sprintf("\tfeatureToggles:\n\t\tbleedingEdge: %s\n", self::bool($bleedingEdge)); + + if ($phpunitParameters !== []) { + $config .= "\tphpunit:\n"; + foreach ($phpunitParameters as $name => $value) { + $config .= sprintf("\t\t%s: %s\n", $name, self::bool($value)); + } + } + + $directory = __DIR__ . '/../../tmp'; + if (!is_dir($directory)) { + mkdir($directory); + } + + self::$configFile = sprintf('%s/rule-toggles-%s.neon', $directory, sha1($config)); + file_put_contents(self::$configFile, $config); + + $rules = []; + foreach (self::getContainer()->getServicesByTag('phpstan.rules.rule') as $rule) { + if (!$rule instanceof Rule) { + continue; + } + + $class = get_class($rule); + if (strpos($class, self::RULES_NAMESPACE) !== 0) { + continue; + } + + $rules[] = $class; + } + + return self::sorted($rules); + } + + private static function bool(bool $value): string + { + return $value ? 'true' : 'false'; + } + + /** + * @param array> $rules + * @return list> + */ + private static function sorted(array $rules): array + { + $rules = array_values($rules); + sort($rules); + + return $rules; + } + +}