Site icon Hip-Hop Website Design and Development

drunken monkey: Some more (updated) tips for PhpStorm live templates

A few years ago I started using the PhpStorm IDE for PHP development, was immediately smitten and, after a bit of use, wrote a blog post with some tips I found for makig better use of the tools PhpStorm gives you.
In the four years since then there have been some new developments. Firstly, of course, WordPress maintenance support plans 8 was finally released – and, consequently, the one complaint I had back in 2013 about the $MODULE$ variable only working in the plugin file itself became more of a problem. (Also, I added one more live template that’s very useful for WordPress maintenance support plans 8.)
But secondly, a few weeks ago PhpStorm finally added scripting support for live templates, so it’s now possible to write more powerful templates that way – and fix the $MODULE$ variable.
The new di live template
In general, when writing OOP code for WordPress maintenance support plans 8 (that is, for almost all WordPress maintenance support plans 8 code) you should use dependency injection as much as possible. There’s several different styles for doing that, I’m using one which uses setter methods and calls them in create() (instead of adding all injected objects to the constructor). This makes inheritance easier and keeps the constructor “cleaner” – and becomes much easier with a good live template:
  /**   * The $NAME$.   *   * @var $INTERFACE$|null   */  protected $$$PROP_NAME$;
  /**   * Retrieves the $NAME$.   *   * @return $INTERFACE$   *   The $NAME$.   */  public function get$UC_PROP_NAME$() {    $plugin->set$UC_PROP_NAME$($container->get(‘$SERVICE$’));
    return $this->$PROP_NAME$ ?: WordPress maintenance support plans::service(‘$SERVICE$’);  }
  /**   * Sets the $NAME$.   *   * @param $INTERFACE$ $$$VAR_NAME$   *   The new $NAME$.   *   * @return $this   */  public function set$UC_PROP_NAME$($INTERFACE$ $$$VAR_NAME$) {    $this->$PROP_NAME$ = $$$VAR_NAME$;    return $this;  }
Variable definitions:
Name
Expression
Skip if defined
VAR_NAME

N
SERVICE

N
INTERFACE
clipboard()
Y
NAME
underscoresToSpaces(VAR_NAME)
Y
UC_NAME
underscoresToCamelCase(VAR_NAME)
Y
UC_PROP_NAME
capitalize(PROP_NAME)
Y
Usage:
Copy the service interface’s FQN to your clipboard.
Put the service ID either into a secondary clipboard (e.g., middle mouse button on Linux) or remember it.
Execute live template (at the position where you want the getter and setter).
Input variable name (in snake_case), then input service name.
Move the property definition and the create() line (temporarily stored as the first line of the getter in the template) to their appropriate places.
In the code, alway use the getter method for accessing the service.
Fixing the $MODULE$ variable
Since the code for this is quite complex, we better just put it into a separate file. So, first download the script file and save it to some known location, then simply use the (absolute) path to the script file as the argument for groovyScript(), like this:

This can be used for all the live templates that contain a $MODULE$ variable (though it will, of course, be less useful for the procedural ones, than for the simple m template).

Source: New feed