Search code examples
phpcodesnifferphpcs

PhpCS exclude sniffs not working


I am running Php 7.0 on my machine but I need to check code that executes in both 7.0 and 5.6. As such, I get a load of

Squiz.Commenting.FunctionComment.ScalarTypeHintMissing

errors when using the -s flag.

I have installed PhpCS via composer for a project which installed 2.6. As I wanted to use the --exclude flag for a number of projects I installed 2.7 globally via composer.

I am trying to use the new --exclude flag but I keep getting issues. First I tried

phpcs -n --standard=phpcsdocblocks.xml --exclude=Squiz.Commenting.FunctionComment.ScalarTypeHintMissing src/

but it complained the sniff didn't exist, so I dropped the Squiz prefix and phpcs ran, but ignored the flag. I have tried a number of sniffs but nothing is excluded.

I have also tried the --sniffs flag and again, the sniffs are ignored. I can't understand what I am overlooking.

I need to ignore these sniffs via CLI.

For reference, the phpcsdocblocks.xml contains

<?xml version="1.0"?>
<ruleset name="MyStandard">
  <description>My custom coding standard.</description>
  <rule ref="Squiz.Commenting.FunctionComment" />
  <rule ref="Squiz.Commenting.FunctionCommentThrowTag" />
  <rule ref="Squiz.Commenting.VariableComment" />
</ruleset>

EDIT: Bonus points if anyone can tell me where to find the phpcs global config file on ubuntu


Solution

  • You can't exclude a single message like that - you need to exclude the entire sniff when using the command line. So you need to do something like --exclude=Squiz.Commenting.FunctionComment.

    But this is probably not what you are after because it excludes all messages coming from that sniff. So you'll need to ignore this specific message in the ruleset.xml file, possibly having a different file for php5 and php7 testing. I imagine the PHP5 one would just included the PHP7 one and then exclude the specific message.

    Something like phpcsdocblocks56.xml:

    <?xml version="1.0"?>
    <ruleset name="Custom Standard">
      <rule ref="./phpcsdocblocks.xml" />
      <rule ref="Squiz.Commenting.FunctionComment.ScalarTypeHintMissing">
        <severity>0</severity>
      </rule>
    </ruleset>
    

    Or this may be cleaner:

    <?xml version="1.0"?>
    <ruleset name="Custom Standard">
      <rule ref="./phpcsdocblocks.xml">
        <exclude name="Squiz.Commenting.FunctionComment.ScalarTypeHintMissing"/>
      </rule>
    </ruleset>
    

    If you can run PHP 5.6 on your machine along side PHP7, you can also run PHPCS using PHP 5.6 and it will hide those messages automatically as it knows the version of PHP doesn't support them.