我们可以按照 WordPress 编码标准在 VS Code 中设置 Intelephense 吗?

Dib*_*ash 8 php wordpress visual-studio-code phpcs intelephense

我在 VS Code 中使用 PHPCS 和 PHPCBF 进行 WordPress 开发。
(按照https://github.com/tommcfarlin/phpcs-wpcs-vscode中提到的说明)

虽然 PHPCBF 按照 WPCS 格式化代码,但我仍然觉得代码看起来非常丑陋,尤其是当 PHP 文档中存在 Html 标签时。

当我使用 Intelephense 格式化 Php 代码时,代码看起来赏心悦目,包括正确缩进的 Html 标签。使用 Intelephense 的代价是代码不再符合 WPCS。有一个选项可以将 WordPress 作为存根包含在 Intelephense 中,但这仅用于包含 WordPress 特定功能,与格式无关。

使用 Intelephense 格式化时遇到的一些问题是:

  1. 左括号之后和右括号之前没有空格,(phpcs)
  2. 文件注释前不能有空行(phpcs)
<?php
// this is a blank line inserted by Intelephense which is causing error no.2 provided in summary above
/**
 * This is a sample file
 *
 * @package something.
 * something something
 * something something
 */

get_header();
if (have_posts()) {
    while (have_posts()) {
// No space is inserted by Intelephense After opening and before Closing parenthesis which is causing error no.1 provided in summary above
        the_post(); ?>
        <h2>
            <a href="<?php the_permalink(); ?>"><?php the_title(); ?> </a>
        </h2>
        <?php the_content(); ?>
        <hr>
<?php
    }
}
get_footer();
?>
Run Code Online (Sandbox Code Playgroud)

zol*_*ndi 0

这个问题的结论是“虽然这个扩展不使用 phpcbf”,所以我不确定如何专门使用 Intelephense for VS Code 来做到这一点。

但是,如果您正在寻找使用 phpcbf 作为 VS Code 中 php 文件的格式化程序的解决方案,并且您的 phpcbf 已设置并且安装了所有编码标准,则可以使用Per Soderlind 的 phpcbf

然后,如果您重新加载编辑器并尝试使用(或您喜欢的方法)格式化 php 文件,Ctrl+Shift+I则会出现一个弹出窗口,您必须在其中选择您喜欢的格式化程序。

在此输入图像描述

那么你应该选择那个说valeryanm.vscode-phpsab

在此输入图像描述

或者,如果弹出窗口没有出现,只需将以下内容添加到您的settings.json

"[php]": {
        "editor.defaultFormatter": "valeryanm.vscode-phpsab"
    },
Run Code Online (Sandbox Code Playgroud)

现在,每当您尝试格式化它时,phpcbf 都会在整个文档上运行。