We have a PHP 5 web application and we're currently evaluating PHP CodeSniffer in order to decide whether forcing code standards improves code quality without causing too much of a headache. If it seems good we will add a SVN pre-commit hook to ensure all new files committed on the dev branch are free from coding standard smells.Is there a way to configure PHP codeSniffer to ignore a particular type of error? or get it to treat a certain error as a warning instead?Here an example to demonstrate the issue:<!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd><html><head>    <meta http-equiv=Content-Type content=text/html; charset=utf-8 /></head><body><div>    <?php    echo getTabContent('Programming', 1, $numX, $numY);    if (isset($msg)) {        echo $msg;    }    ?></div></body></html>And this is the output of PHP_CodeSniffer:> phpcs test.php --------------------------------------------------------------------------------FOUND 2 ERROR(S) AND 1 WARNING(S) AFFECTING 3 LINE(S)--------------------------------------------------------------------------------  1 | WARNING | Line exceeds 85 characters; contains 121 characters  9 | ERROR   | Missing file doc comment 11 | ERROR   | Line indented incorrectly; expected 0 spaces, found 4--------------------------------------------------------------------------------I have a issue with the Line indented incorrectly error. I guess it happens because I am mixing the PHP indentation with the HTML indentation. But this makes it more readable doesn't it? (taking into account that I don't have the resouces to move to a MVC framework right now). So I'd like to ignore it please.
How to ignore certain coding standard errors in PHP CodeSniffer
standards;code;php;code quality
I found a solution for removing the Line indented incorrectly error but first I should say that dragonmantank has a excellent point - you shouldn't mix PHP and HTML. It is a recipe for headaches. However, unfortunatly I think mixing PHP and HTML is very common especially in legacy software.The quickest and dirtyest fix assuming we are using the phpcs default coding standards (which are the PEAR standards) is to remove the relevant Sniff file. Find the location of the PEAR standards, for me, using Ubuntu, it was here:> cd /usr/share/php/PHP/CodeSniffer/Standards/PEAR/Sniffs/WhiteSpaceRename the ScopeIndentSniff.php file so the sniff that checks code indentation will no longer be executed:> sudo mv ScopeIndentSniff.php ScopeIndentSniff.php.bkJob done - now the indentation won't be checked and the Line indented incorrectly error won't occur (and your code won't be standard and won't be such high quality!).The above solution is pretty amateur - the cleaner solution is to create a new coding standard which uses all of the PEAR standards except for your cherry picked customisations. This is very quick to do. Here is how to do it using getIncludedSniffs() and getExcludedSniffs() and some phpcs naming conventions:Go to the directory where the phpcs coding standards are defined:> cd /usr/share/php/PHP/CodeSniffer/StandardsCreate a new directory for your new standard:> sudo mkdir PEARish> cd PEARishCreate your new standard by saving the following in the file:> sudo emacs PEARishCodingStandard.php<?phpclass PHP_CodeSniffer_Standards_PEARish_PEARishCodingStandard extends PHP_CodeSniffer_Standards_CodingStandard{    public function getIncludedSniffs()    {      return array('PEAR');    }    public function getExcludedSniffs()    {      return array('PEAR/Sniffs/WhiteSpace/ScopeIndentSniff.php');    }}?>Test your new customised coding standard by invoking phpcs using the --standard flag. For example:> phpcs --standard=PEARish Test.phpOnce it is working you can set your new standard as the default which means you don't need to type the --standard flag each time you use phpcs:> sudo phpcs --config-set default_standard PEARishSee the phpcs manual for more information.