Search code examples
phpwordpressphpcodesniffer

Multi-line function call not indented correctly


In the coding standards that I'm using we use 2 spaces for indentation

<!-- Tabs should represent 2 spaces. -->
<arg name="tab-width" value="2"/>

<!-- Set the default indent value and force spaces instead of tabs -->
<rule ref="WordPress">
    <exclude name="Generic.WhiteSpace.DisallowSpaceIndent" />
</rule>
<rule ref="Generic.WhiteSpace.ScopeIndent">
  <properties>
    <property name="indent" value="2"/>
    <property name="tabIndent" value="false"/>
  </properties>
</rule>
<rule ref="Generic.WhiteSpace.DisallowTabIndent" />

Which is generally working fine, but if I have, for example this

<?php

class MyClass{
  public function my_function() {
    add_submenu_page(
      'my-top-level-slug',
      'My Custom Page',
      'My Custom Page',
      'manage_options',
      'my-top-level-slug'
    );
  }
}

Where the arguments of the add_submenu_page() functions are I am getting

Multi-line function call not indented correctly; expected 8 spaces but found 6

So the 'correct' would look like

<?php

class MyClass{
  public function my_function() {
    add_submenu_page(
        'my-top-level-slug',
        'My Custom Page',
        'My Custom Page',
        'manage_options',
        'my-top-level-slug'
    );
  }
}

But that's 2 spaces too much. I found some issues on github, but didn't find a solution for this.

How to fix this?


Solution

  • Unfortunately, PHPCS doesn't yet have shared config settings, so you need to set the indent settings for the other sniffs that are reporting errors as well.

    If you add the -s command line argument to PHPCS, you'll get a sniff code for each error message. That code tells you which sniff reported the error and needs changing.

    In this case, I think it is the PEAR.Functions.FunctionCallSignature sniff.

    The settings for this sniff can be found here: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties#pearfunctionsfunctioncallsignature

    If that's the one, you'll need to add this to your ruleset:

    <rule ref="PEAR.Functions.FunctionCallSignature">
      <properties>
        <property name="indent" value="2"/>
      </properties>
    </rule