45

我一直在使用带有 jenkins 的 PHP_CodeSniffer,我的 build.xml 是为 phpcs 配置的,如下所示

<target name="phpcs">
    <exec executable="phpcs">
        <arg line="--report=checkstyle --report-file=${basedir}/build/logs/checkstyle.xml --standard=Zend ${source}"/>
    </exec>
</target> 

我想忽略以下警告

FOUND 0 ERROR(S) AND 1 WARNING(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
 117 | WARNING | Line exceeds 80 characters; contains 85 characters
--------------------------------------------------------------------------------

我怎么能忽略行长警告?

4

4 回答 4

67

您可以创建自己的标准。Zend 非常简单(这是/usr/share/php/PHP/CodeSniffer/Standards/Zend/ruleset.xml在我用 PEAR 安装后的 Debian 安装中)。基于它创建另一个,但忽略 line-length 位:

<?xml version="1.0"?>
<ruleset name="Custom">
 <description>Zend, but without linelength check.</description>
 <rule ref="Zend">
  <exclude name="Generic.Files.LineLength"/>
 </rule>
</ruleset>

并设置--standard=/path/to/your/ruleset.xml

或者,如果您只想在触发之前增加字符数,请重新定义规则:

 <!-- Lines can be N chars long (warnings), errors at M chars -->
 <rule ref="Generic.Files.LineLength">
  <properties>
   <property name="lineLimit" value="N"/>
   <property name="absoluteLineLimit" value="M"/>
  </properties>
 </rule>
于 2012-02-14T17:35:45.140 回答
18

忽略消息行超出 x 个字符的另一种方法是使用--exclude标志来排除规则。

vendor/bin/phpcs --standard=PSR2  --exclude=Generic.Files.LineLength app/

为了找到要排除的规则名称,请在以下目录中找到相应的规则集:

vendor/squizlabs/php_codesniffer/src/Standards/<coding standard>/ruleset.xml

规则名称将在ref节点中:

 <rule ref="Generic.Files.LineLength">
        <properties>
            <property name="lineLimit" value="120"/>
            <property name="absoluteLineLimit" value="0"/>
        </properties>
 </rule>

它比创建单独的规则集更快、更简单。

于 2019-08-23T13:47:46.933 回答
4
  1. 查找文件 CodeSniffer/Standards/PEAR/ruleset.xml – 在 mac/linux 上,您可以在终端中搜索:

    locate PEAR/ruleset.xml或者sudo find / -name "ruleset.xml"

  2. 然后你需要在ruleset.xml中找到以下几行:

    <!-- Lines can be 85 chars long, but never show errors --> <rule ref="Generic.Files.LineLength"> <properties> <property name="lineLimit" value="85"/> <property name="absoluteLineLimit" value="0"/> </properties> </rule>

  3. 只需将数字 85(行的最大长度)更改为您想要的。

请注意,phpc 的默认编码标准是 PEAR 标准。所以你需要在这个位置编辑ruleset.xml:CodeSniffer/Standards/PEAR/ruleset.xml

于 2015-08-03T20:23:03.420 回答
1

如果您不想每次都输入带有参数的整个命令,--standard=PSR2 --exclude=Generic.Files.LineLength app/您可以在主目录中使用覆盖规则设置创建文件phpcs.xml

<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">

    <rule ref="PSR2" /> <!-- ruleset standard -->
    <rule ref="Generic.Files.LineLength"> <!-- rule to override -->
        <properties>
            <property name="lineLimit" value="150"/> <!-- maximum line length -->
        </properties>
    </rule>
    <file>app</file> <!-- directory you want to analyze -->
    <arg name="encoding" value="utf-8"/>
</ruleset>

然后,您只需键入以下命令:

vendor/bin/phpcs
于 2021-01-04T18:19:25.157 回答