Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
PSR2 with tabs instead of spaces
<?xml version="1.0"?>
<ruleset name="MyStandard">
<description>PSR2 with tabs instead of spaces.</description>
<arg name="tab-width" value="4"/>
<rule ref="PSR2">
<exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
</rule>
<rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
<rule ref="Generic.WhiteSpace.ScopeIndent">
<properties>
<property name="indent" value="4"/>
<property name="tabIndent" value="true"/>
</properties>
</rule>
</ruleset>
@gsherwood
Copy link
Author

gsherwood commented Dec 7, 2014

This custom ruleset for PHP_CodeSniffer version 2.0.0+ will validate your code against the PSR-2 standard, but enforce the use of tabs for indenting instead of spaces.

To use it, save the ruleset.xml file somewhere and then run PHP_CodeSniffer like this:

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

This ruleset also works with the auto-fixing feature of PHPCBF, so it will find space indents and replace them with tab indents:

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

A few things to note about this custom ruleset:

  • The tab-width is hard-coded to 4 spaces to match the default values used in other parts of the PSR-2 standard. You shouldn't need to change this value.
  • The only change from the PSR-2 standard is that the DisallowTabIndent sniff is excluded and the DisallowSpaceIndent sniff is included in its place. All other rules, including when indents must be used, are the same.
  • The property changes for the ScopeIndent sniff are there to allow PHPCBF to fix indentation using tabs instead of spaces. Again, it uses a tab-width of 4 spaces (its default) but this shouldn't need to be changed.

Loading

@Kwaadpepper
Copy link

Kwaadpepper commented Jan 26, 2015

Thanks for this !

Loading

@csuarezgfx
Copy link

csuarezgfx commented Apr 24, 2015

noob here - what does '/path/to/code' reference?

Loading

@dereuromark
Copy link

dereuromark commented Sep 15, 2015

👍 Now all that is missing for a sane and consistent code style would be the curly brackets on the same line ( https://github.com/php-fig-rectified/fig-rectified-standards ). Any idea how to do that?

Loading

@Ovsyanka
Copy link

Ovsyanka commented Jan 29, 2017

Now all that is missing for a sane and consistent code style would be the curly brackets on the same line ( https://github.com/php-fig-rectified/fig-rectified-standards ). Any idea how to do that?

Yes. I did this in my fork. There is the explanation:

For functions you should

  • exclude Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine
  • include Generic.Functions.OpeningFunctionBraceKernighanRitchie

For classes you should

  • exclude PSR2.Classes.ClassDeclaration.OpenBraceNewLine
  • include Generic.Classes.OpeningBraceSameLine

Loading

@djibarian
Copy link

djibarian commented May 22, 2020

I’m having trouble with these rules and the following lines:

if(condition)
    foreach(blah){
    }

getting an error

 358 | ERROR | [x] Line indented incorrectly; expected 3 tabs, found
         |       |     4
         |       |     (Generic.WhiteSpace.ScopeIndent.IncorrectExact)

Looks like the lack of brackets in the conditional doesn’t increment the expected indentation level. Is that a bug?

Loading

@VottusCode
Copy link

VottusCode commented Apr 14, 2021

Richard Hendricks would be proud :)

Loading

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment