微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我已经扩展了一个类别的PSR-2嗅探集.现在检查执行两次 – 即使我的chid类是空的.

为什么?怎么做正确?

编辑:

我现在有一个想法为什么:可能的嗅探器可以从实际调用的标准规则到最高(直接或不包括的)父标准的自动化处理规则.它完全执行它们. (is is so?)OK,但该怎么办?如何覆盖父规则集 – 替换他们的类,但是定制它们,并停用单个规则?

码:

[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 Coding Standards的大约一半已经在PSR-2嗅探文件夹中实施.

目前的目标是不实施整个Zend标准.我想从PSR-2开始,也可以逐步添加/实现其他Zend规则.

问题是,PSR-2嗅探器还包含几个支票,这违反了Zend标准.所以我必须覆盖这些嗅闻.示例:/path/to/PHP/PHP/CodeSniffer/Standards/Squiz/Sniffs/Functions/MultiLineFunctionDeclarationSniff.PHP(在关闭中需要功能关键字后的空格). PSR-2是基于PSR-1,它使用这个嗅探.所以我必须覆盖它们.

排除一个嗅探可以很容易地用排除方向进行,例如:
<?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 manual中注释的sample ruleset.xml.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返回预支付订单号的接口,目前微信支付所有场景均使用这一接口。下面介绍的是其中NATIVE的支付实现流程与PC端实现扫码支付流程
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返回预支付订单号的接口,目前微信支付所有场景均使用这一接口。下面介绍的是其中APP的支付的配置与实现流程
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户信息这个功能的开发流程。 配置 1.首先得在微信公众平台申请一下微信小程序账号并获取到小程序的AppID和AppSecret https://mp.weixin.qq.com/cgi-bin/loginpage?url=%2Fwxamp%2F
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一款开源且免费商用的后台开发框架,它基于ThinkPHP和Bootstrap两大主流技术构建的极速后台开发框架,它有着非常完善且强大的功能和便捷的开发体验,使我逐渐喜欢上了它。
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛,就离不开通讯了,然后我就想到了长连接。这里本人用的是GatewayWorker框架。
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返回预支付订单号的接口,目前微信支付所有场景均使用这一接口。下面介绍的是其中JSAPI的支付实现流程
服务器优化必备:深入了解PHP8底层开发原理
Golang的网络编程:如何快速构建高性能的网络应用?
Golang和其他编程语言的对比:为什么它的开发效率更高?
PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用