【问题标题】:Does PHP CodeSniffer support rule-specific configuration like Rubcop?PHP CodeSniffer 是否支持 Rubcop 等特定于规则的配置?
【发布时间】:2018-09-25 07:41:59
【问题描述】:

使用 Rubocop,我可以覆盖 .rubocop.yml 文件中的规则,例如 this

AllCops:
  RunRailsCops: true

# Commonly used screens these days easily fit more than 80 characters.
Metrics/LineLength:
  Max: 120

# Too short methods lead to extraction of single-use methods, which can make
# the code easier to read (by naming things), but can also clutter the class
Metrics/MethodLength: 
  Max: 20

我相信 CodeSniffer 支持这样的东西,但是查看他们的 wiki 页面,我找不到任何关于规则的信息:

而且默认配置文件似乎不支持它:

【问题讨论】:

    标签: codesniffer


    【解决方案1】:

    是的,PHP_CodeSniffer 允许您使用 ruleset.xml 文件定义自己的编码标准。此处提供了带注释的:https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-Ruleset

    您还可以将文件命名为 phpcs.xml 以让 PHPCS 自动拾取它,并且不再需要在每次运行时使用 --standard=/path/to/ruleset.xml。相关文档在这里:https://github.com/squizlabs/PHP_CodeSniffer/wiki/Advanced-Usage#using-a-default-configuration-file

    许多嗅探器具有允许您更改其行为的属性。您可以在此处查看列表:https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties

    一个很好的例子可能是行长之一:https://github.com/squizlabs/PHP_CodeSniffer/wiki/Customisable-Sniff-Properties#genericfileslinelength

    您可能还想查看 PHPCS 用作完整示例的 phpcs.xml 文件:https://github.com/squizlabs/PHP_CodeSniffer/blob/master/phpcs.xml.dist

    如何完全构建文件可能超出了这个问题的范围,但您应该能够搜索构建 PHPCS 规则集以获取更多信息。如果没有,请在此处提出更多问题:)

    【讨论】:

      猜你喜欢
      • 2017-02-06
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 2017-01-02
      • 2014-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多