Just how to overlook particular coding typical mistakes in PHP CodeSniffer

We have a PHP 5 internet application and also we're presently reviewing PHP CodeSniffer in order to determine whether compeling code standards boosts code quality without creating way too much of a frustration. If it appears excellent we will certainly add a SVN pre-commit hook to make certain all new documents devoted on the dev branch are without coding typical scents.

Exists a means to set up PHP codeSniffer to overlook a certain sort of mistake? or get it to deal with a particular mistake as a caution rather?

Below an instance to show the concern:

<!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 also this is the result 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 concern with the "Line indented inaccurately" mistake. I presume it takes place due to the fact that I am blending the PHP impression with the HTML impression. Yet this makes it extra legible does not it? (thinking about that I do not have the resouces to relocate to a MVC structure now). So I would certainly such as to overlook it please.

0
2019-05-04 17:15:11
Source Share
Answers: 2

You're mosting likely to face a great deal of concerns running PHPCS with your PHP code blended in with HTML. PHPCS is just actually valuable when you parse pure PHP manuscripts. The constructed - in coding standards are constructed around pure PHP, not combined PHP/HTML.

One alternative would certainly be to construct your very own personalized criterion and also making use of that rather. The personalized criterion would certainly think about the blending of code, yet it would possibly be a discomfort to write

The various other alternative is to make use of a templating system given that you claimed you do not intend to relocate to a structure. Smarty and also Twig can both be made use of beyond an MVC structure. Relocate to among those and afterwards have PHPCS just parse the.PHP files, not the layout files.

0
2019-05-08 22:28:12
Source

I located a remedy for getting rid of the" Line indented inaccurately " mistake yet first I need to claim that dragonmantank has a superb factor - you should not blend PHP and also HTML. It is a dish for frustrations. Nonetheless, unfortunatly I assume blending PHP and also HTML is really usual specifically in heritage software program.

The quickest and also dirtyest solution thinking we are making use of the phpcs default coding standards (which are the PEAR standards) is to remove the pertinent Sniff documents. Locate the area of the PEAR standards, for me, making use of Ubuntu, it was below :

> cd /usr/share/php/PHP/CodeSniffer/Standards/PEAR/Sniffs/WhiteSpace

Rename the ScopeIndentSniff.php documents so the smell that checks code impression will certainly no more be implemented :

> sudo mv ScopeIndentSniff.php ScopeIndentSniff.php.bk

Job done - currently the impression will not be examined and also the" Line indented inaccurately " mistake will not take place ( and also your code will not be typical and also will not be such excellent quality! ).


The above remedy is rather amateur - the cleaner remedy is to create a new coding criterion which makes use of every one of the PEAR standards with the exception of your cherry selected customisations. This is really fast to do. Below is just how to do it making use of getIncludedSniffs() and also getExcludedSniffs() and also some phpcs calling 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 tailor-maked coding typical by conjuring up phpcs making use of the --standard flag. As an example :

> phpcs --standard=PEARish Test.php

Once it is functioning you can set your new criterion as the default which suggests you do not require to type the --standard flag each time you make use of phpcs :

> sudo phpcs --config-set default_standard PEARish

See the phpcs manual for additional information.

0
2019-05-08 20:47:32
Source