dongxing8766 2014-07-09 19:16
浏览 231
已采纳

PHP代码嗅探器规则

I'm having trouble getting these rules to work:

No space after function name:

// good
public function cities()

// bad
public function cities ()

Ensure space between equals:

// good
$test = 'test';

// bad
$test ='test';
$test= 'test';

Must have spaces after comma in args:

// good
function ($arg1, $arg2)

// bad
function ($arg1,$arg2)
function ($arg1 ,$arg2)

My Rules:

<?xml version="1.0"?>
<ruleset name="PSR2">
<description>The PSR-2 coding standard.</description>

<!-- PHP code MUST use the long <?php ?> tags or the short-echo <?= ?> tags; it MUST NOT use the other tag variations. -->
<rule ref="Generic.PHP.DisallowShortOpenTag.EchoFound">
<severity>0</severity>
</rule>

<!-- PHP code MUST use only UTF-8 without BOM. -->
<rule ref="Generic.Files.ByteOrderMark"/>

<!-- Class names MUST be declared in StudlyCaps. -->
<rule ref="Squiz.Classes.ValidClassName"/>

<!-- Class constants MUST be declared in all upper case with underscore separators. -->
<rule ref="Generic.NamingConventions.UpperCaseConstantName"/>

<!-- Method names MUST be declared in camelCase(). -->
<rule ref="Generic.NamingConventions.CamelCapsFunctionName"/>

<!-- There MUST NOT be trailing whitespace at the end of non-blank lines. -->
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace">

</rule>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.StartFile">
<severity>0</severity>
</rule>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EndFile">
<severity>0</severity>
</rule>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace.EmptyLines">
<severity>0</severity>
</rule>

<!-- There MUST NOT be more than one statement per line. -->
<rule ref="Generic.Formatting.DisallowMultipleStatements"/>

<!-- Code MUST use an indent of 4 spaces, and MUST NOT use tabs for indenting. -->
<rule ref="Generic.WhiteSpace.ScopeIndent">
<properties>
 <property name="ignoreIndentationTokens" type="array" value="T_COMMENT,T_DOC_COMMENT"/>
</properties>
</rule>
<rule ref="Generic.WhiteSpace.DisallowTabIndent"/>

<!-- PHP keywords MUST be in lower case. -->
<rule ref="Generic.PHP.LowerCaseKeyword"/>

<!-- The PHP constants true, false, and null MUST be in lower case. -->
<rule ref="Generic.PHP.LowerCaseConstant"/>

<!-- Method names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility. -->
<rule ref="PSR2.Methods.MethodDeclaration"/>

<!-- When present, there MUST be one blank line after the namespace declaration. -->
<rule ref="PSR2.Namespaces.NamespaceDeclaration"/>

<!-- When present, all use declarations MUST go after the namespace declaration.
    There MUST be one use keyword per declaration.
    There MUST be one blank line after the use block. -->
<rule ref="PSR2.Namespaces.UseDeclaration"/>

<!-- The extends and implements keywords MUST be declared on the same line as the class name.
    The opening brace for the class go MUST go on its own line; the closing brace for the class MUST go on the next line after the body.
    Lists of implements 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 interface per line. -->
<rule ref="PSR2.Classes.ClassDeclaration"/>

<!-- Visibility MUST be declared on all properties.
    The var keyword MUST NOT be used to declare a property.
    There MUST NOT be more than one property declared per statement.
    Property names SHOULD NOT be prefixed with a single underscore to indicate protected or private visibility. -->
<rule ref="PSR2.Classes.PropertyDeclaration"/>

<!-- Visibility MUST be declared on all methods. -->
<rule ref="Squiz.Scope.MethodScope"/>
<rule ref="Squiz.WhiteSpace.ScopeKeywordSpacing"/>

<!-- Method names MUST NOT be declared with a space after the method name. The opening brace MUST go on its own line, and the closing brace MUST go on the next line following the body. There MUST NOT be a space after the opening parenthesis, and there MUST NOT be a space before the closing parenthesis. -->
<rule ref="Squiz.Functions.FunctionDeclaration"/>
<rule ref="Squiz.Functions.LowercaseFunctionKeywords"/>

<!-- In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma. -->
<rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing">
<properties>
 <property name="equalsSpacing" value="1"/>
</properties>
</rule>
<rule ref="Squiz.Functions.FunctionDeclarationArgumentSpacing.SpacingAfterHint">
<severity>0</severity>
</rule>

<!-- Method arguments with default values MUST go at the end of the argument list. -->
<rule ref="PEAR.Functions.ValidDefaultValue"/>

<!-- 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="Squiz.Functions.MultiLineFunctionDeclaration"/>

<!-- When making a method or function call, there MUST NOT be a space between the method or function name and the opening parenthesis, there MUST NOT be a space after the opening parenthesis, and there MUST NOT be a space before the closing parenthesis. In the argument list, there MUST NOT be a space before each comma, and there MUST be one space after each comma.
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. -->
<rule ref="Generic.Functions.FunctionCallArgumentSpacing"/>
<rule ref="PSR2.Methods.FunctionCallSignature.SpaceAfterCloseBracket">
<severity>0</severity>
</rule>

<!-- The general style rules for control structures are as follows:
There MUST be one space after the control structure keyword
There MUST NOT be a space after the opening parenthesis
There MUST NOT be a space before the closing parenthesis
There MUST be one space between the closing parenthesis and the opening brace
The structure body MUST be indented once
The closing brace MUST be on the next line after the body -->
<rule ref="Squiz.ControlStructures.ControlSignature">
<properties>
 <property name="ignoreComments" value="true"/>
</properties>
</rule>
<rule ref="Squiz.WhiteSpace.ScopeClosingBrace"/>
<rule ref="Squiz.ControlStructures.ForEachLoopDeclaration"/>
<rule ref="Squiz.ControlStructures.ForLoopDeclaration"/>
<rule ref="Squiz.ControlStructures.LowercaseDeclaration"/>
<!-- checked in ControlStructures/ControlStructureSpacingSniff -->

<!-- The body of each structure MUST be enclosed by braces. This standardizes how the structures look, and reduces the likelihood of introducing errors as new lines get added to the body. -->
<rule ref="Generic.ControlStructures.InlineControlStructure"/>

</ruleset>
  • 写回答

1条回答 默认 最新

  • drrrdo0802 2014-07-10 00:14
    关注

    Using your standard, I get errors for the 1st and 3rd rules just fine, but not the second.

    Sample code:

    <?php
    // good
    public function cities()
    
    // bad
    public function cities ()
    
    // good
    $test = 'test';
    
    // bad
    $test ='test';
    $test= 'test';
    
    // good
    function ($arg1, $arg2)
    
    // bad
    function ($arg1,$arg2)
    function ($arg1 ,$arg2)
    

    Test run:

    $ phpcs temp.php --standard=mystandard.xml
    
    FILE: temp.php
    --------------------------------------------------------------------------------
    FOUND 4 ERROR(S) AFFECTING 3 LINE(S)
    --------------------------------------------------------------------------------
      6 | ERROR | Expected "function abc(...)"; found "function abc (...)"
     19 | ERROR | Expected 1 space between comma and argument "$arg2"; 0 found
     20 | ERROR | Expected 0 spaces between argument "$arg1" and comma; 1 found
     20 | ERROR | Expected 1 space between comma and argument "$arg2"; 0 found
    --------------------------------------------------------------------------------
    
    Time: 23 ms, Memory: 2.75Mb
    

    To cover the second case, include the following extra sniff in your ruleset:

    <rule ref="Squiz.WhiteSpace.OperatorSpacing"/>
    

    Then you will get:

    FILE: temp.php
    --------------------------------------------------------------------------------
    FOUND 6 ERROR(S) AFFECTING 5 LINE(S)
    --------------------------------------------------------------------------------
      6 | ERROR | Expected "function abc(...)"; found "function abc (...)"
     12 | ERROR | Expected 1 space after "="; 0 found
     13 | ERROR | Expected 1 space before "="; 0 found
     19 | ERROR | Expected 1 space between comma and argument "$arg2"; 0 found
     20 | ERROR | Expected 0 spaces between argument "$arg1" and comma; 1 found
     20 | ERROR | Expected 1 space between comma and argument "$arg2"; 0 found
    --------------------------------------------------------------------------------
    
    Time: 23 ms, Memory: 2.75Mb
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)