A very powerful refactoring is the Change signature refactoring in PhpStorm. It enables us to modify a function signature in many ways: we can change the function name, change its visibility, add, remove and reorder parameters as well as rename parameters.
Take the searchUser function in a class called UserReporsitory.
There is no real use for this method as it is now. It’s private, so no other classes can make use of this function. It also has no parameters where we want to be able to search users based on their username. Using the Refactor | Change Signature… context menu (Ctrl+F6 or Cmd+F6 on Mac) we can open the Change Signature dialog.
In this dialog, we can change the function’s visibility from private to protected or public. We can rename it to searchUserByUsername so that the intent of this function is clearer to the caller. Next, we can add a parameter $username and give it a default value, in this case an empty string. Note that we can also add the parameter type (e.g. MyNamespaceMyType $username) as well as an initializer (e.g. $username = null). We can reorder parameters using the buttons on the right.
The Change Signature dialog shows us a preview of the new function signature. We can use the Refactor button to immediately carry out the refactoring or the Preview button to see a list of all code that is about to be updated with this new signature. Once done, our new searchUserByUsername function will look like this:
What’s really interesting is that all calls to this method have been updated as well. For example, the following call has been updated to use the default value we entered in the Change Signature dialog earlier:
The Change Signature refactoring will update the function itself, all calling code, and overloading functions in the case of interfaces or abstract class functions being refactored. Using the Alt+G shortcut (or clicking the Propagate Parameters button) lets us add new parameters to all calling functions as well.
As always, we appreciate your feedback in the comments below or in our forums!
Develop with pleasure!
– JetBrains Web IDE Team