How to be Efficient in PhpStorm: An IDE that Really Matters
What does it mean to be a good software developer?
First, it requires deep knowledge of the particular tech stack you’re working in. If you are a LAMP developer, you undoubtedly need to be an expert in PHP and MySQL, know modern frameworks, and have some decent skills in Linux administration.
Second, but no less important, is working knowledge of the supplemental tools you use everyday in your workflow.
You need to be able to work properly with version control systems, continuous integration tools, virtual machine management services (like Vagrant) and, obviously, your integrated development environment (IDE).
This article covers the popular, robust IntelliJ IDEA IDE PhpStorm, and how, with a little effort, you can easily double your efficiency and even prevent bugs while you code.
Train Yourself with Keyboard Shortcuts
Let’s start with the simplest and maybe the most important skill for any IDE: proper keyboard usage.
If you compare a seasoned programmer and an ordinary PC user, the first thing you’ll notice is that the programmer spends nearly all of her time using a keyboard and hardly ever touches the mouse. There’s a reason for that: It’s much more efficient.
First, Become a Good Typist
While typing isn’t connected directly to IDE skills, shortcuts are useless if you need to stare at your keyboard to find every single key.
If you’re not already typing at least 50 words per minute (WPM), find a touch typing training website such as TypingClub.com, and increase your speed.
There are also competitive typing websites, such as TypingRaces.com, where you can compete with other people in typing. My recommendation? Engage in a race as a morning warm-up each day.
Train Yourself To Use PhpStorm’s Shortcuts
The fastest way to learn is to not give yourself an alternative to keyboard shortcuts.
When I was studying shortcuts, I turned off all menus, tabs, navigation bars, etc. You can do it as well by navigating to the View menu and unchecking all the marks (you may want to leave the ‘Status Bar’):
My IDE now looks like this:
As you can see, I can’t avoid using shortcuts, even if I wanted to.
It took me about three or four days of suffering to get past the laptop smashing stages of adjustment.
If you find your motivation flagging, PhpStorm has a built-in productivity guide. It contains a list of features and statistic of usage for each of them. Just click ‘Help’, then ‘Productivity Guide’, and you’ll see something like this:
As you can see, shortcuts saved me from typing more than 20,000 additional characters (over the course of six months) and prevented about 1,000 bugs. Pretty motivating!
It also tells me which features I’m not using very often, identifying places where I can improve my shortcut skills. Review your statistics, and use the knowledge wisely.
Top Keyboard Shortcuts
Jetbrains provides a handy PDF with all available shortcuts listed in its official documentation, and furthermore has a list of ‘Keyboard Shortcuts You Cannot Miss ‘. Start learning this immediately, until you can do it in your sleep.
By far, my favorite shortcuts are:
Ctrl+ E
- toggle between recently used filesCtrl + /
- comment/uncomment a block of codeCtrl + B
- go to the class declarationCtrl + N
- navigate to classCtrl + R
- search and replaceCtrl +Alt + L
- reformat codeAlt + Enter
- show intention actions and quick-fixesCtrl + Shift + Enter
- complete statement
Some of these are self explanatory, others could use a little more coverage.
Switching Files with Ctrl + E and Enter
Imagine that you need to frequently switch between two files, e.g. between a PHP class with business logic of your app and a template, which is used to render and output it.
Many developers would switch from the keyboard to the mouse, navigate to a tab with the file, click it, and find the needed part with a scroll wheel. The problem: It’s extremely slow!
Isn’t it better to just push Ctrl+E and then Enter?
Ctrl+E will show you a list of recently used files, and if you immediately push Enter, you’ll be switched to the file used just before the current one.
If you want to go back, just push the same sequence again.
This is the fastest way to toggle between two files, with only three keystrokes! Just imagine how much time you’ll save, when you’re having to do this hundreds of times per day.
Show Intention Actions and Quick-Fixes with Alt+Enter
Any modern IDE highlights errors and provides some suggestions for code improvement. If you move the cursor to any incomplete or undocumented piece of a code, you’ll see a light bulb icon, and if you click on it, you’ll see a popup menu of possible actions.
The same action can be done by pressing Alt+Enter
, which is much more convenient.
In the case on the screenshot above, PhpStorm offers two options: Add a PHPDoc specification to the function (which is a very good advice, stay tuned), and remove an unused parameter.
The same shortcut can be used for a lot of other common actions: creating getters and setters for class properties, initializing fields in constructors, adding undefined methods and properties, etc.
Complete Statements with Ctrl+Shift+Enter
This shortcut is very straightforward and extremely useful at the same time, doing exactly what it says.
For example, if you type $i = 0
and then press Ctrl+Shift+Enter
immediately after it, PhpStorm will add a semicolon to your expression. If you type if
and the shortcut after it, PhpStorm will add brackets, and so forth.
Use this shortcut as frequently as you can. It will save you a lot of time as all code consists of standard structures. Any speed increase in writing structures makes writing the code that much faster as well!
The IdeaVim plugin
Using shortcuts can drastically increase your productivity, but if it’s not enough for you, the next level is the IdeaVim plugin.
As you probably know, Vim is a console text editor widely used by Linux geeks and devops’ engineers, to administrate remote servers via the command line.
Vim has two important traits.
First, it’s hard to learn. Second, if you know it well, it pays off. Your productivity in text editing will become much better than with any GUI editor. There will also be situations in your career when you have no choice but to use a console editor, like Vim, (if you need to fix something on a remote web server and you have only console access to it, for example).
But how is Vim related to PhpStorm?
Well, many developers want to combine Vim efficiency with features of a modern IDE (e.g. code completion, class search, advanced search and navigation). For that purpose the IdeaVim plugin was created.
Important note: From here on out, I assume that you know the basics of Vim. If you don’t, you can get started using Vim with vimtutor, which is accessible in almost all Linux distributions.
Plugin Installation
1. Navigate to ‘File’, then ‘Settings’, then ‘Plugins’ and type IdeaVim in the plugin search box. Mark the checkbox to the right of the plugin name and click “Apply”. 2. Restart PhpStorm. 3. Turn on the plugin via checking ‘Tools’ then ‘Vim Emulator’
4. Original PhpStorm shortcuts and Vim shortcuts (enabled by the plugin) can conflict with each other, so you need to resolve these conflicts manually. Navigate to ‘File’, ‘Settings’, ‘Other Settings’, then ‘Vim Emulation’. You’ll see the list of conflicting shortcuts. In this list, you can choose whether to use the PhpStorm shortcut or the Vim shortcut bound to that key sequence. You can see my current choices in the screenshot below. If you can’t make a decision, just leave a shortcut undefined; PhpStorm will ask you to choose when you use it for the first time.
Plugin Usage
After you turn on the plugin, you’ll immediately notice that the editing area becomes similar to Vim:
- The cursor changed its form and became rectangular, like in Vim.
- If you try to type something in the editor, nothing will happen.
From this point, you can start editing your files exactly like you would do it in Vim, using the h
, j
,k
, l
keys for navigation, and the i
key for switching between normal mode and insert mode, etc.
Almost all Vim shortcuts are supported by the plugin.
Simultaneously, all of the IDE features are still present, so you can edit the text extremely fast, using only your keyboard (like in Vim), and still jump between classes, files, usages and definitions (like in a PhpStorm modern workflow).
Advanced Search
One of the best advantages of any IDE over ordinary text editors is the ability to implement an advanced search.
It may seem redundant, if you have worked only with small projects, but when you start working with a large codebase, with complicated OOP relations between classes, you will understand how indispensable it is.
It’s easy to find a particular string in a file by common search (Ctrl + F
) or search-and-replace (Ctrl + R
), but what if you need to find a definition of a particular class in thousands of files? Or if you want to find a particular method in a class containing thousands of lines of code? Or maybe you want to exclude all comments from the retrieval results? That’s where a modern IDE like PhpStorm helps.
In this section, I’ll show you the most useful variants of search in PhpStorm.
Find in Path
After the ordinary search, the easiest search is for a particular string in all files in some subfolder of the project. It’s so easily implemented in PhpStorm that I use it much more frequently than any other search options.
For example, search for usages of a class User
in my example app.
1. Push Ctrl + Shift + F
. You’ll see a popup window with Search options. Type ‘User
’ in the query string. You’ll see a number of results in a Preview tab:
2. Now, we can switch to the Preview tab, and manually find needed results there, but we have more than 100 matches. Tedious! Let’s be more specific.
First of all, we’re searching for the class usages, so let’s check the “whole words only” option. It will help us to avoid results like E_USER_ERROR
.
Next, let’s exclude searching in comments and string literals, and limit our results by .php
files only.
It also sometimes makes sense to specify a more concrete subfolder of the project. In this case, all project-specific code is situated in the app
folder.
So let’s change the scope directory to example-app/app
.
Now, we have a search window like this one:
Just by being more specific in our search parameters, we’ve decreased the number of results from hundreds to just 24!
Now, it’s very easy to examine each of them manually. Moreover, we don’t even need to open each file separately because we have a very convenient preview tab:
In everyday practice, the find in path search covers about 80 percent of all searches I make. It’s so useful that even if you use nothing else for your retrievals, you’ll still be pretty efficient.
Go to Declaration
Another extremely important ability of PhpStorm (as with all modern IDEs) is to jump between usages and declarations of classes and functions.
Let’s assume we have a function, which creates a new User
. Obviously, we do not remember all methods and properties of the User
class, so we need to examine the declaration of the class to decide how to perform all needed actions. There are several ways to do it.
First, you can open a class declaration as a quick read-only popup window.
Just move your cursor to any class usage and push Ctrl + Shift + I
. The read-only window will appear:
You can examine all necessary data, and return back to the initial file. It’s extremely quick; you don’t even need to switch between files!
But, as I mentioned, the popup window is read-only, so if you decided to change something in the User
class while examining it, you’ll need another shortcut: Ctrl + Shift + B
.
This navigates you directly to the class. When you make your changes, you can easily return to the initial file, using Ctrl + E, Enter
sequence we covered earlier.
Like what you’re reading?
Get the latest updates first.
No spam. Just great engineering posts.
List All Methods of the Current Class
The last variant of the search functionality, which I use everyday is ‘search by class structure’. Sometimes PHP classes are very long, and it’s difficult to find a particular property or method.
If you push Ctrl + F12
while the class file is open, you’ll see a popup window with a structure of the class:
If you begin typing the method name, you’ll see only the methods matching the pattern you typed:
Just imagine that you have about 100 methods (which is not a good practice, but you may run across such code), and you need to frequently switch between two of them. It can make you crazy using ordinary search!
By using the file structure search instead, every switch will take about five keystrokes. Just type Ctrl + F12
, start typing the name of the desired method, press Enter
, and you’ll get to its declaration.
By the way, Ctrl + F12
is not a convenient shortcut, so you may want to consider changing it to something else. Personally, I use Alt + R
.
There are many more search methods in PhpStorm, but these three can cover almost all everyday search tasks.
A Recap on Searching Functionalities
- If you need to search something found in multiple files, use the find in path search (
Ctrl + Shift + F
), and try to make search options (filtering) as specific as you can. - If you need to quickly see the source of a class or a method, use
Ctrl + Shift + I
and examine it via the quick popup window (if you need to edit it, navigate to the declaration viaCtrl + Shift + B
instead). - If you have a large class and need to inspect its structure or quickly switch between methods, use a file structure shortcut,
Ctrl + F12
by default.
Live Templates Usage for All Basic Language Structures
While shortcuts allow you to write and navigate code fast, there is a way to improve it even more — autocompletion and live templates.
Basic autocompletion is pretty easy, and you should certainly know about it.
If you start typing a name of a class, method, or variable, any good IDE will show you a list of possible endings. You can choose any of them, and avoid typing up to the end.
That said, you can go much further with live templates.
In this section, you’ll learn which live templates I use in my everyday work. I divide them into three groups: basic language constructions, functions with long names, and HTML chunks.
Basic Language Constructions
Needless to say, developers use standard language constructions, like while
, for
, foreach
, if
many times per day, so it’s extremely inefficient to type them from scratch every time. That’s why there are built-in live templates for them in PhpStorm.
For some life-changing live template magic, open up PhpStorm, and type forek
in the editing area, and press Enter
. You’ll see this:
PhpStorm substituted a template with a complete foreach
cycle and prepared stubs for all necessary variables. You just need to fill in correct variable names.
If you press Ctrl + J
, you’ll see the list of currently supported live templates, applicable for the current place of the code. You can choose any of them.
As you may have noticed, some very frequent constructions are missing here. For example, I always miss a live template for the while
cycle, so let’s create it!
Navigate to ‘Settings’, then ‘Editor’, then ‘Live Templates’, choose ‘PHP’, and click on the green ‘+’ at the right-top of the popup window:
Fill out the ‘abbreviation’ field with ‘wh’, and the ‘description’ field with ‘while(bool_expr){…}’. The ‘template text’ field should have the following:
while($EXPR$) {
$END$
}
Click the ‘define’ link below the ‘template text’ textarea and choose ‘PHP’ from the list, so it knows in what context to use this template.
Now, let’s discuss how it works.
When you type wh, Enter
in your editing area, it will be substituted with the template text.
The cursor will be moved to the first template variable, $EXPR$
in our case, then you can type any expression (e.g. $i > 0
) and press Enter
again.
After that, the cursor will be moved to the next variable (we only have one in this example), and so on. $END$
is a special variable that denotes the place where the cursor should move after all variables are filled.
We have only one variable, so it will be moved to the $END$
right after we fill $EXPR$
, and we’ll be ready to input a cycle body.
Create your own live templates for all language constructions that you frequently use. It’s much better to invest some time upfront than to spend it everyday on typing such constructions from scratch.
PHP Functions
Unfortunately, PHP has a reputation of a language with long and inconsistent function names.
The good news it that you can substitute them with short abbreviations using live templates. For example, if you want to properly check if some key exists in an array, you need to call the function array_key_exists($my_key, $my_array)
.
It’s extremely tedious to type it every time even with standard autocompletion, so just create live templates for all such cases.
Personally, I prefer ‘ake’ for array_key_exists, you can choose any other. Here’s how to create the ‘ake’ live template:
HTML chunks
Last, but certainly not the least popular usage of live templates, is handling HTML chunks.
If you’re not already using live templates, I bet you’ve been doing a lot of tedious (and basically, useless) work, creating standard HTML markup elements. Live templates change that by doing what we’ve done above but with the ‘template text’ field being your HTML.
Avoid Bugs with PHPDocs
So far, you’ve learned PhpStorm efficiency tips and tricks, but wait, there’s more. Efficiency is not the only area where it can help, it can also help avoid bugs, even before the first run of your program.
Out of the box, PhpStorm tells you about obvious errors.
For example, if you have forgotten a semicolon after a statement, it will mark it red, but that functionality is trivial, so I’ll not describe it.
What’s interesting is when PhpStorm tells you about much more complex bugs, when you give it the information it needs to via PHPDocs.
Ideally, PHPDoc specifications is mandatory on your team for all methods and made as rigorous as possible. Consider an example with our User
class again. Assume we have a function, which returns an array of User
objects:
By default it’s not documented, so other developers may not know the type of the returned value and may use it improperly.
For example, some other developer wants to retrieve a list of usernames. For some reason, she thinks, that the User
object has a getName() method. But in reality it doesn’t, so the developer makes a mistake:
PhpStorm doesn’t treat this is as a bug because it knows nothing about the $user variable. So it stays unnoticed, and we end up with a very nasty error.
The first option is to just turn on strict typing, which became possible in PHP7, but let’s assume we can’t do that for some reason. Maybe we are working with legacy code. In this case, PhpStorm will help us, if we let it.
For this to work, you’ll need to generate a PHPDoc for every function. It’s commonly considered a good habit, and it will help you to avoid bugs, get along with fellow developers, and even pass interviews in which employers examine your source code.
To generate a PHPDoc, move the cursor to the name of the function, press Alt + Enter
, pick the item “Generate PHPDoc for function” in the popup menu, and press Enter
again.
The basic PHPDoc template will appear:
You can see PhpStorm guessed that the returning value is an array, but it couldn’t guess that it’s an array of User
objects, so let’s explicitly stipulate this by changing ‘array’ to read ‘User[]’ instead:
Now that we’ve told PhpStorm what it needs to know about the method, let’s return to the getUserNames()
method call:
Now that PhpStorm can parse the PHPDoc return type declaration, it can point us to the error and even hint as to what exactly is wrong. PhpStorm, in combination with PHPDocs, can make PHP a very rigorous language! An amazing outcome!
PhpStorm is not Just for PHP Developers
While all the topics covered center on PHP development, PhpStorm is based on a very powerful IntelliJ Platform, making it possible to use with various languages, frameworks, and tech stacks, that are far from the PHP world.
Front-end development
Even if you are a back-end developer, you’ll need to solve some front-end problems from time-to-time. It can be a simple fix in HTML markup, adding a parameter to the REST client, written on AngularJS, etc. The good news is that all of that can be done in PhpStorm.
The basic rule of thumb is: Check if it’s supported out of the box. If not, try to find a plugin.
For example, Javascript, CSS, and popular CSS preprocessors, like LESS and SASS, are supported out of the box. AngularJS support is easily added as a plugin (installation path is the same as for IdeaVim plugin). With this mindset, you’ll be able to solve nearly all possible front-end tasks.
Working with Databases
Typically, application databases are managed via command line or special tools, like MySQL Workbench, but PhpStorm can help you here too.
Just click Database on the right margin of your screen, and you’ll see a list of available databases (initially empty) and a query window, which has autocompletion, like the common editing area, making it very convenient.
PhpStorm supports almost all modern databases. MySQL, PostgreSQL, Oracle Database, and SQLite are supported out of the box, while MongoDB is supported via plugin installation.
MEAN stack
The final and the most amazing non-PHP example is MEAN stack development.
MEAN stands for MongoDB, ExpressJS, AngularJS, Node.js.
It’s a trending tech stack, being used just about everywhere. It’s very fast and extremely good for fast, lightweight web-services.
Even as a PHP developer, I sometimes create Node.js services as supplemental tools for PHP projects (e.g. websocket services, simple chatbot back-ends, etc).
As you probably guessed, all components of the MEAN stack are supported in PhpStorm, so whenever you need to create a MEAN stack project, keep in mind that you don’t need another IDE. PhpStorm will do the job perfectly.
The Power of PhpStorm is in Your Hands
Using the power of PhpStorm and the key skillsets covered in this article, you too can increase your efficiency and prevent bugs before you even finish typing.
Post by Partner poster : Toptal
Originally published at DreamiFly.