簡體   English   中英

如何忽略行長 PHP_CodeSniffer

[英]How to Ignore Line Length PHP_CodeSniffer

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

我怎么能忽略行長警告?

您可以創建自己的標准。 Zend 非常簡單(這是在我的 Debian 安裝中的/usr/share/php/PHP/CodeSniffer/Standards/Zend/ruleset.xml與 PEAR 一起安裝后)。 基於它創建另一個,但忽略行長位:

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

忽略消息行超過 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>

與創建單獨的規則集相比,它更快、更簡單。

  1. 找到文件 CodeSniffer/Standards/PEAR/ruleset.xml – 在 mac/linux 上你可以在終端中搜索:

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

如果您不想每次都輸入帶有參數的整個命令--standard=PSR2 --exclude=Generic.Files.LineLength app/您可以使用覆蓋規則設置在主目錄中創建文件phpcs.xml

<?xml version="1.0"?>
<ruleset name="PHP_CodeSniffer">
    <rule ref="PSR2"> <!-- ruleset standard -->
        <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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM