IntelliJ IDEA:重構(gòu)PHP

2019-03-20 11:56 更新

重構(gòu)PHP

重構(gòu)意味著更新源代碼的結(jié)構(gòu)而不改變應(yīng)用程序的行為。重構(gòu)可幫助您保持代碼的穩(wěn)定,簡潔和易于維護。

更改簽名

  1. 在編輯器中,將光標放在要更改其簽名的方法的名稱中。

  2. 按下Ctrl+F6?;蛘?,在主菜單或上下文菜單上選擇:重構(gòu)|更改簽名。

  3. 在“更改簽名”對話框中,對方法簽名進行必要的更改,并指定所需的其他相關(guān)更改。
    • 例如,更改方法名稱。為此,請在“名稱” 字段中編輯文本 。您可以通過編輯“返回類型”字段的內(nèi)容來更改返回類型。只能在PHP語言版本7.1及更高版本中設(shè)置方法返回類型。您可以在“設(shè)置/首選項”對話框(Ctrl+Alt+S)的PHP頁面上指定PHP語言級別。 

    • 使用“參數(shù)”區(qū)域中的表和按鈕管理方法參數(shù)。添加參數(shù)時,您可能希望將這些參數(shù)傳播到調(diào)用當前方法的方法。

    • 在PHP上下文中,當從類的構(gòu)造函數(shù)調(diào)用更改簽名重構(gòu)時 ,可以將新參數(shù)初始化為類字段。為此,請使用“創(chuàng)建并初始化類屬性”復(fù)選框:選中此復(fù)選框后,新添加的參數(shù)將初始化為字段。IntelliJ IDEA創(chuàng)建一個與此參數(shù)同名的字段,并添加一行,其中包含以下賦值:
      $this-><parameter_name> = $<parameter_name>; 清除該復(fù)選框后,將添加一個參數(shù)而不進行初始化。

      在此示例中,我們在__construct()方法上調(diào)用“更改簽名重構(gòu)”,并添加一個新的$q參數(shù)。結(jié)果取決于是否選中了“創(chuàng)建和初始化類屬性”復(fù)選框。使用默認可見性修飾符創(chuàng)建新字段,該修飾符在代碼樣式的“代碼生成”選項卡上設(shè)置 ?!霸O(shè)置/首選項”對話框(Ctrl+Alt+S)的PHP頁面。

      之前

      之后

      class ChangeSignatureNewParam {
          function __construct() {
              $a = "Constructor in ChangeSignatureNewParam";
              print $a;
          }
      }

      選中“創(chuàng)建和初始化類屬性”復(fù)選框:

      class ChangeSignatureNewParam {
          private $q;
          function __construct($q) {
              $a = "Constructor in ChangeSignatureNewParam";
              print $a;
              $this->q = $q;
          }
      }

      清除“創(chuàng)建和初始化類屬性”復(fù)選框:

      class ChangeSignatureNewParam {
          function __construct($q) {
              $a = "Constructor in ChangeSignatureNewParam";
              print $a;
          }
      }
    • 您可以刪除或重新排序添加的參數(shù)。要更改參數(shù)的名稱或默認值,請在參數(shù)表中進行必要的更新(分別在“名稱”和“默認值”字段中 )。

    • 您可以沿調(diào)用當前方法的方法的層次結(jié)構(gòu)傳播新方法參數(shù)(如果有)。單擊“傳播參數(shù)”按鈕( propagateParameters)。

  4. 要立即執(zhí)行重構(gòu),請單擊“重構(gòu)”。要在實際執(zhí)行重構(gòu)之前查看預(yù)期的更改并進行必要的調(diào)整,請單擊“預(yù)覽”。

示例

下表顯示了執(zhí)行相同“更改簽名”重構(gòu)的3種不同方法 。

在所有情況下,result()函數(shù)都重命名為generateResult(),并且向此函數(shù)添加了一個新的$b參數(shù)。

這些示例顯示了函數(shù)調(diào)用,調(diào)用函數(shù)(showResult())和其他代碼片段如何受到影響,具體取決于重構(gòu)設(shè)置。

之前

之后

// This is the function whose signature will be changed:
function result($a) {
// some code here
}

function showResult($a) {

// Here is the function call:
$this->result($a);
}

// Now we'll rename the function and
// add one parameter.
// The function has been renamed to generateResult.
// The new parameter $b has been added.

function generateResult($a,$b) {
// some code here
}

function showResult($a,$b) {

// The function call has been changed accordingly:
$this->generateResult($a,$b);
}
// This is the function whose signature will be changed:


function result($a) {
// some code here
}

function showResult($a) {

// Here is the function call:
$this->result($a);
}

// Now we'll rename the function and add one parameter.
//We will also specify the default value 'new_param'
//for this new parameter
// The function has been renamed to generateResult.
// The new parameter $b has been added.

function generateResult($a,$b) {
// some code here
}

function showResult($a) {

// The function call has been changed accordingly:
$this->generateResult($a,'new_param');
}

// When performing the refactoring, 'new_param' was specified as
// the default parameter value.
// This is the function whose signature will be changed:


function result($a) {
// some code here
}


function showResult($a) {

// Here is the function call:
$this->result($a);
}

// Now we'll rename the function and add one parameter.
//We will also propagate this new parameter
//through the showResult() calling function to the function call.
// The function has been renamed to generateResult.
// The new parameter $b has been added.

function generateResult($a,$b) {
// some code here
}

// Note the new function parameter:
function showResult($a,$b) {

// The function call has been changed accordingly:
$this->generateResult($a,$b);
}


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號