Our 14th door in our Advent Calendar!
Do you want to know how to do PHP Code refactoring like a boss with PhpStorm? Then this post is for you.
PhpStorm is probably the most powerful IDE for PHP, but with great power comes great responsibility. You will probably not be able to save the world, like Peter, but you will definitely save a lot of time if you learn to get most out of the amazing features of this IDE.
In this post, I will show you some of its refactoring tools.
Refactoring Tool 1: “Refactor This”
Refactoring Tool 2: Change signature
Use it to change method/function signature (name, parameters). It’s far more powerful than just renaming the function or parameters. You can also remove, reorder, add parameters (including default values) and propagate all those changes to the callers!
Refactoring Tool 3: Copy/Clone
Refactoring Tool 4: Extract Constant
Extract static values to constants. You will also be able to replace all occurrences of the static value. Imagine you have some kind of statuses as string literal all other the project and you want to extract them to constants, then that feature will be much better than search and replace, because it’s context aware and respectively safer.
Refactoring Tool 5: Extract Field
Refactoring Tool 6: Extract Interface
Refactoring Tool 7: Extract Method
Extract a block of code into a method. Extract code to functions to encapsulate logic and to reuse it further. It’s powerful because it takes care of the parameters and return value(s). Divide and rule!
Refactoring Tool 8: Extract Parameter
Add a new parameter to a function declaration. The example is with JS code, but it works with PHP too. Replace hard coded values with parameters, so that you can change them when needed without breaking the existing code.
Refactoring Tool 9: Extract Variable
Refactoring Tool 10: Inline
Refactoring Tool 11: Move
Refactoring Tool 12: Pull Members Up / Push Members Down
Refactoring Tool 13: Rename
Refactoring Tool 14: Safe Delete
Refactoring Tool 15: Move Static Member
In this post we demonstrated some of useful refactoring capabilities of the PhpStorm. Hope you found those examples useful and wish you good luck at refactoring, now you know how to do it right!
And tomorrow there will be another great contribution to our Inpsyde WordPress Advent Calendar!