Search code examples
xmlphpcs

PHPCS ruleset.xml "Checked in xxx"


When I am looking at the PSR2 ruleset.xml as an example, I see a lot of commented blocks saying <!-- checked in Files/SideEffectsSniff -->

Here is a little snippet from: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/PSR2/ruleset.xml

<!-- PHP code MUST use only UTF-8 without BOM. -->
<rule ref="Generic.Files.ByteOrderMark"/>

<!-- 2.3. Side Effects -->

<!-- A file SHOULD declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it SHOULD execute logic with side effects, but SHOULD NOT do both. -->
<!-- checked in Files/SideEffectsSniff -->

The rest of the file does not have a rule similar to Files.SideEffectsSniff.

My question is, is the side effects checked or not? And if yes, what rule is responsible for it?

Thank you for clarifying.


Solution

  • Short answer: Yes, side effects are checked by PSR1 and PSR2 and the sniff responsible is this one: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/PSR1/Sniffs/Files/SideEffectsSniff.php and the code to import it into your own standard is PSR1.Files.SideEffects.


    First a note that the ruleset snippet you've posted is actually from PSR1, but PSR2 imports the entire PSR1 standard at the top of its ruleset.xml file, so the sniff is executed when running both standards.

    When I wrote those ruleset.xml files, I used <rule> tags to show when I was importing sniffs from other standards, and used comments to show when the sniff was included inside the standard itself.

    In PHPCS, you can either import sniffs using a code, or include classes inside a Sniffs folder in the same directory as the ruleset.xml file. PSR1 and PSR2 both have their own sniff classes, and these are imported automatically, which is why I don't need to use a <rule> tag in the ruleset.