我们如何自定义 Wordpress 编码标准并在 VSCode 中使用它们?

How can we customize Wordpress coding standards and use them in VSCode?

我们如何在 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 and 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,所以我只在 Wordpress 主题中放置了 phpcs.xml 文件,现在我可以手动调用 phpcs page.php - 效果很好。但是现在的问题是我无法在VSCode中设置它以便在保存文件后自动运行?

几点建议:

  • phpcs.xml 文件(也可以是 phpcs.xml.dist 和其他具有首选顺序的类似变体)(不是 ruleset.xml),应该位于 project代码,不在你安装WPCS的目录下。
  • 然后 VS Code 配置将是 phpcs.xml 文件的路径(绝对或相对):

    "phpcs.standard": "/path/to/project/phpcs.xml"

    "phpcs.standard": "./phpcs.xml"

您仍然需要确保您引用的 phpcs 可执行文件已注册 WordPress 标准。

参考:https://marketplace.visualstudio.com/items?itemName=ikappas.phpcs

经过长时间的搜索和试用,我启用了 WordPress 编码标准 (WPCS)。这是我在 Windows 10 (Home) 机器上所做的。

Please note, I activated WPCS only in Visual Studio Code, and not in project scope using composer.json and ./vendor/

我遵循的步骤:

  1. 在 cmd 或 git bash 运行 composer global require "squizlabs/php_codesniffer=*"

安装路径为:

C:\Users\{YOUR USER}\AppData\Roaming\Composer\vendor\bin

  1. 安装 phpcs VSCode 扩展(作者 Ioannis Kappas)
  2. 在 VSCode 中,文件 » 首选项 » 设置 (ctrl + ,)
  3. 搜索"phpcs"
  4. 现在在Phpcs: Executable Path中输入:C:\Users\{YOUR USER}\AppData\Roaming\Composer\vendor\bin\phpcs(别忘了在尾部加上phpcs
  5. 在您计算机的某处,创建一个用于存储 WordPress 编码标准的目录(例如 coding-standards)(参考:tommcfarlin
  6. 目录内(例如coding-standards/)运行命令:composer create-project wp-coding-standards/wpcs:dev-master --no-dev
  7. 打开命令 window 和 运行:C:/Users/{YOUR USER}/AppData/Roaming/Composer/vendor/bin/phpcs --config-set installed_paths H:/coding-standards/wpcsH:/coding-standards/wpcs 将是您的路径,您在第 6 步中已经完成)

The installed_paths is updated (overridden) in C:\Users\{YOUR USER}\AppData\Roaming\Composer\vendor\squizlabs\php_codesniffer\CodeSniffer.conf. If your command in step 7 failed somehow, you can manually edit this file and add the path to the WPCS here.

  1. 在您的项目根目录中添加一个 phpcs.xmlphpcs.xml.dist,内容如下:
<?xml version="1.0"?>
<ruleset name="YOURPROJECT">
    <config name="minimum_supported_wp_version" value="4.4.0" />
    <!-- <config name="testVersion" value="0.6-"/> -->

    <rule ref="WordPress-Core" />
    <rule ref="WordPress-Docs" />
    <rule ref="WordPress-Extra" />

    <rule ref="WordPress.WP.I18n">
        <properties>
            <property name="text_domain" type="array" value="your-text-domain" />
        </properties>
    </rule>

    <!-- Check all PHP files in directory tree by default. -->
    <arg name="extensions" value="php"/>
    <file>.</file>

    <exclude-pattern>/docker/*</exclude-pattern>
    <exclude-pattern>/node_modules/*</exclude-pattern>
    <exclude-pattern>/tests/*</exclude-pattern>
    <exclude-pattern>/vendor/*</exclude-pattern>
</ruleset>

It's a template. Don't forget to update the minimum_supported_wp_version with your project's data. For a sample template, See phpcs.xml.dist.sample - WordPress Coding Standard

  1. 如有必要,请重新启动 VSCode,然后就完成了