自定义 phpcs 规则
在旧项目中利用 phpcs 使用自定义规则格式化代码
旧项目中很可能没有命名空间。但是希望能够通过 phpcs 来检测代码规范,又不希望抛出11 | ERROR | Each class must be in a namespace of at least one level (a top-level vendor name)
这个顶级命名空间错误。
查阅某个文档 与 这篇文章 看到了希望,可以自定义规则,然后忽略特定规则。
研究一段时间源码后,在 这里 找到了我要忽略的规则 「MissingNamespace」
最后得到了既能按照 PSR2 规范,又可以忽略特定规则的配置:
~/code_standards/CustomStandard/ruleset.xml
<?xml version="1.0"?>
<ruleset name="CustomStandard">
<rule ref="PSR2">
<exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace"/>
</rule>
</ruleset>
最后你可以选择将自定义规则与你的 ide 、sublime、atom 等结合起来,就可以做到保存就自动检测并格式化代码了。
本作品采用《CC 协议》,转载必须注明作者和本文链接
如果是laravel项目,可以只排除指定目录。