如何忽略行长度 PHP_CodeSniffer

发布于 2025-01-05 17:10:03 字数 665 浏览 1 评论 0原文

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

如何忽略行长度警告?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

诗化ㄋ丶相逢 2025-01-12 17:10:03

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

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

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

<?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.

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>
微凉 2025-01-12 17:10:03

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

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

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

vendor/squizlabs/php_codesniffer/src/Standards//ruleset.xml

规则名称将位于ref 节点:

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

速度更快,而且速度更快。比创建单独的规则集更方便。

An alternative way of ignoring the message Line exceeds x characters is to use the --exclude flag to exclude the rule.

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

The rule name will be in the ref node:

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

银河中√捞星星 2025-01-12 17:10:03
  1. 查找文件 CodeSniffer/Standards/PEAR/ruleset.xml – 在 mac/linux 上您可以在终端中搜索:

    找到 PEAR/ruleset.xmlsudo find / -name "ruleset.xml"

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


    <规则引用=“Generic.Files.LineLength”>
    <属性>
    <属性名称=“lineLimit”值=“85”/>
    <属性名称=“absoluteLineLimit”值=“0”/>

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

请注意,phpc 的默认编码标准是 PEAR 标准。因此,您需要在此位置编辑ruleset.xml:CodeSniffer/Standards/PEAR/ruleset.xml

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

    locate PEAR/ruleset.xml or 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.

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

倚栏听风 2025-01-12 17:10:03

如果您不想每次都使用参数输入整个命令 --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

If you don't want to type every time whole command with parameters --standard=PSR2 --exclude=Generic.Files.LineLength app/ you can create file phpcs.xml in your main directory with overriden rule setting.

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

You then need to type only following command:

vendor/bin/phpcs
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文