PhpStorm Live Template & Postfix Snippets

Welcome to my collection of PhpStorm tricks!

This site hosts a collection of live template snippets and postfix completion templates to supercharge your development in PhpStorm.

Copy and paste Live Templates in PhpStorm to install them.

You'll need to use the Custom Postfix Template plugin for the postfix templates.

Use the tags to see related snippets. Some snippets are best used with others !

All Snippets

asseq

Generate a call to assert equals

asseq
.asseq : Assert equals
ANY → \$this->assertEquals($expectedVar#1:complete()$, $expr$);

Custom Postfix Template

asseq

Generate a call to assert equals already calling expected and actual variables.

asseq
<template name="asseq" value="$$this-&gt;assertEquals($$expected$VAR1$, $$actual$VAR2$);" description="" toReformat="true" toShortenFQNames="true">
<variable name="VAR1" expression="complete()" defaultValue="" alwaysStopAt="true" />
<variable name="VAR2" expression="complete()" defaultValue="" alwaysStopAt="true" />
<context>
<option name="PHP Statement" value="true" />
</context>
</template>

av

Generate an actual variable for tests.

av
<template name="av" value="$$actual$COMPLETE$ = $EXPECTEDVALUE$;" description="Actual variable assignation" toReformat="true" toShortenFQNames="true">
<variable name="EXPECTEDVALUE" expression="complete()" defaultValue="" alwaysStopAt="true" />
<variable name="COMPLETE" expression="capitalize(regularExpression(phpSuggestVariableName(), &quot;\\$&quot;, &quot;&quot;))" defaultValue="" alwaysStopAt="true" />
<context>
<option name="PHP Expression" value="true" />
</context>
</template>

av

Generate an actual variable for tests.

av
.av : Create an expected variable
ANY -> \$actual$var:capitalize(groovyScript("if(_1) { _1.substring(1) } else { _1 }", phpSuggestVariableName()))$ = $expr$;$END$

Custom Postfix Template

build

Generate the build method for builders.

build
<template name="build" value="public function build(): $TYPE$&#10;{&#10;    return new $TYPE$($PARAMETERS$$END$);&#10;}" description="Build method" toReformat="true" toShortenFQNames="true">
<variable name="TYPE" expression="" defaultValue="regularExpression(phpClassName(), &quot;Builder&quot;, &quot;&quot;)" alwaysStopAt="false" />
<variable name="PARAMETERS" expression="complete()" defaultValue="" alwaysStopAt="true" />
<context>
<option name="PHP Class Member" value="true" />
</context>
</template>

Live template

$date$

Get the current date.

$date$
<template name="$date$" value="'$date$'$END$" description="Generate the current date" toReformat="false" toShortenFQNames="true">
<variable name="date" expression="date(&quot;yyyy-MM-dd'T'HH:mm:ss.SSSz&quot;)" defaultValue="" alwaysStopAt="false" />
<context>
<option name="PHP Expression" value="true" />
</context>
</template>

Live template

ev

Generate an expected variable for tests

ev
<template name="ev" value="$$expected$COMPLETE$ = $EXPECTEDVALUE$;" description="Expected variable assignation" toReformat="false" toShortenFQNames="true">
<variable name="EXPECTEDVALUE" expression="complete()" defaultValue="" alwaysStopAt="true" />
<variable name="COMPLETE" expression="capitalize(regularExpression(phpSuggestVariableName(), &quot;\\$&quot;, &quot;&quot;))" defaultValue="" alwaysStopAt="true" />
<context>
<option name="PHP Expression" value="true" />
</context>
</template>

ev

Generate an expected variable for tests

ev
.ev : Create an expected variable
ANY -> \$expected$var:capitalize(groovyScript("if(_1) { _1.substring(1) } else { _1 }", phpSuggestVariableName()))$ = $expr$;$END$

Custom Postfix Template

fbuild

Generate the encapsulating function for builders.

fbuild
<template name="fbuild" value="function a$FUNCTIONAME$(): $TYPE$ {&#10;&#9;return new $TYPE$();&#10;}" description="function encapsulating a builder" toReformat="true" toShortenFQNames="true">
<variable name="TYPE" expression="complete()" defaultValue="" alwaysStopAt="true" />
<variable name="FUNCTIONAME" expression="regularExpression(TYPE, &quot;Builder&quot;, &quot;&quot;)" defaultValue="" alwaysStopAt="true" />
<context>
<option name="PHP Statement" value="true" />
</context>
</template>

Live template

fclass

Generate a final class.

fclass
<template name="fclass" value="final class $CLASS_NAME$ {&#10;&#9;$END$&#10;}" description="final class" toReformat="false" toShortenFQNames="true">
<variable name="CLASS_NAME" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="PHP" value="true" />
</context>
</template>

Live template

ivo

Generate the body for a integer value object class.

ivo
<template name="ivo" value="&#10;    private $$$FIELD_NAME$;&#10;&#10;    public static function fromInt (int $$$FIELD_NAME$): $TYPE$ { return new $TYPE$($$$FIELD_NAME$); }&#10;&#10;    private function __construct (int $$$FIELD_NAME$) { $this-&gt;$FIELD_NAME$ = $$$FIELD_NAME$; }&#10;&#10;    public function __toString (): string { return (string) $this-&gt;$FIELD_NAME$; }" description="Simple Integer Value Object" toReformat="true" toShortenFQNames="true">
<variable name="TYPE" expression="phpClassName()" defaultValue="" alwaysStopAt="false" />
<variable name="FIELD_NAME" expression="camelCase(TYPE)" defaultValue="" alwaysStopAt="false" />
<context>
<option name="PHP Class Member" value="true" />
</context>
</template>

Live template

ret

Return current expression

ret
.ret : Return expression
ANY -> return $expr$;

Custom Postfix Template

svo

Generate the body for a string value object class.

svo
<template name="svo" value="&#10;    private string $$$FIELD_NAME$;&#10;&#10;    public static function fromString (string $$$FIELD_NAME$): $TYPE$ { return new $TYPE$($$$FIELD_NAME$); }&#10;&#10;    private function __construct (string $$$FIELD_NAME$) { $this-&gt;$FIELD_NAME$ = $$$FIELD_NAME$; }&#10;&#10;    public function __toString (): string { return $this-&gt;$FIELD_NAME$; }" description="Simple String Value Object" toReformat="true" toShortenFQNames="true">
<variable name="TYPE" expression="phpClassName()" defaultValue="" alwaysStopAt="false" />
<variable name="FIELD_NAME" expression="camelCase(TYPE)" defaultValue="" alwaysStopAt="false" />
<context>
<option name="PHP Class Member" value="true" />
</context>
</template>

Live template

@t

Generate an empty PhpUnit test with the @test annotation.

@t
<template name="@t" value="/**&#10;* @test&#10;*/&#10;public function $NAME$(): void {&#10;    $END$&#10;}" description="Add a test function" toReformat="true" toShortenFQNames="true">
<variable name="NAME" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="PHP Class Member" value="true" />
</context>
</template>

@tl

Generate an empty PhpUnit test with the @test annotation and a fake name.

@tl
<template name="@tl" value="/**&#10;* @test&#10;*/&#10;public function TODO_RENAME_$LINE_NUMBER$(): void {&#10;    $END$&#10;}" description="Add a lazy named test" toReformat="true" toShortenFQNames="true">
<variable name="LINE_NUMBER" expression="lineNumber()" defaultValue="" alwaysStopAt="false" />
<context>
<option name="PHP Class Member" value="true" />
</context>
</template>

@ts

Generate an empty skipped PhpUnit test with the @test annotation.

@ts
<template name="@ts" value="/**&#10;* @test&#10;*/&#10;public function $NAME$(): void {&#10;&#9;$this-&gt;markTestSkipped('Not implemented yet.');&#10;}&#10;&#10; $END$" description="Add a test function" toReformat="true" toShortenFQNames="true">
<variable name="NAME" expression="" defaultValue="" alwaysStopAt="true" />
<context>
<option name="PHP Class Member" value="true" />
</context>
</template>

$uuid$

Generate an UUID.

$uuid$
<template name="$uuid$" value="'$uuid$'$END$" description="Generate an UUID" toReformat="false" toShortenFQNames="true">
<variable name="uuid" expression="groovyScript(&quot;UUID.randomUUID().toString()&quot;) " defaultValue="" alwaysStopAt="false" />
<context>
<option name="PHP Expression" value="true" />
</context>
</template>

Live template

with

Generate a "wither" method for builders.

with
<template name="with" value="public function with$NAME$($PARAMETER_TYPE$ $$$PARAMETER_NAME$): self&#10;{&#10;&#9;$clone = clone $this;&#10;&#9;&#10;&#9;$clone-&gt;$PARAMETER_NAME$$END$ = $$$PARAMETER_NAME$;&#10;&#9;&#10;&#9;return $clone;&#10;}" description="Create a wither" toReformat="false" toShortenFQNames="true">
<variable name="PARAMETER_TYPE" expression="complete()" defaultValue="" alwaysStopAt="true" />
<variable name="PARAMETER_NAME" expression="complete()" defaultValue="" alwaysStopAt="true" />
<variable name="NAME" expression="" defaultValue="capitalize(PARAMETER_NAME)" alwaysStopAt="false" />
<context>
<option name="PHP Class Member" value="true" />
</context>
</template>

Live template