Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP, indentation width of 4, and CodeSniffer: is "4" a de-facto standard?

I've asked a question here because all my Php code is indented by 2, not 4.

Php CodeSniffer, which is a really great tool, has an indentation of "4" and it seems you can't change that value.

Php CodeSniffer has a lot of options. So this it should be easy to add "indentation width". But it seems the creators have voluntary avoided the possibility, because it's not possible (which is weird IMHO).

So if you can't change this, I was wondering: is "indentation width = 4" a de-facto standard?

Is this the same for Java / JavaDoc generation ?

Do I have to change all my 10.000+ lines of code to a 4 - indendation width?

like image 678
Olivier Pons Avatar asked Dec 20 '22 23:12

Olivier Pons


2 Answers

Any public member var in sniffs can be overwritten by using a ruleset.xml file. A ruleset.xml file is an XML representation of your own custom coding standard. It can import sniffs from any of the included standards and can also include your own custom sniffs if you choose to write them.

In your case, you want the exact PEAR standard, but with the indent value changed. So create a file mystandard.xml and make this the contents:

<?xml version="1.0"?>
<ruleset name="MyStandard">
 <description>My custom PEAR coding standard.</description>
 <rule ref="PEAR"/>
 <rule ref="PEAR.WhiteSpace.ScopeIndent">
  <properties>
   <property name="indent" value="2"/>
  </properties>
 </rule>
</ruleset>

Now, instead of running PHP_CodeSniffer using the command phpcs --standard=PEAR ... you use phpcs --standard=/path/to/mystandard.xml ...

Note that you can call the standard anything you want and change the XML metadata (name and description) to something more appropriate.

There is a lot more you can do with a custom ruleset.xml file. You can find more information about custom rulesets here: http://pear.php.net/manual/en/package.php.php-codesniffer.annotated-ruleset.php

like image 185
Greg Sherwood Avatar answered Apr 23 '23 06:04

Greg Sherwood


No it's not a de-facto standard. There is no standard for indentation.

You can choose rules using --sniffs -- see the manual. You don't need to do the indentation test.

If PHP CodeSniffer was any good, it would be possible to define the indentation style in your coding standard. There is no mention of it in the documentation though.

Alternatively, you can write your own rule as described here: http://pear.php.net/manual/en/package.php.php-codesniffer.coding-standard-tutorial.php

like image 40
j13r Avatar answered Apr 23 '23 08:04

j13r