您能否使用Visual Code的Beautify扩展程序在保存时同时格式化PHP代码?如果可以,该如何设置?如果不行,有哪个扩展程序可以实现此功能?
您能否使用Visual Code的Beautify扩展程序在保存时同时格式化PHP代码?如果可以,该如何设置?如果不行,有哪个扩展程序可以实现此功能?
尝试使用由Ben Mewburn开发的插件PHP Intelephense。正如手册所说:
无损PSR-12兼容文档/范围格式化。也可以格式化组合的HTML / PHP / JS / CSS文件。
vscode-php-formatter
扩展程序,该程序可以在需要时(通过按键绑定)或保存时格式化您的PHP代码。.php_cs
文件,并将其添加到参数设置中。phpformatter.arguments: ["--custom-config=/path/to/file/config.php_cs"]
并创建一个带有您自定义规则的文件:
<?php
$finder = Symfony\Component\Finder\Finder::create()
->files()
->in(__DIR__)
->exclude('vendor')
->exclude('resources/views')
->exclude('storage')
->exclude('public')
->notName("*.txt")
->ignoreDotFiles(true)
->ignoreVCS(true);
$fixers = [
'-psr0',
'-php_closing_tag',
'blankline_after_open_tag',
// more custom rules
];
return Symfony\CS\Config\Config::create()
->level(Symfony\CS\FixerInterface::PSR2_LEVEL)
->fixers($fixers)
->finder($finder)
->setUsingCache(true);
大写的 '[PHP]' 对我不起作用。然而小写的可以。像这样:
"[php]": {
"editor.defaultFormatter": "bmewburn.vscode-intelephense-client"
}