PHP Code Refactoring with PhpStorm – this is how to do it

In this post, we present some refactoring tools of PhpStorm.

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

Refactoring with PhpStorm - 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 with PhpStorm - 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

Refactoring with PhpStorm - 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 with PhpStorm - 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

Refactoring with PhpStorm - Extract Interface

Extract an interface from a class.


Refactoring Tool 7: Extract Method

Refactoring with PhpStorm - 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

Refactoring with PhpStorm - 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 with PhpStorm - 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

Refactoring with PhpStorm - Inline

Replace redundant variables or functions with the full expression.


Refactoring Tool 11: Move

Refactoring with PhpStorm - Move

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


Refactoring Tool 12: Pull Members Up / Push Members Down

PHP Code Refactoring with PhpStorm - Pull Members Up

Move members from subclass to superclass or vice-versa.


Refactoring Tool 13: Rename

Refactoring with PhpStorm - Rename

Rename symbols, automatically correcting all references in the code.


Refactoring Tool 14: Safe Delete

Refactoring with PhpStorm - Move Static Member

Safely remove files, code or symbols.


Refactoring Tool 15: Move Static Member

Refactoring with PhpStorm - Move Static Member

Move static fields and methods to another type.


Refactoring Tool 16: Make Static

Refactoring with PhpStorm - Make Static

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!