【问题标题】:How to setup PHP CodeSniffer that extend WordPress coding standards + autofix errors in VSCode?如何设置扩展 WordPress 编码标准的 PHP CodeSniffer + VSCode 中的自动修复错误?
【发布时间】:2019-05-07 04:49:41
【问题描述】:

如何使用扩展 WordPress 编码标准的个人规则为项目设置 PHP CodeSniffer + 保存时自动修复 VSCode 中的错误?

我有installed CodeSniffer globally

composer global require "squizlabs/php_codesniffer=*"

WordPress coding standards are installed 在主题文件夹中(所以在项目中它们位于/wp-content/themes/bideja/wpcs

git clone -b master https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards.git wpcs

在我创建的主题文件夹中 phpcs.xml 应该继承 WordPress 标准,以便我可以进一步自定义它

<?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 终端中,我可以扫描页面以查找错误并修复它们

phpcs page.php
phpbcf page.php 

但是 VSCode 如何在保存时修复它们?我收到弹出错误:

phpcs: Referenced sniff "WordPress-VIP" does not exist

我应该在 User settings.json 中放置什么?

"phpcs.enable": true,
"phpcs.standard": "WordPress", // ?

我试过设置

phpcs --config-set installed_paths 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

CodeSniffer 也可以自动缩进 PHP 和 HTML 还是超出它的能力范围?我正在为 VSCode 中的良好缩进而苦苦挣扎

【问题讨论】:

    标签: php wordpress visual-studio-code codesniffer


    【解决方案1】:

    我已经 git 将 WordPress 编码标准克隆到 C:/wamp64/www/tools/phpcs 中,并且我在 C:/wamp64/www/tools/phpcs/bideja 中创建了自定义 ruleset.xml

    <?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添加环境变量

     C:\Users\User\AppData\Roaming\Composer\vendor\bin
    

    并用正确的路径和标准名称修改User settings.json

    "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 终于修复了它可以解决的问题。

    【讨论】:

    • 另一种设置是在composer.json 脚本中使用phpcs ./path/to/code --standard=./path/to/ruleset.xml,而不是更改设置
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-21
    • 2021-12-02
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 2019-07-28
    • 2021-01-28
    相关资源
    最近更新 更多