php 如何忽略行长 PHP_CodeSniffer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9280716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 06:29:21  来源:igfitidea点击:

How to Ignore Line Length PHP_CodeSniffer

phpjenkinscontinuous-integrationpearcodesniffer

提问by dextervip

I have been using PHP_CodeSniffer with jenkins, my build.xml was configured for phpcs as below

我一直在用 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> 

And I would like to ignore the following warning

我想忽略以下警告

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

How could I ignore the line length warning?

我怎么能忽略行长警告?

回答by Wrikken

You could create your own standard. The Zend one is quite simple (this is at /usr/share/php/PHP/CodeSniffer/Standards/Zend/ruleset.xmlin my Debian install after installing it with PEAR). Create another one based on it, but ignore the line-length bit:

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

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

And set --standard=/path/to/your/ruleset.xml.

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

Optionally, if you just want to up the char count before this is triggered, redefine the rule:

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

 <!-- 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>

回答by Hyder B.

An alternative way of ignoring the message Line exceeds x charactersis to use the --excludeflag to exclude the rule.

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

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

In order to find the rule name to exclude, find your corresponding ruleset in following directory:

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

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

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

The rule name will be in the refnode:

规则名称将在ref节点中:

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

It's quicker & less cumbersome than creating a separate ruleset.

与创建单独的规则集相比,它更快且更简单。

回答by cofirazak

  1. Find file CodeSniffer/Standards/PEAR/ruleset.xml – on mac/linux you can search in terminal:

    locate PEAR/ruleset.xmlor sudo find / -name "ruleset.xml"

  2. Then you need to find the following lines in the 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. Just change the number 85 (max length of the line) to what you want.

  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(行的最大长度)更改为您想要的。

Notice that the phpc's default coding standard is the PEAR standard. So you need to edit ruleset.xml at this location: CodeSniffer/Standards/PEAR/ruleset.xml

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