VSCode中使用Intelephense格式化时如何避免缩进丢失?
问题描述
我的原始PHP代码如下:
class UpdateInternalCompanyNamesFromCompanyShortNames extends Command { protected $signature = 'update:internal-company-names-from-company-short-names'; protected $description = 'Copy company short names to internal company names (company_erp_name) from and to local database for all ' . 'legal persons.'; }
经过格式化后变成了这样:
class UpdateInternalCompanyNamesFromCompanyShortNames extends Command { protected $signature = 'update:internal-company-names-from-company-short-names'; protected $description = 'Copy company short names to internal company names (company_erp_name) from and to local database for all ' . 'legal persons.'; }
可以看到$signature变量等号后的换行缩进丢失,导致无法正常使用格式化工具,而$description的第二行却仍保留缩进,这一不一致的情况更让人困扰。我已检查phpfmt的UI和JSON设置,也进行了谷歌搜索,但均未解决问题。请问如何使用Intelephense解决这个缩进不一致的问题?
解决方案
统一Intelephense格式化配置
在编辑器(如VS Code)的设置中,找到Intelephense的格式化相关选项:- 调整
intelephense.format.indentSize为项目使用的缩进值(比如4),确保intelephense.format.useTabs与项目缩进规则一致 - 开启
intelephense.format.braces相关的对齐配置,保证多行赋值语句的后续行保持统一缩进
- 调整
禁用phpfmt,仅用Intelephense处理格式化
同时启用phpfmt和Intelephense会导致规则冲突,先在插件管理中禁用phpfmt,再将编辑器默认格式化工具设置为Intelephense,避免双重格式化带来的问题通过.editorconfig统一项目规则(进阶)
在项目根目录创建.editorconfig文件,写入统一的缩进规则,Intelephense会优先读取该配置:[*.php] indent_style = space indent_size = 4 trim_trailing_whitespace = true insert_final_newline = true
内容的提问来源于stack exchange,提问作者s3c
相关产品推荐
相关产品推荐

