Skip to content

Commit

Permalink
YoastCS rules: enforce final classes for all test files
Browse files Browse the repository at this point in the history
... while allowing `abstract` TestCases.

Note: the typical/default "Doubles" directories within the "tests" directory are exempt from this rule as "Double" classes are often still combined with mocking and making the class `final` would break those tests.

Related to 303

Impact on Yoast packages:

| Plugin/Tool       | Errors/Warnings |
|-------------------|-----------------|
| PHPUnit Polyfills | 25 (fixtures for the tests, these will be exempt)
| WP Test Utils     | 9 (mostly fixtures for the tests)
| YoastCS           | --
| WHIP              | 9
| Yoast Test Helper | --
| Duplicate Post    | 25
| Yst ACF           | 10
| Yst WooCommerce   | 25
| Yst News          | 13
| Yst Local         | 11
| Yst Video         | 101
| Yst Premium       | 126
| Yst Free          | 647
  • Loading branch information
jrfnl committed Dec 14, 2023
1 parent 4154116 commit 80662b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@
</rule>

<!-- Enforce Final classes to prevent issues with the PHPCS autoloader. -->
<rule ref="Universal.Classes.RequireFinalClass"/>
<rule ref="Universal.Classes.RequireFinalClass">
<!-- YoastCS only applies this rule to test files. Overrule it to apply to all files. -->
<include-pattern>*\.php$</include-pattern>
</rule>


<!--
Expand Down
7 changes: 7 additions & 0 deletions Yoast/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -483,4 +483,11 @@
<exclude name="PSR1.Classes.ClassDeclaration.MultipleClasses"/>
</rule>

<!-- Enforce that all test classes are either final or abstract. -->
<rule ref="Universal.Classes.RequireFinalClass">
<include-pattern>*/tests/*\.php$</include-pattern>
<!-- But don't enforce this for Mock/Double classes. -->
<exclude-pattern>*/tests(/*)?/Doubles/*\.php$</exclude-pattern>
</rule>

</ruleset>

0 comments on commit 80662b1

Please sign in to comment.