我在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进行格式化时遇到的一些问题:
- 左括号之后和右括号之前没有空格 (phpcs)
- 文件注释之前不能有空行 (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();
?>