IntelliJ IDEA 2018.2 Help

Refactoring PHP

Refactoring means updating the structure of the source code without changing the behaviour of the application. Refactoring helps you keep your code solid, dry, and easy to maintain.

Change Signature

  1. In the editor, place the cursor within the name of the method whose signature you want to change.
  2. Press Ctrl+F6. Alternatively, choose Refactor | Change Signature on the main menu or context menu.
  3. In the Change Signature dialog, make the necessary changes to the method signature and specify what other, related, changes are required.
    • For example, change the method name. To do that, edit the text in the Name field. You can change the return type by editing the contents of the Return type field. Setting the method return type is only possible in PHP language version 7.1 and later. You can specify the PHP language level on the PHP page (File | Settings | Languages and Frameworks | PHP for Windows and Linux or IntelliJ IDEA | Preferences | Languages and Frameworks | PHP for macOS).
    • Manage the method parameters using the table and the buttons in the Parameters area. When adding parameters, you may want to propagate these parameters to the method that call the current method.
    • In the PHP context, when the Change signature refactoring is invoked from the constructor of a class, the new parameter can be initialized as a class field. To do that, use the Create and initialize class properties check box: When this checkbox is selected, the newly added parameter is initialized as a field. IntelliJ IDEA creates a protected field with the same name as this parameter and adds a line with the following assignment:
      $this-><parameter_name> = $<parameter_name>; When the checkbox is cleared, a parameter is added without initialization.

      For example, you have the following constructor:

      class ChangeSignatureNewParam { function __construct() { $a = "Constructor in ChangeSignatureNewParam"; print $a; } }
      If you invoke the Change signature refactoring from the __construct() method and add a new $q parameter, the result will depend on whether you select or clear the Create and initialize class properties checkbox:

      • The Create and initialize class properties checkbox is selected:

        class ChangeSignatureNewParam { private $q; function __construct($q) { $a = "Constructor in ChangeSignatureNewParam"; print $a; $this->q = $q; } }
      • The Create and initialize class properties checkbox is cleared:

        class ChangeSignatureNewParam { function __construct($q) { $a = "Constructor in ChangeSignatureNewParam"; print $a; } }
    • You can delete or reorder the added parameters. To change the name or the default value of a parameter, make the necessary updates in the table of parameters (in the fields Name and Default value respectively).
    • You can propagate new method parameters (if any) along the hierarchy of the method that call the current method. Click the Propagate Parameters button propagateParameters.
  4. To perform the refactoring right away, click Refactor. To see the expected changes and make the necessary adjustments prior to actually performing the refactoring, click Preview.

Examples

The following table shows 3 different ways of performing the same Change Signature refactoring.

In all the cases, the result() function is renamed to generateResult() and a new $b parameter is added to this function.

The examples show how the function call, the calling function (showResult()) and other code fragments may be affected depending on the refactoring settings.

BeforeAfter
// 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); }
Last modified: 14 June 2018