首页 > 解决方案 > 我们可以按照 WordPress 编码标准在 VS Code 中设置 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 特定功能,与格式无关。

使用 Intelephene 进行格式化时面临的一些问题是:

  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();
?>

标签: phpwordpressvisual-studio-codephpcsintelephense

解决方案


这个问题以“这个扩展虽然不使用 phpcbf”的结论结束,所以我不确定你如何专门使用 Intelephense for VS Code 来完成它。

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

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

在此处输入图像描述

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

在此处输入图像描述

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

"[php]": {
        "editor.defaultFormatter": "valeryanm.vscode-phpsab"
    },

现在,无论何时您尝试对其进行格式化,phpcbf 都会在整个文档上运行。


推荐阅读