Create A New Coding Standard

Please follow the below given steps

1) Find out the location of PHP_CodeSniffer install .

locate PHP_CodeSniffer

2) Change the directory to install

cd path/to/PHP_CodeSniffer

3) Create a directory that represent the new coding standard and change the directory to it

sudo mkdir PHPCodezStandard

cd PHPCodezStandard

4) Create the directory sniff that is used to store all the sniff files for this coding standard and change the directory to it

sudo mkdir Sniffs

5) Create a file ruleset.xml

sudo vi ruleset.xml

6) Paste the below given content in ruleset.xml

<?xml version=”1.0″?>
<ruleset name=”PHPCodezStandard”>
<description>New coding standard.</description>
</ruleset>

Then change the directory to Sniffs

cd Sniffs

7) Create the sniff that require a single php file and it’s name should be end with Sniff.php and should be placed in a subdirectory

sudo  mkdir Commenting
cd Commenting
sudo vi PHPCodezSniff.php

8 ) Paste the below given code in PHPCodezSniff.php

<?php
class PHPCodezStandard_Sniffs_Commenting_PHPCOdezSniff implements PHP_CodeSniffer_Sniff
{
public function register()  {
return array(T_COMMENT);
}

public function process(PHP_CodeSniffer_File $phpcsFile, $stackPtr){
$tokens = $phpcsFile->getTokens();
if ($tokens[$stackPtr][‘content’]{0} === ‘#’) {
$error = ‘Hash comments are prohibited; found %s’;
$data  = array(trim($tokens[$stackPtr][‘content’]));
$phpcsFile->addError($error, $stackPtr, ‘Found’, $data);
}

}
}
?>

10) You can use this standard as follows

phpcs –standard=/usr/share/php/data/PHP_CodeSniffer/PHPCodezStandard exception.php

FILE: /var/www/test/exception.php
——————————————————————————–
FOUND 1 ERROR(S) AFFECTING 1 LINE(S)
——————————————————————————–
2 | ERROR | Hash comments are prohibited; found #
——————————————————————————–

Share