You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让PhpStorm 2023格式化代码时不拆分短数组为多行?

在PhpStorm 2023中调整数组格式化规则,避免120字符边距内拆分数组

问题场景

执行「Reformat Code」操作时,原本紧凑排列的数组代码:

protected $fillable
    = ['branch_id', 'user_id', 'first_name', 'last_name',  'department_id', 'status', 'job_start','job_end',
        'phones', 'website', 'notes', 'last_login', 'gender', 'birthday', 'salary', 'country', 'state_id',
        'region_id', 'subregion_id', 'city_id', 'street', 'postal_code' ];

会被自动格式化为每个元素单独一行的形式。需要设置让编辑器在120字符边距内不拆分该数组为多行。

解决方案

按以下步骤调整PhpStorm的格式化规则:

  • 打开设置窗口:使用快捷键 Ctrl+Alt+S(Windows/Linux)或 Cmd+,(Mac),或通过菜单栏「File」→「Settings」(Windows/Linux)/「PhpStorm」→「Settings」(Mac)进入。
  • 导航到PHP代码样式设置:依次展开「Editor」→「Code Style」→「PHP」,切换到「Wrapping and Braces」标签页。
  • 调整数组初始化器的换行规则:
    1. 找到「Array initializer」选项组,取消勾选「Wrap when typing reaches right margin」。
    2. 切换到「Code Style」→「General」标签页,确认「Right margin (columns)」已设置为120。
    3. 若需要更严格的紧凑格式,可将「Wrap array initializer」的选项改为「Do not wrap」。
  • 保存设置:点击「Apply」→「OK」,之后执行「Reformat Code」时,数组会在120字符范围内保持紧凑排列,不会强制拆分每行一个元素。

内容的提问来源于stack exchange,提问作者mstdmstd

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 04:57:07