PhpStorm 2023.2 Help

Change signature

The Change Signature refactoring combines several different modifications that can be applied to a function signature. You can use this refactoring to:

  • change the function name and return type

  • add, remove, and reorder parameters

  • assign default values to the parameters

  • change parameter names and types

  • propagate new parameters through the function call hierarchy

When changing a function signature, PhpStorm searches for all usages of the function and updates all the calls, implementations, and override replacements of the function that can be safely modified to reflect the change.

The Change Signature refactoring is supported only for PHP and JavaScript (refer to Refactoring JavaScript).

Default value and propagation of new parameters

For each new parameter added to a function, you can specify a default value (or an expression) in the Default field.

You can also propagate the parameters you have introduced to the functions that call the function whose signature you are changing. The refactoring result depends on whether you specify the default value and whether you use propagation.

Propagation: New parameters can be propagated to any function that calls the function whose signature you are changing. In such case, generally, the signatures of the calling functions change accordingly. These changes, however, also depend on the default values set for the new parameters.

Default: Generally, this is the value to be added to the function calls. If the new parameter is not propagated to a calling function, the calls within such function will also use this value. If propagation is used, this value won't affect the function calls within the calling functions.

Changing a function signature

  1. Place the caret at the name of the function that you want to refactor.

  2. Press Control+F6. Alternatively, select Refactor | Change Signature from the main or context menu. The Change Signature dialog opens.

    the Change Signature dialog
  3. In the Change Signature dialog, make necessary changes to the function signature depending on your needs:

    • Change the function name. To change the name, edit the text in the Name field.

    • Change the function return type by editing the contents of the Return type field.

    • Manage the function parameters. To configure the parameters, use the table and buttons in the Parameters area:

      • To add a new parameter, click The Add button and specify the new parameter's properties in the corresponding table row. Press Control+Space to use code completion.

        • To provide the default value for the parameter, which will affect the function signature, use the Parameter field as follows:

          $parameterName = 'parameterValue';
        • To provide the default value for the argument, which will affect the function's calls, use the Default field as follows: argumentValue.

        When adding parameters, you may want to propagate them to the functions that call the current function.

        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. PhpStorm 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.

        The new properties are created with the default visibility modifier, which is set on the Code Generation tab of the Code Style. PHP page of the Settings dialog (Control+Alt+S).

      • To remove a parameter, select any row and click The Remove button.

      • To reorder the parameters, click The Up icon and The down icon. For example, if you want to put a certain parameter first in the list, click any of the cells in the row corresponding to that parameter, and then click The Up icon the required number of times.

      • To change the type, name, or the default value of a parameter, make the necessary updates in the table of parameters (in the Type and Parameter fields).

    • Propagate new method parameters (if any) along the hierarchy of the functions that call the current function. There may be functions that call the function whose signature you are changing. These functions, in their turn, may be called by other functions, and so on. You can propagate the changes you are making to the parameters of the current function through the hierarchy of calling functions and specify, which calling functions should be affected and which shouldn't.

      To propagate a new parameter:

      1. Click the Propagate Parameters button the Propagate Parameters button.

      2. In the left-hand pane of the Select Methods to Propagate New Parameters dialog, expand the necessary nodes and select the checkboxes next to the functions you want the new parameters to be propagated to.

        the Propagate Parameters dialog

        To help you select the necessary functions, the code for the calling function and the function being called is shown in the right-hand part of the dialog (in the Caller Method and Callee Method panes respectively).

        As you switch between the functions in the left-hand pane, the code in the right-hand pane changes accordingly.

      3. Click OK.

  4. To see the expected changes and make adjustments prior to the refactoring, click Preview.

  5. Click Refactor.

Examples

Example 1

This example 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 are affected depending on the refactoring settings.

Before

After

function result($a) { } function showResult($a) { $this->result($a); }

The function is renamed and a new $b parameter is added:

function generateResult($a,$b) { } function showResult($a,$b) { $this->generateResult($a,$b); }

The function is renamed and a new $b parameter is added with the new_param default value:

function generateResult($a,$b) { } function showResult($a) { $this->generateResult($a,'new_param'); }

The function is renamed, and a new $b parameter is added and propagated through the showResult() calling function to the function call.

function generateResult($a,$b) { } function showResult($a,$b) { $this->generateResult($a,$b); }

Example 2

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 dialog (Control+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; } }

Change signature dialog

The Change Signature refactoring combines several different modifications that can be applied to a function signature. You can use this refactoring to:

  • change the function name and return type

  • add, remove, and reorder parameters

  • assign default values to the parameters

  • change parameter names and types

  • propagate new parameters through the function call hierarchy

When changing a function signature, PhpStorm searches for all usages of the function and updates all the calls, implementations, and override replacements of the function that can be safely modified to reflect the change.

The Change Signature refactoring is supported only for PHP and JavaScript (refer to Refactoring JavaScript).

Item

Description

Name

Name of a function, method, or a method specification.

Parameters

List of parameters in the signature. In the Parameters field, you can perform the following actions with parameters:

  • Add The Add icon: Adds a new parameter. You can specify properties of the new parameter in the corresponding table row (a name, type, and a default value).

  • Remove The Remove icon: Removes a parameter.

  • Up The Up icon and Down The Down icon icons: Reorders parameters.

Last modified: 27 October 2023