How often have you been working on a project and had the nuances of someone else’s style complicate your workflow? Too often if I ask myself. This is why my teams now are almost all universally using phpStorm, and why I absolutely love code styles.
Generally speaking, PSR-2 is a great standard and if it weren’t for a half-dozen or so things I disagree with I would definitely support it. However, I cannot get on board with supporting the standard for conditionals, try/catch, and other minutiae. Fundamentally I agree with 80% or more of the standard and fully support it’s usage and implementation globally 100% but in my teams we use a slightly modified version that improves readability slightly and spacing of code quite a bit.
For anyone interested I’ve included that here: https://github.com/litwicki/phpstorm
- Code style schemes
- Default project settings
- File types
- General settings
- UI Settings
litwicki.jar.zip and unzip)
- Open phpStorm
- File > Import Settings
- Browse to and select
- phpStorm > Preferences > Code Style
- Select the
- Click Apply
phpStorm has a wonderful feature called Live Templates that lets you quickly and easily define code templates (what I usually refer to as a snippet) for code you frequently have to write. For example:
- Foreach loops on a standard array of entities ($entities)
- Symfony isGranted checks via security.context
- Getters or setters that aren’t for standard properties
- Try/Catch blocks
And the list goes on..
Fortunately in phpStorm we can make this incredibly quick and easy:
Open phpStorm, and select phpStorm > Preferences from the menu. Within preferences, select Live Templates about 1/3 from the bottom.
Next, we need to add the new template (snippet) to the selected context. Select the language/environment you want to add to. Now click the “+” and then “Live Template” to create a new live template for the selected section.
Now define your template. Select the languages it applies to, and give it an abbreviation and a description. The abbreviation will also map the keystrokes you use later to access this template.
The easy part is writing the snippet itself. This example is an Entity class with a standard $id property and getter.
When you’ve saved from the previous step, all that’s left is to access the template in your files. In this example, I’ve selected PHP so all phpStorm looks for is the <?php tag to define the file as part of the PHP context. Once done, you simple click CMD+J and begin typing the abbreviation you used earlier. Select the template when you’ve found it, and voila.