phpphpcodesnifferpsr-12

Tell phpcs to use a different indent inside control structures


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?


Solution

  • It is possible to change the default. To change the default value for indentation in the PSR12 spec from 4 spaces to 2 spaces, put this in the phpcs settings file (phpcs.xml):

    <rule ref="PSR12.ControlStructures.ControlStructureSpacing">
      <properties>
        <property name="indent" value="2" />
      </properties>
    </rule>
    

    See https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties#psr12controlstructurescontrolstructurespacing