簡體   English   中英

PhpStorm 多行 if() 語句自動格式化

[英]PhpStorm multi line if() statement auto format

假設我們在項目中使用 PHP 並在 80 處進行硬包裝。

我要這個:

if ($text === 'testTestTestTestTestTestTestTestTest' || $text === 'testTestTestTestTestTestTestTestTest' || $text === 'testTestTestTestTestTestTestTestTest') {
    $text = 'test';
}

變成這樣:

if ($text === 'testTestTestTestTestTestTestTestTest' 
    || $text === 'testTestTestTestTestTestTestTestTest'
    || $text === 'testTestTestTestTestTestTestTestTest')
{
    $text = 'test';
}

有什么方法可以配置 PhpStorm,以便在按下 Ctrl + Alt + L時執行此格式化?

我在Settings - Editor - Code Style - PHP - Wrapping and Braces - 'if()' statement中嘗試了一些選項,但沒有幫助。

好像我做錯了什么。 請幫忙。

首先,在 PSR-12 中有一條關於如何格式化這種類型的長表達式的聲明。 在我們的例子中,它應該是這樣的:

if (
    $text === 'testTestTestTestTestTestTestTest'
    || $text2 === 'testTestTestTestTestTestTestTestTest'
    || $text3 === 'testTestTestTestTestTestTest'
) {
    $text = 'test';
}

要使 PhpStorm 像這樣格式化它:

  1. Go 到File | Settings | Editor | Code Style | PHP File | Settings | Editor | Code Style | PHP
  2. 在右側點擊"Set from..." -> PSR-12
  3. Go 到Wrapping and Braces選項卡
  4. 設置Binary expressions = Wrap if long and checkmark to Operation sign on next line
  5. 'if()' statement部分的同一選項卡上,檢查New line before first elementNew line before last element

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM