How to ignore certain coding standard errors in PHP CodeSniffer

21,348

Solution 1

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/WhiteSpace

Rename the ScopeIndentSniff.php file so the sniff that checks code indentation will no longer be executed:

> sudo mv ScopeIndentSniff.php ScopeIndentSniff.php.bk

Job 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/Standards

Create a new directory for your new standard:
> sudo mkdir PEARish
> cd PEARish

Create your new standard by saving the following in the file:
> sudo emacs PEARishCodingStandard.php
<?php
class 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.php

Once 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 PEARish

See the phpcs manual for more information.

Solution 2

You're going to run into a lot of issues running PHPCS with your PHP code mixed in with HTML. PHPCS is only really useful when you parse pure PHP scripts. The built-in coding standards are built around pure PHP, not mixed PHP/HTML.

One option would be to build your own custom standard and using that instead. The custom standard would take into account the mixing of code, but it would probably be a pain to write.

The other option is to use a templating system since you said you don't want to move to a framework. Smarty and Twig can both be used outside of an MVC framework. Move to one of those and then have PHPCS only parse the .PHP files, not the template files.

Share:
21,348

Related videos on Youtube

Tom
Author by

Tom

Updated on September 17, 2022

Comments

  • Tom
    Tom over 1 year

    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.

    • Admin
      Admin almost 14 years
      this is a bit too much of a programming question IMO to be here.. since it's about a "LINT" like utility that would mostly be of interest to hard-core programmers, not necessarily webmasters. But it is nicely asked and formatted, so we can leave it be for now.
    • Admin
      Admin almost 14 years
      About the line 1 warning: May I suggest using the HTML5 DOCTYPE?
    • Admin
      Admin almost 14 years
      Thanks for your feedback Jeff - I'm happy to move it. I probably should have realised when there was no PHP tag when I created the question :-) .
    • Admin
      Admin almost 14 years
      @luiscubal - he he, good suggestion; like your thinking ;-)