PHP Code Refactoring Tools described.

How to do PHP Code Refactoring with PhpStorm

In this Advent Calendar post I would like to present some refactoring tools of PhpStorm.

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”

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 with PhpStorm - 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 with PhpStorm - Change Signature

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 with PhpStorm - 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 with PhpStorm - Extract Constant

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 with PhpStorm - Extract Field

Refactoring Tool 6: Extract Interface

Extract an interface from a class.
Refactoring with PhpStorm - 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 with PhpStorm - Extract Method

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 with PhpStorm - Extract Parameter

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 with PhpStorm - Extract Variable

Refactoring Tool 10: Inline

Replace redundant variables or functions with the full expression.
Refactoring with PhpStorm - Inline

Refactoring Tool 11: Move

Change the location of a file, directory, class or static member. Move things without fearing to break something.
Refactoring with PhpStorm - Move

Refactoring Tool 12: Pull Members Up / Push Members Down

Move members from subclass to superclass or vice-versa.
Refactoring with PhpStorm - Pull Members Up

Refactoring Tool 13: Rename

Rename symbols, automatically correcting all references in the code.
Refactoring with PhpStorm - Rename

Refactoring Tool 14: Safe Delete

Safely remove files, code or symbols.
Refactoring with PhpStorm - Move Static Member

Refactoring Tool 15: Move Static Member

Move static fields and methods to another type.
Refactoring with PhpStorm - Move Static Member

Refactoring Tool 16: Make Static

Make method static.
Refactoring with PhpStorm - Make 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!

And tomorrow there will be another great contribution to our Inpsyde WordPress Advent Calendar!

Leave a Reply

Your email address will not be published. Required fields are marked *