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

PhpStorm自动格式化时为对象方法添加换行的配置咨询

Fixing Chained Method Call Formatting in PhpStorm

Got it, let's get that chained method formatting sorted in PhpStorm! Here's exactly how to set it up—both through the IDE's native settings and your .editorconfig file, since you mentioned using that:

1. Configure via PhpStorm Settings

This is the most straightforward way to preview and tweak the behavior:

  • Open PhpStorm Settings (press Ctrl+Alt+S on Windows/Linux, Cmd+, on Mac)
  • Navigate to Editor > Code Style > PHP
  • Switch to the Wrap and Braces tab
  • Scroll down to the Chained method calls section:
    • Select Wrap always to force every method in the chain onto a new line (exactly what you’re aiming for)
    • Check Align when multiline if you want the -> operators to line up vertically
  • Next, switch to the Spaces tab to add spaces around the -> operator (matching your example):
    • Under Within, find Method call/invocation
    • Check the boxes for Space before '->' and Space after '->'

2. Configure via .editorconfig

To sync this setting across your team or projects, add these lines to your .editorconfig file:

[*.php]
# Force chained methods to wrap onto new lines
php_chain_method_calls = always
# Add spaces around the arrow operator
php_space_before_arrow = true
php_space_after_arrow = true
# Optional: Align arrows vertically when wrapped
php_align_chain_method_calls = true

Test the Formatting

Select your original code:

function (object $object) { 
    $object->method1()->method2()->method3(); 
}

Press Ctrl+Alt+L to format, and you’ll get the desired result:

function (object $object) { 
    $object ->method1()
            ->method2()
            ->method3(); 
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 20:33:03