PhpStorm tricks ✨
Customize the IDE with templates, plugins and more
Found a typo? Edit mePhpStorm is a powerful IDE that helps to boost your development, let's take a look to some hidden tricks to make PhpStorm even better! ⚡️
Plugins
Plugins are a crucial part of the IDE.
If you are a Symfony user, probably you use plugins
like Symfony Support
and Twig. In the other hand, if you are a Laravel one, I bet you use Laravel Idea
and Blade.
However, I recommend you GitToolBox
or String Manipulation in
case you never tried them.
Another recommendation, in case you want to familiarize with the IDE shortcuts
is Key Promoter X.
Although, my favorite so far, which is also not very well-known
is PHP Inspections (EA
Extended). This plugin helps you with architecture related issues, regular expressions, non-optimal, duplicated and
suspicious "if" conditions, performance issues beyond others.
You can check the full list of features on their
GitHub page, this one is a MUST.
Code Style
PHP is a programming language where everybody can write the code as they wish, in order to unify the
industry, PSRs was born.
That is the reason we can find standards for Loggering
(
PSR-3), Event Dispatcher
(PSR-14),
and even for the Clock
(PSR-20). You can check the
full list of all available PSRs here.
One of the most popular PSRs is concerning the Code Style
, that is, it defines things like:
- Should I use tabs or spaces?
- Where should I place the brackets in a method? And in a conditional/loop?
- Are the parenthesis required when instantiating a class with empty constructor?
During the last years, due to the big and fast changes the language experienced, PSR-2 was quickly replaced by PSR-12.
However, the language continues evolving very fast,
so PERs were introduced.
Unfortunately, at the moment is not possible to define PER
as Code Style
in PhpStorm, anyway, PSR-12
is good
enough.
To define PSR-12 by default, go to the following path and select PSR12.
Settings ➔ Editor ➔ Code Style ➔ PHP ➔ "Set from..." ➔ PSR12
Live Templates
Live Templates are snippets of code that are very used, and you can write them in a few characters.
PhpStorm brings some live templates, for example, if we type prif
, the generated code will be:
private function ░
{
}
There is a live template for all function combinations: prif
, prisf
, prof
, prosf
, pubf
& pubsf
.
It is possible to enhance the auto-completion with a default : void
return type as follows.
Go to Editor ➔ Live Templates
and adjust each template with the $RETURN_TYPE$
variable.
private
Finally, press on Edit variables and insert "void"
(with quotes) on RETURN_TYPE field.
File and Code Templates
The scaffolding you get when you create a new file in PhpStorm is also editable.
If you are into strict_types
and final
classes by default, you are in luck! 🎉
To change this, go to Editor ➔ File and Code Templates
and update all PHP
templates.
You can copy-paste the following code snippets into its own category (notice the spaces).
PHP File
PHP Class
PHP Interface
PHP Trait
PHP Enum
PHP Test
PHP Unit 6 Test
PHPSpec Specification
Codeception Unit Test
Codeception Functional Test
Shortcuts
It is very important to learn the IDE shortcuts, that will bring you confidence and will help you to be more productive,
in case you don't know them, definitely you SHOULD.
I would like to remind the plugin Key Promoter X
that I mentioned before.
PhpStorm also brings a PDF file with the most important shortcuts, go to Help ➔ Keyboard Shortcuts PDF
to download it.
List of most important shortcuts:
Windows/Linux
Ctrl + Shift + S
= Open SettingsAlt + 1
= Display/hide Project barAlt (twice)
= Search in the whole project (class names, files, symbols, actions...)Ctrl + N
= Search by class name (similar toAlt (twice)
but only for class names)Alt + →/←
= Move to left/right tabCtrl + G
= Go to line X:YCtrl + B
= Navigate in/out (similar toCtrl + Click
)Ctrl + Y
= Delete current lineCtrl + C/V (without selection)
= Copy/Paste full lineCtrl + Shift + V
= Paste with historyCtrl + E
= Display recent filesCtrl + Shift + E
= Display last 3 recent filesCtrl + Shift + F
= Find in path (search words in the whole project)Ctrl + Shift + L
= Reformat CodeCtrl + Shift + ↑/↓
= Moves the current code blockCtrl + Shift + M
= Go to start/end of current bracketsCtrl + Shift + T
= Open test from specific class (and vice-versa)Shift + Alt + Click
= Multiple cursorCtrl (twice keeping it pushed) + arrows
= Multiple cursor from adjacent linesCtrl + W
= Select graduallyAlt + J
= Select next occurrence similar to current oneCtrl + Alt + F/M/C/V/P
= Refactor Function/Method/Constant/Variable/ParameterCtrl + Alt + N
= Inline refactorCtrl + Insert
= Contextual menu to override methods, generate getters, setters...Ctrl + Shift + F10
: Run the scope where the caret isCtrl + Alt + Enter
= Jump to next line (adds ";" automatically if needed)
Mac
⌘ + ,
= Open Settings⌘ + 1
= Display/hide project bar⇧ (twice)
= Search in the whole project (class names, files, symbols, actions...)⌘ + O
= Search by class name (similar to⇧ (twice)
but only for class names)⌃ + →/←
= Move to left/right tab⌘ + L
= Go to line X:Y⌘ + B
= Navigate in/out (similar toCtrl + Click
)⌘ + ⌫
= Delete current line⌘ + C/V (without selection)
= Copy/Paste full line⌘ + ⇧ + V
= Paste with history⌘ + E
= Display recent files⌘ + ⇧ + E
= Display last 3 recent files⌘ + ⇧ + F
= Find in path (search words in the whole project)⌥ + ⌘ + L
= Reformat Code⌘ + ⇧ + ↑/↓
= Moves the current code block⌘ + ⇧ + T
= Open test from specific class (and vice-versa)⌥ + ⌘ + ⇧ + Click
or = Multiple cursor⌘ (twice keeping it pushed) + arrows
= Multiple cursor from adjacent lines⌥ + ↑
= Select gradually⌥ + ⌘ + F/M/C/V/P
= Refactor Function/Method/Constant/Variable/Parameter⌥ + ⌘ + N
= Inline refactor⌘ + N
= Contextual menu to override methods, generate getters, setters...⇧ + ⌃ + R
= Run the scope where the caret is⌘ + ⇧ + Enter
= Jump to next line (adds ";" automatically if needed)
In case the shortcut
⌃ + →/←
changes between Desktops in Mac, you can disable it inSettings ➔ Keyboard ➔ Keyboard Shortcuts... ➔ Mission Control
.
Add Custom shortcuts
We just saw the Ctrl + 1
/⌘ + 1
shortcut displays/hides the project bar, it is also very handy to set
the shortcut Ctrl + Alt + 1
/⌘ + ⇧ + 1
to display and select in the project bar the current file.
This way, we don't need to use the mouse at all, we can navigate between tabs with only the keyboard.
To set this new combination, in Settings ➔ Keymap
search by Select File in Project View
and Add Keyboard Shortcut
.
It is also very handy to add shortcuts to Split and Move Right
or Split and Move Down
with Ctrl + I/-
/⌃ + I/-
.