如何使用PHP CodeSniffer设置我的首选缩进级别?

Che*_*eso 9 php codesniffer

我不想忽略缩进级别.

我想强制执行一个非默认的特定缩进级别,4.

显然这是可能的:

在此输入图像描述

怎么样?

关于这些东西的文档似乎在逃避我.

Che*_*eso 5

显然,一种方法是:创建一个新的"标准",创建一个新的ruleset.xml,然后在该ruleset.xml文件中插入一个设置该属性的XML节.

例如,(我在Windows上,所以我的反斜杠都是反斜杠而不是反斜杠)

cd\dev\phpcs\CodeSniffer
mkdir NewStandard

在该目录中,创建ruleset.xml,包含以下内容:

<?xml version="1.0"?>
<ruleset name="Custom Standard">
  <description>My custom coding standard</description>
  <rule ref="PEAR">
    <exclude name="PEAR.Commenting.ClassComment"/>
    <exclude name="PEAR.Commenting.FileComment"/>
    <exclude name="PEAR.Commenting.FunctionComment"/>
    <exclude name="PEAR.Commenting.InlineComment"/>
    <exclude name="PEAR.Classes.ClassDeclaration"/>
    <exclude name="Generic.Files.LineEndings"/>
  </rule>

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

</ruleset>
Run Code Online (Sandbox Code Playgroud)

xml文件中的最后一节设置适当的属性.

要做到这一点,你必须知道这一点

A)缩进嗅(规则)是PEAR.WhiteSpace.ScopeIndent

B)调用该嗅探的属性indent.

然后像往常一样运行phpcs:

\php\php.exe phpcs\scripts\phpcs --standard=NewStandard --report=emacs MyCode.php

文档:

http://pear.php.net/manual/en/package.php.php-codesniffer.annotated-ruleset.php