Change signature refactoring in PhpStorm
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
Subscribe to Blog updates
Thanks, we've got you!
Qodana Clone Finder: Early Access Program
In December 2020, we announced the EAP for Qodana, which is rapidly evolving into a comprehensive platform that allows companies to perform multi-level evaluations of the quality of code they own, contract, or purchase. Qodana helps you detect bugs without relying on an IDE, either on a local machi…
PhpStorm 2020.2 EAP #3
The Early Access Program for PhpStorm 2020.2 is in full swing and today we’ve got the third build of the 2020.2 EAP for you. In this blog post, you can read about improvements for Git installed in WSL2 and Search Everywhere. Download PhpStorm 2020.2 EAP (more…)…
PhpStorm 2019.3.3 Preview
We’ve just rolled out a preview for the third minor update for PhpStorm 2019.3. Please give the PhpStorm 2019.3.3 Preview build 193.6494.5 a try and share your feedback with us. (more…)…
How to Upgrade to PHP 7.4 with PhpStorm
Check out our fresh video with a few nice tips and tricks on how to update your codebase to PHP 7.4. It combines materials from the PhpStorm 2019.2 and PhpStorm 2019.3 releases, with a few cool bonuses to boot. Nikita drew our attention to the fact that setters are slower than assigning prop…