PHPStorm Live Templates Are Epic


I think that by now 100% of the entire community has concluded that PHPStorm is the best thing sliced Vim ;)

Ok, I am aware not everyone uses (or loves) PHPStorm, but those of us that do recognise that it's full of time saving awesomeness, from simple things like highlighting unused variables through to more interesting things like code generation (easy getters/setters), and being able to determine whether we have implemented all the required interface methods, etc.

The code generation feature is particularly useful.

What you may not know is that you are not only limited to the code generators that PHPStorm comes with out of the box.

Custom Live Templates

Live Templates are the secret sauce to increasing your development productivity inside PHPStorm.

If you work like I do, copy/paste is a common occurrence. Read some docs / blog post / StackOverflow article, copy the interesting bits, paste into your code and see if it works.

I'm particularly bad at remembering how to correctly define a Symfony controller from scratch, or the exact syntax and layout for a Symfony Form Type.

For these, I use custom live templates. This frees up valuable brain space, allowing me to remember more important things like old Simpsons' quotes (true), and relatives birthdays (no true).

Live Template Example

This example comes directly from the video. If you have ever worked with Symfony's Form Collection then you will likely have had to type this out at least once.

It's not so important to understand what this code does, so if you are unsure then don't worry about it. This is about the process, rather than the code.

The underlying code looks as follows:

public function addSpeaker(Speaker $speaker)
{
    if ( ! $this->speakers->contains($speaker) ) {
        $speaker->setConference($this);
        $this->speakers->add($speaker);
    }
    return $this->speakers;
}

public function removeSpeaker(Speaker $speaker)
{
    if ($this->speakers->contains($speaker)) {
        $this->speakers->removeElement($speaker);
    }
    return $this->speakers;
}

This is commonly used code if you work with collections.

Copying / pasting is error prone, time consuming, and with Live Templates, we can do better.

On a Mac, once inside PHPStorm, go to the top menu, PHPStorm > Preferences > Editor > Live Templates.

Go to the 'user' section, and click the + button on the right.

This will allow you to define a new Live Template.

The 'abbreviation' field is important. This is what you will type in to your code, which will then prompt you with an auto-complete (hit tab by default, but you can change this on a per-Live Template basis), and then you fill in the blanks.

The template code looks a little funky:

public function add$Something$($Something$ $$$SomethingLowerCase$)
{
    if ( ! $this->$SomethingLowerCase$s->contains($$$SomethingLowerCase$) ) {
        $$$SomethingLowerCase$->$OtherSide$($this);
        $this->$SomethingLowerCase$s->add($$$SomethingLowerCase$);
    }

    return $this->$SomethingLowerCase$s;
}

public function remove$Something$($Something$ $$$SomethingLowerCase$)
{
    if ($this->$SomethingLowerCase$s->contains($$$SomethingLowerCase$)) {
        $this->$SomethingLowerCase$s->removeElement($$$SomethingLowerCase$);
    }

    return $this->$SomethingLowerCase$s;
}

As crazy as this looks, there's really only three variables here, and only two we have to directly interact with.

All variables are surrounded with dollar signs: $Something$.

Our variable name doesn't matter, but a descriptive variable name is always better.

Every place in the Live Template that has $Something$ will be replaced in one go when we use our template.

Live Template PHP Variables

More confusing is $$$SomethingLowerCase$.

We need the initial double $ to tell PHPStorm to escape the dollar sign.

Then we use the standard Live Template variable definition as normal: $SomethingLowerCase$.

When inside the Live Template editor we can click the 'Edit variables' button and tweak our vars to better suit our needs.

Here, we are using a Live Template Variable Expression to decapitilise our variable name.

This is easier to see with a picture:

PHPStorm Live Template Variable Expression

Notice a couple of things from this picture.

Firstly, Live Template Variable Expressions can take the name of another variable as a parameter. When doing this, we don't need to use the $ sign.

Secondly, providing a Default Value for our variable will act as helper text when our Live Template is initially used, serving as a reminder to what might make a good value to use here. The quotes here are important, otherwise this won't work.

Live Templates are Language Specific

A really nice part of PHPStorm is that whilst more expensive than Webstorm, we do get all of Webstorm's functionality thrown in.

This means we can use PHPStorm for editing JavaScript.

What's nice about this (aside from the obvious) is that we can define Live Templates inside JavaScript files that won't conflict with our PHP files. Indeed, we can define Live Templates for any time of file that won't conflict with PHP files.

This means our Live Templates become context specific - we won't end up accidentally inserting a console.log() statement into our PHP files for example.

I hope you find these as useful as I do. Ultimately you do have to put a little effort in up front, but the productivity pay off is huge in the long term.

Episodes