Search code examples
phpphpcs

PHPCS ignoring too many indentations


I have updated my phpcs rules in my phpcs.xml to include:

  <rule ref="Generic.WhiteSpace.ScopeIndent">
    <properties>
      <property name="indent" value="2" />
    </properties>
  </rule>

which works perfectly, however if I were to have:

function test($x) {
        $string = 'Hello';
  return $string . $x;
}

No error appears, but it does appear if there is no indentation. I can't figure out what seems to be the issue.


Solution

  • The Generic.WhiteSpace.ScopeIndent sniff does not enforce exact alignment of code by default. It only enforces that there are at least n spaces or tabs before a line.

    The exception to this is when it checks control structures like IF statements, FOREACH loops, or functions. It uses these structures to determine the indentation rules for the rest of the code, so it needs these to be indented to exact columns.

    You can turn on exact checking of all lines by setting the exact sniff property to true. An example of how to do that can be found here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties#genericwhitespacescopeindent