Skip to content

Commit

Permalink
Merge pull request #367 from Yoast/JRF/yoastcs-test-specific-rules-an…
Browse files Browse the repository at this point in the history
…d-exceptions

YoastCS: add some test specific rules and exceptions
  • Loading branch information
jrfnl committed Dec 14, 2023
2 parents b92c396 + 80662b1 commit a9f5054
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
10 changes: 8 additions & 2 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@
</rule>

<!-- Enforce PSR1 compatible namespaces. -->
<rule ref="PSR1.Classes.ClassDeclaration"/>
<rule ref="PSR1.Classes.ClassDeclaration">
<!-- YoastCS only applies this rule to test files. Overrule it to apply to all files. -->
<include-pattern>*\.php$</include-pattern>
</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
40 changes: 40 additions & 0 deletions Yoast/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -450,4 +450,44 @@
<!-- Modernize: Enforce a nullable type declaration when the param has a null default value (PHP 7.1+). -->
<rule ref="SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue"/>


<!--
#############################################################################
RULES SPECIFICALLY FOR THE AUTOMATED TESTS
A few rules never need to be applied to test files.
Other rules should _only_ be applied to test files.
#############################################################################
-->
<!-- Tests are not run within the context of a WP install, so overwriting WP globals is fine
(as long as they're reset after). -->
<rule ref="WordPress.WP.GlobalVariablesOverride">
<exclude-pattern>*/tests/*\.php$</exclude-pattern>
</rule>

<!-- It is fine to use the PHP json_encode() function for mocking the WP/Yoast alternative
or for setting up the "expected" value. -->
<rule ref="Yoast.Yoast.JsonEncodeAlternative">
<exclude-pattern>*/tests/*\.php$</exclude-pattern>
</rule>

<!-- Double classes may overload methods in the parent class to just change visibility. -->
<rule ref="Generic.CodeAnalysis.UselessOverridingMethod">
<exclude-pattern>*/tests(/*)?/Doubles/*\.php$</exclude-pattern>
</rule>

<!-- Enforce that all test code is namespaced. -->
<rule ref="PSR1.Classes.ClassDeclaration">
<include-pattern>*/tests/*\.php$</include-pattern>

<!-- "One object per file" is already enforced by another sniff, so don't report. -->
<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 a9f5054

Please sign in to comment.