簡體   English   中英

如何正確覆蓋PHP CodeSniffer規則集中的規則/嗅探並避免重復檢查代碼?

[英]How to override a rule/sniff in a PHP CodeSniffer ruleset correctly and to avoid doublechecking of code?

我擴展了一類PSR-2嗅探器。 現在檢查執行兩次 - 即使我的chid類是空的。

為什么? 以及如何正確地做到這一點?


編輯:

我現在有了一個想法:可能是Sniffer自下而上處理規則集 - 從實際調用的標准規則集到最高(直接或不正確包含)父標准。 它顯然完全執行它們。 (是嗎?)好的,但該怎么辦? 如何覆蓋父規則集 - 替換他們的類而不是自定義規則集並停用單個規則?


碼:

[CodeSniffer] /Standards/ZF/ruleset.xml

<?xml version="1.0"?>
<ruleset name="ZF">
    <description>...</description>
    <!-- Include the whole PSR-2 standard -->
    <rule ref="PSR2"/>
    <!-- Argument lists MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line. When the argument list is split across multiple lines, the closing parenthesis and opening brace MUST be placed together on their own line with one space between them. -->
    <rule ref="ZF.Functions.MultiLineFunctionDeclaration"/>
    ... just comments yet
    <!-- 5.7.2. Closure Definitions -->
    <!-- TODO: Revome unwished check: Space after the function keyword is required. -->
    <!-- 5.7.3. Function and Method Usage -->
    <!-- TODO: Revome unwished check: one argument per line in a multi-line function call is required. -->
    ... just comments yet
</ruleset>

[CodeSniffer] /Standards/ZF/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php

<?php
if (class_exists('PEAR_Sniffs_Functions_FunctionDeclarationSniff', true) === false) {
    $error = 'Class PEAR_Sniffs_Functions_FunctionDeclarationSniff not found';
    throw new PHP_CodeSniffer_Exception($error);
}

class ZF_Sniffs_Functions_MultiLineFunctionDeclarationSniff extends PEAR_Sniffs_Functions_FunctionDeclarationSniff
{
    public function processMultiLineDeclaration(PHP_CodeSniffer_File $phpcsFile, $stackPtr, $tokens)
    {
    }

    public function processBracket(PHP_CodeSniffer_File $phpcsFile, $openBracket, $tokens, $type='function')
    {
    }
}
?>

呼叫:

$ phpcs --standard=ZF -sw /path/to/Test.php

FILE: /path/to/Test.php
--------------------------------------------------------------------------------
FOUND 2 ERROR(S) AFFECTING 1 LINE(S)
--------------------------------------------------------------------------------
 106 | ERROR | Expected 1 space after FUNCTION keyword; 0 found
     |       | (ZF.Functions.MultiLineFunctionDeclaration.SpaceAfterFunction)
 106 | ERROR | Expected 1 space after FUNCTION keyword; 0 found
     |       | (Squiz.Functions.MultiLineFunctionDeclaration.SpaceAfterFunction)
--------------------------------------------------------------------------------

背景資料:

我想為Zend Framework 2項目編寫一個PHP CodeSniffer規則集。 大約一半的Zend Framework 2編碼標准已經在PSR-2 sniffs文件夾中實現。

現在的目標不是實現整個Zend標准。 我想從PSR-2開始,可能會逐步添加/實現其他Zend規則。

問題是,PSR-2嗅探還包含幾項檢查,打破了Zend標准。 所以我必須覆蓋這些嗅探。 示例: /path/to/php/PHP/CodeSniffer/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.php (在閉包中的function關鍵字后面需要一個空格)。 PSR-2基於PSR-1,它使用了這個嗅探器。 所以我必須覆蓋它們。

單嗤之以鼻剔除易與進行exclude的方向,例如:

<?xml version="1.0"?>
<ruleset name="ZF">
    <description>...</description>
    <!-- Include the whole PSR-2 standard -->
    <rule ref="PSR2">
        <!-- to disable a single error -->
        <exclude name="Squiz.Functions.MultiLineFunctionDeclaration.SpaceAfterFunction"/>
        <!-- or to disable the whole sniff -->
        <exclude name="Squiz.Functions.MultiLineFunctionDeclaration"/>
    </rule>
    ...
</ruleset>

代替

<?xml version="1.0"?>
<ruleset name="ZF">
    <description>...</description>
    <!-- Include the whole PSR-2 standard -->
    <rule ref="PSR2"/>
    ...
</ruleset>

覆蓋 =排除嗅探+創建替代嗅探。

另請參閱PHP CodeSniffer手冊中帶注釋的示例ruleset.xml

暫無
暫無

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

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