 [ ← blog / drupal ](/) Aug 29, 2023  ·  \#drupal  ·  3 min read 

# PhpStorm live templates to automate the boring things



I finally took a look at writing [a custom live template with PhpStorm](https://www.jetbrains.com/help/phpstorm/using-live-templates.html). I've used them several times to automate scaffolding a `foreach` statement or other random suggestions that a Tab keypress leads to automated scaffolded code. But I never really dove in to see how they work or could be customized. Then, I had to fix some of my code to comply with Drupal's coding standards.

Drupal requires document comments for all methods, including a short comment.

- When a method overrides a method or implements one from an interface, you use `{@inheritdoc}`, which indicates that the documentation for the method should come from the parent definition.
- For `__construct`, we used a pattern of `Constructs a new $CLASS_NAME object.` as our short comment

Most of the time, I skip these nuanced coding standards until I am happy with my code. Then I toil along copying, pasting, and manually adjusting. Finally, I got sick of it and decided to take 10 minutes to try and automate the dang thing.

To create a live template, [you can follow the documentation](https://www.jetbrains.com/help/phpstorm/creating-and-editing-live-templates.html) or these quick steps:

1. Open settings (`CMD + ,` or `Ctrl + Alt + S`)
2. Select **Editor**
3. Select **Live templates**
4. Click the **+** icon to add a template

     ![Editor | Live templates dialog](/sites/default/files/styles/large/public/2023-08/Screenshot%202023-08-21%20at%2010.26.29%20PM.png?itok=5uw9OmwI) 

 

The kicker was figuring out that you had to assign contexts for live templates. It took me a minute to notice the warning at the bottom of the user interface or the link to open the menu to select a context.

     ![Live template context menu](/sites/default/files/styles/large/public/2023-08/Screenshot%202023-08-21%20at%2010.34.42%20PM.png?itok=6mC2APKD) 

 

## Live template for `{@inheritdoc}`

This one was pretty simple. For the abbreviation, I just used `inheritdoc`. And then for the template:

```php
/**
 * {@inheritdoc}
 */
```

And then, for the contexts, I selected **class members**.

     ![inheritdoc live template configuration](/sites/default/files/styles/large/public/2023-08/Screenshot%202023-08-21%20at%2010.35.29%20PM.png?itok=wsnWFX5P) 

 

When typing code, I only need to type `inheritdoc` and press `Tab` to get my document block for the method.

     ![inheritdoc live template in use](/sites/default/files/styles/large/public/2023-08/Screenshot%202023-08-21%20at%2010.37.33%20PM.png?itok=g2iuJtWL) 

 

## Live template for `__construct`

Creating a live template to create `__construct` comment blocks requires configuring a variable for the template. The format I use is `Constructs a new $CLASS_NAME object.` We need `$CLASS_NAME` to be the value of the current class name.

I couldn't think of a good abbreviation, so I used `cnsdoc` as shorthand for "constructor" and "doc."

The template is:

```php
/**
 * Constructs a new $CLASS_NAME$ object.
 */
```

Variables in live templates start and end with `$`. Once a template has a variable, the **Edit Variables** button becomes active. We have to define what `$CLASS_NAME$` is derived from. The expression is `phpClassName()`.

     ![CLASS&#95;NAME variable definition](/sites/default/files/styles/large/public/2023-08/Screenshot%202023-08-25%20at%204.40.41%20PM.png?itok=A1FH_Qtf) 

 

For the contexts, I selected **class members**.

Now, I can go to my `__construct` method and generate my comment block!

     ![cnsdoc autocomplete](/sites/default/files/styles/large/public/2023-08/Screenshot%202023-08-25%20at%204.43.45%20PM.png?itok=xhCKTV0A) 

 

Here is the result:

     ![Result of cnsdoc](/sites/default/files/styles/large/public/2023-08/Screenshot%202023-08-25%20at%204.44.25%20PM.png?itok=tkccIZqH) 

  

  ![](/themes/custom/mglaman/images/headshot.webp)Thanks for reading

I write about Drupal, PHP, and the quiet infrastructure behind large sites — caching, config, and testing. New posts land every week or two.

 [Subscribe via RSS →](/rss.xml) [About me →](/about-me) 

 



 

  [← Older

How do theme template overrides in Drupal work?

 ](/blog/how-do-theme-template-overrides-drupal-work) [Newer →

My adventure up to Twin Cities DrupalCamp 2023

 ](/blog/my-adventure-twin-cities-drupalcamp-2023)