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”
Dialog is a place where you will find all refactoring capabilities available in the current context. It’s a good start until you remember keyboard shortcuts for each option in this menu.
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
Copy a class, file or directory to another directory or clone it within the same directory. You can also use this feature with drag and drop while holding Ctrl.
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
Extract an expression into a field, choosing it’s visibility and where the value should be set: field declaration, constructor or current method.
Refactoring Tool 6: Extract Interface
Extract an interface from a class.
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
Put the result of a selected expression into a variable. Very useful for big boolean expression that will be easier to understand if splitted into variables with suggestive names.
Refactoring Tool 10: Inline
Replace redundant variables or functions with the full expression.
Refactoring Tool 11: Move
Refactoring Tool 12: Pull Members Up / Push Members Down
Move members from subclass to superclass or vice-versa.
Refactoring Tool 13: Rename
Refactoring Tool 14: Safe Delete
Refactoring Tool 15: Move Static Member
Make method static.
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!