PHP code refactoring
Last modified: 10 August 2022Refactoring 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
In the editor, place the cursor within the name of the method whose signature you want to change.
Press Ctrl+F6. Alternatively, choose Refactor | Change Signature on the main menu or context menu.
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
.
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.
tip
(There may be methods that call the method whose signature you are changing. These methods, in their turn, may be called by other methods, and so on. You can propagate the changes you are making to the parameters of the current method through the hierarchy of calling methods and also specify which calling methods should be affected and which shouldn't.)
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 |
---|---|
|
|
|
|
|
|
Thanks for your feedback!