PHPCS嗅探器名称似乎与PHPCBF修复不匹配


PHPCS sniff names don't seem to match to PHPCBF fixes

我在phpcs.xml中定义了一些嗅探器,如下所示:

<rule ref="PSR2">
    <exclude name="Generic.ControlStructures.InlineControlStructure.NotAllowed"/>
</rule>

我的意图是以粒度的方式逐渐收紧CS规则(有更多的排除)。

但是,当我尝试运行CBF时,使用相同的嗅探器名称失败了:

$ ./vendor/bin/phpcbf --standard=PEAR --sniffs=Generic.Sniffs.ControlStructures.InlineControlStructure.NotAllowed path/to/code
生产:

错误:指定的嗅探代码"Generic.Sniffs.ControlStructures.InlineControlStructure"。"notalallowed"无效

如何是无效的,如果它适用于phpcs?我似乎在文档中找不到映射或命名约定映射。

(我已经尝试过"PEAR","PSR2"answers"Generic"作为标准,我也尝试过它就像Generic. controlstructures . inlinecontrolstructure . notalallowed)

当您运行PHPCBF时,请使用与PHPCS完全相同的参数运行它。PHPCBF将正常读取您的ruleset.xml文件,并处理其中找到的所有规则。任何被排除在错误报告之外的内容也将被排除在修复之外。如果您设置了任何配置变量,它也将读取这些。PHPCBF和PHPCS共享相同的代码库。

您得到的特定错误是Generic.Sniffs.ControlStructures.InlineControlStructure.NotAllowed不是嗅探名称。这是一个完整的错误代码。不能在命令行中排除特定的错误代码。如果您使用Generic.Sniffs.ControlStructures.InlineControlStructure,它将工作,但它也会排除嗅探中的所有错误,而不仅仅是您提供的特定消息。

因此,最好的办法是为PHPCS创建自定义编码标准,并确保报告您想要修复的所有错误,并且隐藏您想要排除的错误。当您对结果感到满意时,只需将phpcs命令更改为phpcbf并让它运行。

如果您发现自己处于需要不同规则进行检查和修复的情况,您可以标记ruleset.xml文件,以告诉PHPCS和PHPCBF执行哪些规则:https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml#selectively-applying-rules

确保标准由:

phpcs -i

否则加:

phpcs --config-set installed_paths path/to/coder_sniffer/standards

注册后,您可以列出所有嗅探:

phpcs -e --standard=PEAR

在我的phpcs中,这个嗅探被称为:Generic.ControlStructures.InlineControlStructure

$ phpcs -e --standard=PEAR | grep ControlStructures.InlineControlStructure
  Generic.ControlStructures.InlineControlStructure