【问题标题】:Ignore code snippets in PHP_CodeSniffer忽略 PHP_CodeSniffer 中的代码片段
【发布时间】:2011-05-17 10:02:38
【问题描述】:

当被PHP_CodeSniffer分析时,可以忽略php文件中的某些部分代码吗?

【问题讨论】:

    标签: php codesniffer


    【解决方案1】:

    是的,可以使用 @codingStandardsIgnoreStart 和 @codingStandardsIgnoreEnd 注释

    <?php
    some_code();
    // @codingStandardsIgnoreStart
    this_will_be_ignored();
    // @codingStandardsIgnoreEnd
    some_other_code();
    

    也有描述in the documentation.

    【讨论】:

    • 是否可以忽略某些规则?
    • @TroodoN-Mike:不,现在不是。现在,可以忽略整个文件,也可以忽略上面用 Start 和 End cmets 分隔的块。
    • @TroodoN-Mike:在 CodeSniffer 1.3 版中,您可以在 ruleset.xml 文件级别从特定文件 (squizlabs.com/php-codesniffer/rule-based-exclude-patterns) 中排除特定嗅探。但这不适用于给定文件的特定部分。
    • 是否有代码 sn-p 也可以忽略复杂性?:)
    • 请参阅stackoverflow.com/a/52881595/6523409 了解允许忽略某些规则的新语法。
    【解决方案2】:

    您可以使用组合:@codingStandardsIgnoreStart@codingStandardsIgnoreEnd,也可以使用 @codingStandardsIgnoreLine

    示例:

    <?php
    
    command1();
    // @codingStandardsIgnoreStart
    command2(); // this line will be ignored by Codesniffer
    command3(); // this one too
    command4(); // this one too
    // @codingStandardsIgnoreEnd
    
    command6();
    
    // @codingStandardsIgnoreLine
    command7(); // this line will be ignored by Codesniffer
    

    【讨论】:

    【解决方案3】:

    在 3.2.0 版本之前,PHP_CodeSniffer 使用不同的语法来忽略文件中的部分代码。请参阅 Anti Veeranna'sMartin Vseticka's 答案。旧语法将在 4.0 版中移除

    PHP_CodeSniffer 现在使用 // phpcs:disable// phpcs:enable cmets 忽略部分文件,使用 // phpcs:ignore 忽略一行。

    现在,还可以仅禁用或启用特定的错误消息代码、嗅探、嗅探类别或整个编码标准。您应该在 cmets 之后指定它们。如果需要,您可以使用-- 分隔符添加注释,解释为何禁用和重新启用嗅探。

    <?php
    
    /* Example: Ignoring parts of file for all sniffs */
    $xmlPackage = new XMLPackage;
    // phpcs:disable
    $xmlPackage['error_code'] = get_default_error_code_value();
    $xmlPackage->send();
    // phpcs:enable
    
    /* Example: Ignoring parts of file for only specific sniffs */
    // phpcs:disable Generic.Commenting.Todo.Found
    $xmlPackage = new XMLPackage;
    $xmlPackage['error_code'] = get_default_error_code_value();
    // TODO: Add an error message here.
    $xmlPackage->send();
    // phpcs:enable
    
    /* Example: Ignoring next line */
    // phpcs:ignore
    $foo = [1,2,3];
    bar($foo, false);
    
    /* Example: Ignoring current line */
    $foo = [1,2,3]; // phpcs:ignore
    bar($foo, false);
    
    /* Example: Ignoring one line for only specific sniffs */
    // phpcs:ignore Squiz.Arrays.ArrayDeclaration.SingleLineNotAllowed
    $foo = [1,2,3];
    bar($foo, false);
    
    /* Example: Optional note */ 
    // phpcs:disable PEAR,Squiz.Arrays -- this isn't our code
    $foo = [1,2,3];
    bar($foo,true);
    // phpcs:enable PEAR.Functions.FunctionCallSignature -- check function calls again
    bar($foo,false);
    // phpcs:enable -- this is out code again, so turn everything back on
    

    更多详情请见PHP_CodeSniffer's documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-06
      • 1970-01-01
      • 1970-01-01
      • 2021-05-21
      • 1970-01-01
      • 2013-01-28
      • 2015-08-19
      • 2021-11-24
      相关资源
      最近更新 更多