如何配置PhpStorm实现指定格式的PHP多行if语句自动格式化?
PHP代码格式化配置(实现长if条件换行+大括号单独行)
针对你需要的格式化效果,按以下步骤配置JetBrains系列IDE(如PHPStorm):
设置硬换行长度
打开Settings → Editor → Code Style → PHP,切换到Wrapping and Braces标签页:- 找到
Hard wrap at,设置为80。
- 找到
调整if语句的换行与大括号规则
同样在Wrapping and Braces标签页,定位到'if()' statement区域:- 勾选
Wrap long lines(确保超过硬换行长度时自动触发换行); - 在
Conditions下拉菜单中选择Wrap if long(若需强制所有多条件换行,可选Wrap always); - 找到
Operator wrapping,选择Wrap before operator(让||这类逻辑运算符出现在新行开头,匹配你要的格式); - 在
Braces placement区域,将'if()' statement设置为Next line(让大括号单独占一行)。
- 勾选
配置二元表达式换行逻辑
在Wrapping and Braces标签页的Expressions区域,找到Binary expressions:- 选择
Wrap if long; - 同样设置
Operator wrapping为Wrap before operator,确保条件中的逻辑运算符换行位置符合预期。
- 选择
完成以上配置后,按下Ctrl + Alt + L格式化代码,就能得到目标效果:
格式化前代码:
if ($text === 'testTestTestTestTestTestTestTestTest' || $text === 'testTestTestTestTestTestTestTestTest' || $text === 'testTestTestTestTestTestTestTestTest') { $text = 'test'; }
格式化后代码:
if ($text === 'testTestTestTestTestTestTestTestTest' || $text === 'testTestTestTestTestTestTestTestTest' || $text === 'testTestTestTestTestTestTestTestTest') { $text = 'test'; }
内容的提问来源于stack exchange,提问作者LVX
相关产品推荐
相关产品推荐

