phpcs:如何修改 PSR2 以检查大括号是否与方法在同一行? [英] phpcs: How can I modify PSR2 to check that the brace is on the same line as the method?

查看:91
本文介绍了phpcs:如何修改 PSR2 以检查大括号是否与方法在同一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经花了 2 小时以上试图弄清楚如何在方法声明的同一行中要求 { 而不是默认要求是下一行.我怎样才能做到这一点?我已将 PSR2 标准复制到名为 PSR2 的新文件夹中,以便能够根据自己的喜好对其进行修改.所以我正在研究的基础基本上是我想修改的 PSR2 标准.

I've spent now over 2h on trying to figure out how to require the { in the same line as the method declaration instead of the default requirement being the next line. How can I get this done? I've copied the PSR2 standard to a new folder named PSR2 to be ably to modify it to my liking. So the base I'm working on is basically the PSR2 standard which I would like to modify.

我试过rulesset.xml,也试过直接在代码中修改,没有成功.

I've tried the ruleset.xml and I've tried to modify it in the code directly without success.

<rule ref="PEAR.Classes.ClassDeclaration">
    <properties>
        <property name="eolChar" value="{"/>
    </properties>
</rule>
<rule ref="PSR2R.Classes.ClassDeclaration">
    <properties>
        <property name="eolChar" value="{"/>
    </properties>
</rule>

我已经发现这是错误的.EOL 由 phpcs 设置.但我不知道是否有我可以通过规则配置的值.

I've already figured out that this is wrong. The EOL is set by phpcs. But I can't figure out if there is at all a value I can configure via a rule.

到目前为止,这对我来说很好用(搞砸了愚蠢的空间!!!):

This works fine for me so far (screw the silly spaces!!!):

<?xml version="1.0"?>
<ruleset name="PSR2R">
    <description>PSR2 with tabs instead of spaces.</description>
    <arg name="tab-width" value="4"/>
    <rule ref="PSR2">
        <exclude name="Generic.WhiteSpace.DisallowTabIndent"/>
    </rule>
    <rule ref="Generic.WhiteSpace.DisallowSpaceIndent"/>
    <rule ref="Generic.WhiteSpace.ScopeIndent">
        <properties>
            <property name="indent" value="4"/>
            <property name="tabIndent" value="true"/>
        </properties>
    </rule>
</ruleset>

但我想添加上面的规则.

But I would like to add the rule above.

推荐答案

将此代码放入您的 ruleset.xml 文件中:

Put this code in your ruleset.xml file:

<rule ref="PSR2">
    <exclude name="Squiz.Functions.MultiLineFunctionDeclaration.BraceOnSameLine" />
</rule>
<rule ref="Generic.Functions.OpeningFunctionBraceKernighanRitchie" />

这将包括 PSR2 标准,但不包括有关需要在同一行上的支架的特定消息.然后,它包括强制方法和函数大括号位于下一行的通用嗅探.

That will include the PSR2 standard, but exclude the specific message about the brace needing to be on the same line. Then, it includes the Generic sniff that forces method and function braces to be on the following line.

随着这一变化,这段代码:

With that change, this code:

<?php
namespace Test;

class Foo
{
    public function bar() {
    }
}

不会产生任何错误,但直接在其上运行 PSR2 会产生一个错误:

Will produce no errors, but running PSR2 directly over it produces one error:

FILE: temp.php
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
 6 | ERROR | [x] Opening brace should be on a new line
----------------------------------------------------------------------
PHPCBF CAN FIX THE 1 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

这篇关于phpcs:如何修改 PSR2 以检查大括号是否与方法在同一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆