JesusValera

PhpStorm tricks ✨

Customize the IDE with templates, plugins and more

 Found a typo? Edit me

PhpStorm is a powerful IDE that helps to boost your development, let's take a look to some hidden tricks to make PhpStorm even better! ⚡️

phpstorm-logo

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:

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

phpstorm-define-psr-12

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.

live templates in action

Go to Editor ➔ Live Templates and adjust each template with the $RETURN_TYPE$ variable.

private function $NAME$($PARAMETERS$): $RETURN_TYPE$ {
    $END$
}

Finally, press on Edit variables and insert "void" (with quotes) on RETURN_TYPE field.

live-template-setup

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
declare(strict_types=1);
#parse("PHP File Header.php")
PHP Class
<?php
declare(strict_types=1);
#parse("PHP File Header.php")

#if (${NAMESPACE})
namespace ${NAMESPACE};

#end
final class ${NAME} {

}
PHP Interface
<?php
declare(strict_types=1);
#parse("PHP File Header.php")

#if (${NAMESPACE})
namespace ${NAMESPACE};

#end
interface ${NAME} {

}
PHP Trait
<?php
declare(strict_types=1);
#parse("PHP File Header.php")

#if (${NAMESPACE})
namespace ${NAMESPACE};

#end
trait ${NAME} {

}
PHP Enum
<?php
declare(strict_types=1);
#parse("PHP File Header.php")

#if (${NAMESPACE})
namespace ${NAMESPACE};

#end
enum ${NAME}#if (${BACKED_TYPE}) : ${BACKED_TYPE} #end{

}
PHP Test
<?php
declare(strict_types=1);
#parse("PHP File Header.php")

#if (${NAMESPACE})
namespace ${NAMESPACE};
#end

#if (${TESTED_NAME} && ${NAMESPACE} && !${TESTED_NAMESPACE})
use ${TESTED_NAME};
#elseif (${TESTED_NAME} && ${TESTED_NAMESPACE} && ${NAMESPACE} != ${TESTED_NAMESPACE})
use ${TESTED_NAMESPACE}\\${TESTED_NAME};
#end

final class ${NAME} extends#if(${NAMESPACE}) \PHPUnit_Framework_TestCase #else PHPUnit_Framework_TestCase #end{

}
PHP Unit 6 Test
<?php
declare(strict_types=1);
#parse("PHP File Header.php")

#if (${NAMESPACE})
namespace ${NAMESPACE};
#end

#if (${TESTED_NAME} && ${NAMESPACE} && !${TESTED_NAMESPACE})
use ${TESTED_NAME};
#elseif (${TESTED_NAME} && ${TESTED_NAMESPACE} && ${NAMESPACE} != ${TESTED_NAMESPACE})
use ${TESTED_NAMESPACE}\\${TESTED_NAME};
#end
use PHPUnit\Framework\TestCase;

final class ${NAME} extends TestCase {

}
PHPSpec Specification
<?php
declare(strict_types=1);
#parse("PHP File Header.php")

#if (${NAMESPACE})
namespace ${NAMESPACE};
#end

#if (${TESTED_NAME} && ${NAMESPACE} && !${TESTED_NAMESPACE})
use ${TESTED_NAME};
#elseif (${TESTED_NAME} && ${TESTED_NAMESPACE} && ${NAMESPACE} != ${TESTED_NAMESPACE})
use ${TESTED_NAMESPACE}\\${TESTED_NAME};
#end
use PhpSpec\ObjectBehavior;

final class ${NAME} extends ObjectBehavior {

}
Codeception Unit Test
<?php
declare(strict_types=1);
#parse("PHP File Header.php")

#if (${NAMESPACE})
namespace ${NAMESPACE};

#end
#if (${TESTED_NAME} && ${NAMESPACE} && !${TESTED_NAMESPACE})
use ${TESTED_NAME};
#elseif (${TESTED_NAME} && ${TESTED_NAMESPACE} && ${NAMESPACE} != ${TESTED_NAMESPACE})
use ${TESTED_NAMESPACE}\\${TESTED_NAME};
#end

final class ${NAME} extends \Codeception\Test\Unit {

}
Codeception Functional Test
<?php
declare(strict_types=1);
#parse("PHP File Header.php")

#if (${NAMESPACE})
namespace ${NAMESPACE};

#end
#if (${TESTED_NAME} && ${NAMESPACE} && !${TESTED_NAMESPACE})
use ${TESTED_NAME};
#elseif (${TESTED_NAME} && ${TESTED_NAMESPACE} && ${NAMESPACE} != ${TESTED_NAMESPACE})
use ${TESTED_NAMESPACE}\\${TESTED_NAME};
#end

final class ${NAME} {

}

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 Settings
  • Alt + 1 = Display/hide Project bar
  • Alt (twice) = Search in the whole project (class names, files, symbols, actions...)
  • Ctrl + N = Search by class name (similar to Alt (twice) but only for class names)
  • Alt + →/← = Move to left/right tab
  • Ctrl + G = Go to line X:Y
  • Ctrl + B = Navigate in/out (similar to Ctrl + Click)
  • Ctrl + Y = Delete current line
  • Ctrl + C/V (without selection) = Copy/Paste full line
  • Ctrl + Shift + V = Paste with history
  • Ctrl + E = Display recent files
  • Ctrl + Shift + E = Display last 3 recent files
  • Ctrl + Shift + F = Find in path (search words in the whole project)
  • Ctrl + Shift + L = Reformat Code
  • Ctrl + Shift + ↑/↓ = Moves the current code block
  • Ctrl + Shift + M = Go to start/end of current brackets
  • Ctrl + Shift + T = Open test from specific class (and vice-versa)
  • Shift + Alt + Click = Multiple cursor
  • Ctrl (twice keeping it pushed) + arrows = Multiple cursor from adjacent lines
  • Ctrl + W = Select gradually
  • Alt + J = Select next occurrence similar to current one
  • Ctrl + Alt + F/M/C/V/P = Refactor Function/Method/Constant/Variable/Parameter
  • Ctrl + Alt + N = Inline refactor
  • Ctrl + Insert = Contextual menu to override methods, generate getters, setters...
  • Ctrl + Shift + F10: Run the scope where the caret is
  • Ctrl + 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 to Ctrl + 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 in Settings ➔ 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/-.