IntelliJ IDEA 2021.3 Help

PHP code refactoring

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 of the Settings/Preferences dialog (Ctrl+Alt+S).

    • 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 property. To do that, use the Create and initialize class properties checkbox:

      • When this checkbox is selected, the newly added parameter is initialized as a field. IntelliJ IDEA creates a field with the same name as this parameter and adds a line with the following assignment:

        $this->parameterName = $parameterName;
      • When the checkbox is cleared, a parameter is added without initialization.

      • In this example, we invoke the Change signature refactoring on the __construct() method and add a new $q parameter. The result depends on whether the Create and initialize class properties checkbox is selected or not. The new field is created with the default visibility modifier, which is set on the Code Generation tab of the Code Style. PHP page of the Settings/Preferences dialog (Ctrl+Alt+S).

        Before

        After

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

        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 Propagate parameters.

  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.

Before

After

// 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: 01 August 2022