Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying PEAR standard for phpcs

I'd like to make a small change to the PEAR standard for our phpcs validation. At the moment PEAR requires you to write else statements like this:

} else {

We'd like to write them like this:

}
else {

How can I go about making this change?

like image 616
Noodles Avatar asked Nov 22 '12 02:11

Noodles


1 Answers

The only way to do this is to write your own coding standard using a ruleset.xml file. It needs to import the whole PEAR coding standard but exclude the specific sniff performing this check. This bit can be done with the single XML file.

But there are no built-in sniffs that enforce the type of else syntax you are looking for. So to enforce that, you'll need to write a custom sniff, which is more complicated as you also have to store it somewhere.

If you want to start by trying the simple custom coding standard, create a file called ruleset.xml and make this the contents:

<?xml version="1.0"?>
<ruleset name="MyStandard">
 <description>My custom coding standard.</description>
 <rule ref="PEAR">
  <exclude name="PEAR.ControlStructures.ControlSignature"/>
 </rule>
</ruleset>

Then run PHP_CodeSniffer like this:

phpcs --standard=/path/to/ruleset.xml /path/to/code

This will check your code against the PEAR coding standard but exclude the specific checks for control structures. If this is all you need, you can stop there. If you have a team of people you want running this custom standard, just give them the file and they can use it as well.

There is more info about the ruleset.xml format here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml

If you want to go ahead and write a custom sniff, it is best to start with the basic tutorial: http://pear.php.net/manual/en/package.php.php-codesniffer.coding-standard-tutorial.php

This will explain what a coding standard is. Then, you'll want to create your own sniff, which is just a copy of the PEAR one with slight changes for how you do control structured. The PEAR sniff is here: https://github.com/squizlabs/PHP_CodeSniffer/blob/master/CodeSniffer/Standards/PEAR/Sniffs/ControlStructures/ControlSignatureSniff.php

Without testing, I assume you'd want to change } else {EOL', to }EOLelse {EOL', and see if that works.

like image 193
Greg Sherwood Avatar answered Oct 22 '22 03:10

Greg Sherwood