强制空格缩进而不是PHP_CodeSniffer中的制表符?

时间:2017-07-15 15:41:28

标签: php wordpress codesniffer phpcodesniffer

在我的自定义嗅探中,我添加了

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

适用于其中的函数和控制结构,但例如

function test_plugin_admin_enqueue( $hook ) {
  /**
   * Load only on ?page=test-ajax-admin.php
   * The easiest way to find out the hook name is to go to the
   * options page and put print_r( $hook ); before the conditional.
   */
  if ( $hook !== 'toplevel_page_test-ajax-admin' ) {
    return;
  }

  wp_enqueue_script( 'test-plugin-admin-script', plugin_dir_url( __FILE__ ) . 'js/admin.js', array( 'jquery' ) );
}

return将出现错误Tabs must be used to indent lines, spaces are not allowed

有没有办法检查是否有正确的间距,而不使用制表符,只有空格?

1 个答案:

答案 0 :(得分:2)

如果你想使用WordPress编码标准,但你想缩进使用空格,你需要覆盖ScopeIndent设置(如你所做的那样),排除提供全面禁止空间缩进的嗅探,并包括嗤之以鼻,禁止标签缩进。

这个规则集可能就是你想要的:

<?xml version="1.0"?>
<ruleset name="MyStandard">
    <description>WordPress with space indent.</description>
        <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" />
</ruleset>

如果其他人想要做类似的事情,但使用4个空格的缩进,只需将2更改为规则集中的4,它就会按照您想要的方式运行。

如果您实际上没有包含整个WordPress标准(可能只是WordPress-Core),请相应地更改WordPress参考。

相关问题