PhpStorm JavaScriptCodeRefactoringsinPhpStorm 200116 0047 2518
PhpStorm JavaScriptCodeRefactoringsinPhpStorm 200116 0047 2518
Tweet
Martin Fowler states "Refactoring is a controlled technique for improving the design of an existing code base." In this tutorial, we'll have a
look at the JavaScript code refactorings available in PhpStorm. Many refactorings are available in the IDE, which can carry them out
automatically and makes sure existing code is updated and will not break. Let's see.
Refactoring in PhpStorm
The Refactor This Action
Available Refactorings
Change Signature Refactoring
Copy Refactoring
Extract Function Refactoring
Extract Variable/Constant Refactoring
Extract Parameter Refactoring
Inline Refactoring
Move Refactoring
Rename Refactoring
Safe Delete Refactoring
While this tutorial covers refactorings for JavaScript code, the IDE also supports refactoring other languages like PHP, HTML and so
forth. Check our PHP Code Refactorings in PhpStorm tutorial to learn more, or use the the Refactor This in the various language
editors.
Refactoring in PhpStorm
Let's first see what a typical refactoring looks like. Each refactoring does different things, but there are some things they have in common. Let's try
renaming a function in our codebase!
To do this, we can use the Refactor | Rename... context menu on any file or symbol, or place the cursor on it and use the Shift+F6 keyboard
shortcut to invoke the rename refactoring immediately. Note that we could also use the Refactor This action. This last one is very convenient to
invoke refactorings as we only need to remember one keyboard shortcut, Ctrl+Shift+Alt+T (Ctrl-T on Mac OS X), to show a pop-up with the
different refactorings that we can do.
Each refactoring will show a different dialog in which we can provide the options for it. In this case, we have to provide the new name for the
function we're about to rename. Some of the options in this dialog will be available for other refactorings as well: we can (optionally) search in
comments and strings and do some sort of find-replace in there, we can search for text occurrences and so on.
When invoking a refactoring in PhpStorm, the IDE will:
Perform the refactoring
Track down and correct the affected code references automatically
Warn about occurrences it cannot update automatically
If you are unsure about the outcome, consider clicking the Preview button before carrying out the refactoring. It will open the Find Refactoring
Preview Tool Window and show us all of the actions the refactoring will perform.
Using the up/down keys and Enter (or the numeric identifier for each entry in the pop-up), we can invoke the refactoring.
Available Refactorings
There are many refactorings available for JavaScript code. In this section, let's go over them and see what actions they perform. For every
refactoring, we'll add a link to the PhpStorm Web Help, which contains a full description of all available options for a given refactoring.
All refactorings are available from the Refactor context menu or via their respective keyboard shortcuts.
Check out the Hands-on PhpStorm workshop materials to download a sample project in which we can try all of these refactorings.
Copy Refactoring
Using the Copy Refactoring, we can copy a class, file or directory to another directory. We can invoke it by selecting a file and using the keyboard
shortcut (F5) or by dragging the file and dropping it in a folder whilst we hold the Ctrl key down.
+M (or Alt+CMD+M on MacOS X). Upon invocation, PhpStorm lets us select the scope in which the extracted function will be defined. The IDE
will also show us exactly where the function will be injected into our code.
The refactoring will extract the selected block of code into a function, detecting parameters and return values. We can choose the name of the
function, pick how it will be defined (using declaration or not) and select and name function parameters.
The refactoring will create a new function parameter which we can name, make optional (so a default value assignment will be generated as well)
and generate JSDoc comments for.
Inline Refactoring
The Inline Refactoring Replace is the opposite of Extract Variable/Constant / Extract Function refactorings: it replaces redundant variables or
functions with the full expression. We can select a variable and press Ctrl+Alt+N (or Alt-CMD-N on Mac OS X) to invoke it.
Move Refactoring
With the Move Refactoring (F6), we can change the location of a file or directory.
Rename Refactoring
Using the Rename Refactoring (Shift+F6), we can rename symbols, automatically correcting all references in our codebase. We can rename
Functions (and therefore, namespaces and classes), Variables, Parameters, CSS Color Values, Files and Directories.