Tell phpcs to use a different indent inside control structures

55 Views Asked by At

Output of phpcs is telling me to put 8 spaces inside the if

class bla
{
  private function wop()
  {
    if (true) {
      $something = 'yes'; // 6 spaces indent is what I want!
    }
  }
}

indented at least once; expected at least 8 spaces, but found 6 (PSR12.ControlStructures.ControlStructureSpacing.LineIndent) Each line in a multi-line control structure must be

How do I tell phpcs that I prefer 6 spaces?

2

There are 2 best solutions below

0
On BEST ANSWER

Indentation size is part of PSR-12 so it isn't possible to fine-tune it if you want to comply with that coding style:

2.4 Indenting

Code MUST use an indent of 4 spaces for each indent level, and MUST NOT use tabs for indenting.

2
On

How do I tell phpcs that I prefer 6 spaces?

Look into the default rules that ship with phpcs itself. There is an easy system where you can pick and play with every single rule and their parameters.

A large set of them are so called Customisable Sniff Properties and from the Sniff you'd like to tailor — PSR12 — and the name of the rule in the violation — PSR12.ControlStructures.ControlStructureSpacing.LineIndent — we have the educated guess that it is the indent property of PSR12.ControlStructures.ControlStructureSpacing.

One of the rules that this sniff enforces is the indent of each condition in a control structure when the conditions have been split over multiple lines. By default, this sniff ensures that the conditions are indented 4 spaces, but you can change the size of the indent by setting the indent property. [src]

To have your configuration with your project, I'd recommend using a default configuration file.

In general, IIRC, there is one folder with many standard whitespace rules, it should be re-using the existing rule for the indent and setting your preference of six spaces via parameters (these are XML Documents).

Btw. the whitespace handling is pretty superior in phpcs/phpcbf, I've not seen any other tooling that came close. However, this may mean you'd need to tweak more than one parameter/rule/sniff.

YMMV, so please let me know if this information was already helpful, otherwise ask in comments for further clarifications.