在PHP_CodeSniffer中强制使用空格缩进而不是制表符?

问题描述:

在我的自定义嗅出我加在PHP_CodeSniffer中强制使用空格缩进而不是制表符?

<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

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

如果您想使用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空间缩进,只是在规则集中更改24,它的工作方式将你想。

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

+0

完美!我尝试着用'DisallowSpaceIndent'和'DisallowTabIndent'来摆弄,但我无法让它正常工作。谢谢! –