【发布时间】:2018-07-19 17:48:18
【问题描述】:
我们如何在 Windows 上自定义 Wordpress coding standards 并将它们与 VSCode 一起使用? 我正在尝试在全球范围内这样做,所以我不必为每个项目都这样做(我认为目前这不是一个坏主意?)但我想同样的事情可以应用于本地项目,只有路径应该改变。
首先,我已将它们安装在 C:\wamp64\www\_standards\wpcs 中,并使用以下方法设置了正确的路径:
phpcs --config-set installed_paths C:\wamp64\www\_standards\wpcs
现在当我执行phpcs -i 时,我可以看到已安装 WordPress 标准
The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs, WordPress-Extra and WordPress-VIP
在 VSCode 用户设置中我做了
"phpcs.enable": true,
"phpcs.standard": "WordPress"
到目前为止,一切似乎都运行良好。现在,我如何自定义它们并禁用一些东西,比如Spaces must be used to indent lines; tabs are not allowed?
里面写了about using custom rules和here is their sample file,所以我创建了C:\wamp64\www\_standards\phpcs.xml文件
<?xml version="1.0"?>
<ruleset name="Custom">
<!-- Enable colors in report -->
<arg name="colors"/>
<!-- Add source codes in the report -->
<arg value="s"/>
<!-- Load WordPress Coding standards -->
<rule ref="WordPress"/>
<!-- Customize -->
<rule ref="WordPress">
<exclude name="Generic.WhiteSpace.DisallowSpaceIndent.SpacesUsed" />
</rule>
</ruleset>
这样在我做phpcs -i 之后什么都没有出现(甚至没有出现 WordPress 标准)
phpcs --config-set installed_paths C:\wamp64\www\_posao\_standards\wpcs,C:\wamp64\www\_posao\_standards
但是,如果我将 phpcs.xml 重命名为 ruleset.xml 并重复之前安装路径的代码,我会得到这个
The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs, WordPress-Extra, WordPress-VIP and _standards
所以我在 VSCode 中更改了用户设置
phpcs.standard": "_standards"
现在,如何将 _standards 重命名为更好的名称,如何首先加载 WordPress 标准并用自定义规则覆盖它?因为文件的内容(即使已加载)也不会被使用 - 当我手动调用 phpcs page.php 时,我仍然会得到 Spaces must be used to indent lines; tabs are not allowed。
编辑:因此,由于我在项目之外安装了 WPCS,并且 config-set 已经指向该文件夹,因此我只在 Wordpress 主题中放置了 phpcs.xml 文件,现在我可以手动调用 phpcs page.php - 它工作得很好。但是现在的问题是我不能在VSCode中设置它保存文件后自动运行?
【问题讨论】:
-
请注意,您不需要像以前那样安装 XML 文件,因为您没有编写任何自定义嗅探类。您可以只使用文件的完整路径作为“标准”参数。例如,
phpcs --standard=/path/to/ruleset.xml ...。希望您可以将该路径作为standard输入到VSCode 中,它也可以正常工作。
标签: php wordpress visual-studio-code codesniffer