如何正确覆盖PHP CodeSniffer规则集中的规则/嗅探并避免重复检查代码?

aut*_*tix 12 php coding-style codesniffer zend-framework2

我扩展了一类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>
Run Code Online (Sandbox Code Playgroud)

[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')
    {
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

呼叫:

$ 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)
--------------------------------------------------------------------------------
Run Code Online (Sandbox Code Playgroud)

背景资料:

我想为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.phpfunction关键字后面需要一个空格).PSR-2基于PSR-1,它使用了这个嗅探器.所以我必须覆盖它们.

aut*_*tix 20

通过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>
Run Code Online (Sandbox Code Playgroud)

代替

<?xml version="1.0"?>
<ruleset name="ZF">
    <description>...</description>
    <!-- Include the whole PSR-2 standard -->
    <rule ref="PSR2"/>
    ...
</ruleset>
Run Code Online (Sandbox Code Playgroud)

覆盖 =排除嗅探+创建替代嗅探.

参见注释样品ruleset.xmlPHP CodeSniffer手册.