如何在 VSCode 中设置扩展 WordPress 编码标准的 PHP CodeSniffer + 自动修复错误? [英] How to setup PHP CodeSniffer that extend WordPress coding standards + autofix errors in VSCode?
问题描述
如何使用个人规则为项目设置 PHP CodeSniffer,扩展 WordPress 编码标准 + 保存时自动修复 VSCode 中的错误?
How to setup PHP CodeSniffer for project with personal rules that extend WordPress coding standards + autofix errors in VSCode on save?
composer global require "squizlabs/php_codesniffer=*"
WordPress 编码标准安装在主题文件夹内(所以里面他们位于 /wp-content/themes/bideja/wpcs
)
WordPress coding standards are installed inside theme folder (so inside project they are at /wp-content/themes/bideja/wpcs
)
git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs
在主题文件夹内我创建了 phpcs.xml
它应该继承 WordPress 标准,以便我可以进一步自定义它
Inside theme folder I have created phpcs.xml
which should inherit WordPress standards so I can customize it further
<?xml version="1.0"?>
<ruleset name="Bideja">
<description>Sniffs</description>
<config name="installed_paths" value="wpcs" />
<arg value="s"/>
<exclude-pattern>assets/*</exclude-pattern>
<exclude-pattern>node_modules/*</exclude-pattern>
<exclude-pattern>vendor/*</exclude-pattern>
<rule ref="WordPress-VIP">
<exclude name="Generic.Files.EndFileNewline.NotFound" />
<exclude name="WordPress.PHP.YodaConditions.NotYoda" />
</rule>
</ruleset>
在主题内的 Bash 终端中,我可以扫描页面以查找错误并修复它们
In Bash terminal inside theme, I can scan pages for errors and fix them
phpcs page.php
phpbcf page.php
但是 VSCode 如何在保存时修复它们?我收到弹出错误:
But how can VSCode fix them on save? I get popup error:
phpcs: Referenced sniff "WordPress-VIP" does not exist
我应该在 User settings.json 中放置什么?
What should I place in User settings.json?
"phpcs.enable": true,
"phpcs.standard": "WordPress", // ?
我试过设置
phpcs --config-set installed_paths wpcs
当我使用 phpcs -i
检查已安装的标准时,我可以看到列出了 WordPress 标准
When I check installed standards with phpcs -i
I can see that WordPress standards are listed
The installed coding standards are MySource, PEAR, PSR1, PSR12, PSR2, Squiz, Zend, WordPress, WordPress-Core, WordPress-Docs, WordPress-Extra and WordPress-VIP
CodeSniffer 是否还能够自动缩进 PHP 和 HTML,还是超出了它的能力范围?我在 VSCode 中挣扎于良好的缩进
Also is CodeSniffer able to auto indent PHP and HTML or is that beyond of its capabilites? I am struggling with good indentation in VSCode
推荐答案
我已经 git 将 WordPress 编码标准克隆到 C:/wamp64/www/tools/phpcs
并且我已经创建了自定义 C:/wamp64/www/tools/phpcs/bideja
中的规则集.xml
I have git cloned WordPress coding standards into C:/wamp64/www/tools/phpcs
and I have created custom ruleset.xml
in C:/wamp64/www/tools/phpcs/bideja
<?xml version="1.0"?>
<ruleset name="Bideja">
<arg value="s"/>
<exclude-pattern>assets/*</exclude-pattern>
<exclude-pattern>node_modules/*</exclude-pattern>
<exclude-pattern>vendor/*</exclude-pattern>
<rule ref="WordPress">
<exclude name="WordPress.PHP.YodaConditions.NotYoda" />
<!-- exclude other stuff as you wish -->
</rule>
</ruleset>
安装新标准
phpcs --config-set installed_paths C:/wamp64/www/tools/phpcs/wp-coding-standards/wpcs,C:/wamp64/www/tools/phpcs/bideja
检查是否安装了标准
phpcs -i
设置默认标准
phpcs --config-set default_standard bideja
安装 phpcbf
扩展,根据你的文件夹路径在 Windows 中为 Path 添加环境变量
Install phpcbf
extension, add Environment variable for Path in Windows depending on your folder path
C:\Users\User\AppData\Roaming\Composer\vendor\bin
并使用标准的正确路径和名称修改User settings.json
and modify User settings.json
with correct path and name of your standard
"phpcbf.executablePath": "C:\\Users\\User\\AppData\\Roaming\\Composer\\vendor\\bin\\phpcbf.bat",
"phpcbf.executablePathWindows": "C:\\Users\\User\\AppData\\Roaming\\Composer\\vendor\\bin\\phpcbf.bat",
"phpcs.standard": "bideja",
"phpcbf.standard": "bideja",
"phpcs.enable": true,
"phpcbf.onsave": true,
我希望我没有忘记一些东西,因为我在这个问题上搞砸了几个小时,但现在终于在保存文件后 VSCode 修复了它所能做的.
I hope I didn't forget something as I messed around this for hours but finally now after saving file VSCode fixes what it can.
这篇关于如何在 VSCode 中设置扩展 WordPress 编码标准的 PHP CodeSniffer + 自动修复错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!