Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  Jeff Atwood Jul 16 '10 at 23:28
2  
About the line 1 warning: May I suggest using the HTML5 DOCTYPE? –  luiscubal Jul 16 '10 at 23:30
    
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 :-) . –  Tom Jul 16 '10 at 23:32
    
@luiscubal - he he, good suggestion; like your thinking ;-) –  Tom Jul 16 '10 at 23:43

3 Answers 3

up vote 8 down vote accepted

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.

share|improve this answer
    
This was really handy - I used this answer to remove errors coming up along the lines of "Class must be in a namespace of at least one level" along with camel case function naming errors. I'm using PSR2 as much as I can, but editing a Magento site, I'm not in a position to go through and rename every function and sort out namespacing. –  Dave Child Dec 31 '13 at 16:49

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|improve this answer

Take the new way: http://www.squizlabs.com/view?a=2697

share|improve this answer
    
Good point - The new version of PHPCS (v1.3) looks great - I highly recommend using it over the one referenced by this question (v1.2.2). –  Tom Jan 13 '11 at 9:12

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.