【问题标题】:PHPcs xml config disable some rulesPHPcs xml配置禁用一些规则
【发布时间】:2018-11-29 09:25:25
【问题描述】:

我了解如何使用 xml 配置 phpcs,但找不到如何禁用某些嗅探。这是我目前的conf(甚至不知道这是否正确):

<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="custom" 
  xmlns="http://pmd.sf.net/ruleset/1.0.0" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://pmd.sf.net/ruleset/1.0.0 http://pmd.sf.net/ruleset_xml_schema.xsd" xsi:noNamespaceSchemaLocation="http://pmd.sf.net/ruleset_xml_schema.xsd">
  <rule ref="rulesets/codesize.xml"/>
  <rule ref="rulesets/controversial.xml/Superglobals"/>
  <rule ref="rulesets/controversial.xml/CamelCaseParameterName"/>
  <rule ref="rulesets/controversial.xml/CamelCaseVariableName"/>
  <rule ref="rulesets/design.xml"/>
  <rule ref="rulesets/naming.xml/ShortMethodName"/>
  <rule ref="rulesets/naming.xml/ConstructorWithNameAsEnclosingClass"/>
  <rule ref="rulesets/naming.xml/ConstantNamingConventions"/>
  <rule ref="rulesets/naming.xml/BooleanGetMethodName">
    <properties>
      <property name="checkParameterizedMethods" value="true"/>
    </properties>
  </rule>
  <rule ref="rulesets/unusedcode.xml"/>

  <arg name="tab-width" value="2"/>
  <rule ref="Generic.WhiteSpace.ScopeIndent">
    <properties>
      <property name="indent" value="2"/>
    </properties>
  </rule>

  <rule ref="Generic.Files.LineLength">
    <properties>
      <property name="lineLimit" value="140"/>
      <property name="absoluteLineLimit" value="0"/>
    </properties>
  </rule>
</ruleset>

我想禁用这些:

  • [phpcs] @copyright tag must contain a year and the name of the copyright holder所有文档评论嗅探(我不需要它们)
  • [phpcs] PHP version not specified
  • [phpcs] There must be exactly one blank line before the tags in a doc comment
  • [phpcs] The open comment tag must be the only content on the line
  • [phpcs] Missing short description in doc comment
  • [phpcs] The close comment tag must be the only content on the line
  • [phpcs] Line indented incorrectly; expected at least 4 spaces, found 2我用了两个空格!
  • [phpcs] Missing file doc comment
  • [phpcs] Line exceeds 85 characters; contains 91 characters我希望最多140个

如果我想添加更多,我可以在哪里搜索可能的配置?

【问题讨论】:

标签: php phpcodesniffer phpcs


【解决方案1】:

我建议你找到最相似的规则集,并制作一个不包含指定嗅探的副本。

Annotated ruleset.xml 示例包含一个从规则中排除某些嗅探的块,以及自定义规则集的使用。

 <!--
    Include all sniffs in the Squiz standard except one. Note that
    the name of the sniff being excluded is the code that the sniff
    is given by PHP_CodeSniffer and is based on the file name and
    path of the sniff class. You can display these codes using the
    -s command line argument when checking a file.
 -->
 <rule ref="Squiz">
  <exclude name="Squiz.PHP.CommentedOutCode"/>
 </rule>

这里的大部分工作是找出每个嗅探是如何被包含的,如果它没有在其他任何地方列出,可以通过 GitHub 搜索消息 (example) 找到。

【讨论】:

    【解决方案2】:

    谢谢大家,我找到了办法:http://edorian.github.io/php-coding-standard-generator/#phpcs

    之前试过了,没用,发现要点击phpcs(第一时间看是选中的)。

    【讨论】:

      【解决方案3】:

      忽略规则的另一种方法是使用--exclude 标志。

      vendor/bin/phpcs --standard=PSR2  --exclude=Generic.Files.LineLength,Generic.WhiteSpace.ScopeIndent app/
      

      为了找到要排除的规则名称,请在以下目录中找到您对应的规则集:

      vendor/squizlabs/php_codesniffer/src/Standards/&lt;coding standard&gt;/ruleset.xml

      规则名称将在 ref 节点中:

       <rule ref="Generic.Files.LineLength">
              <properties>
                  <property name="lineLimit" value="120"/>
                  <property name="absoluteLineLimit" value="0"/>
              </properties>
       </rule>
      

      这比创建单独的规则集更快、更简单。

      【讨论】:

        猜你喜欢
        • 2014-10-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多