Skip to content

Allow rules to be enabled and disabled individually - #374

Merged
ondrejmirtes merged 1 commit into
phpstan:2.1.xfrom
sebastianbergmann:rule-toggles
Dec 1, 2026
Merged

ondrejmirtes merged 1 commit into
phpstan:2.1.xfrom
sebastianbergmann:rule-toggles

Conversation

@sebastianbergmann

Copy link
Copy Markdown
Contributor

Motivation

rules.neon is currently all or nothing. Nine rules are registered unconditionally under rules:, two more are tagged directly as services, and the remaining five are only gated on Bleeding Edge (and, for two of them, on phpstan-strict-rules). A project that wants some of the rules but not all of them has to leave rules.neon out and cherry-pick rule classes by hand. That means copying the service definitions of the rules that take constructor arguments, and it silently misses every rule added in a later release.

phpstan/phpstan-strict-rules solved this with strictRules.allRules plus one parameter per rule, and ergebnis/phpstan-rules and shipmonk/phpstan-rules follow the same idea. This pull request brings that convention to phpstan-phpunit.

The discussion that led here is in sebastianbergmann/phpunit#7195 (comment): PHPUnit wants the type inference of this extension for its own test suite and wants to adopt the rules one at a time.

Usage

Disable a single rule:

parameters:
	phpunit:
		assertSameWithCount: false

Enable only selected rules:

parameters:
	phpunit:
		allRules: false
		classCoversExists: true

Changes

  • extension.neon gains phpunit.allRules (default true) and one parameter per rule, named after the rule class without the Rule suffix (ClassCoversExistsRule becomes classCoversExists). Each one defaults to %phpunit.allRules%.
  • rules.neon no longer has a rules: section. Every rule is a service and gets the phpstan.rules.rule tag through conditionalTags from its own parameter.
  • The README documents the parameters and what each rule's default depends on.
  • RuleTogglesTest covers the configuration (see below).

No PHP source in src/ changes.

Backward compatibility

Default behaviour is unchanged. The Bleeding Edge and strictRulesInstalled conditions moved from conditionalTags into the parameter defaults, the same way phpstan-strict-rules does it for booleansInLoopConditions:

dataProviderData: [%phpunit.allRules%, %featureToggles.bleedingEdge%]
assertEmptyIsDiscouraged: [%phpunit.allRules%, %strictRulesInstalled%, %featureToggles.bleedingEdge%]

The set of enabled rules is therefore the same as before for all three combinations (plain, Bleeding Edge, Bleeding Edge with phpstan-strict-rules), and the tests assert each of them.

One thing is new and intended: setting such a parameter to true replaces the list, so the rule can be enabled explicitly without Bleeding Edge or phpstan-strict-rules.

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.
@VincentLanglet

Copy link
Copy Markdown
Contributor

I'm not sure about it, but I think one time ondrej said he wanted to deprecate the option to select rules one by one on phpstan-strict-rules.
Instead of allowing rules one by one, the baseline by identifier could be used to disable some rules.

@sebastianbergmann

Copy link
Copy Markdown
Contributor Author

I'm not sure about it, but I think one time ondrej said he wanted to deprecate the option to select rules one by one on phpstan-strict-rules. Instead of allowing rules one by one, the baseline by identifier could be used to disable some rules.

I really like the elegance of this approach: I can start using an extension, disable all of its rules by default, and enable them one by one over time (or don't). I would really miss this functionality would it be removed from extensions that already support this.

@VincentLanglet

Copy link
Copy Markdown
Contributor

Found the post #339 (comment)

But I let ondrej chose on this

@ondrejmirtes

Copy link
Copy Markdown
Member

I was prepared to say the same thing: that the strict rules config approach predates being able to ignore errors cleanly by error identifier.

But it looks like you actually like this approach even after identifiers. So I'll merge this and no longer plan to remove this possibility from strict-rules and here.

@ondrejmirtes

Copy link
Copy Markdown
Member

Thank you.

@ondrejmirtes
ondrejmirtes merged commit 80d6e8e into phpstan:2.1.x Dec 1, 2026
113 checks passed
@sebastianbergmann

Copy link
Copy Markdown
Contributor Author

Thank you.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants