Using Editor Config to Help Maintain PSRs in PHP

There are two PHP-FIG PSRs (PHP Standards Recommendations) named PSR-1 and PSR-2 which make suggestions as to how you should present your PHP code. I won’t be going into why you should follow standards but it is always nice when we can get our tools to do much of the heavy lifting to help us. Of course, many editors already allow you lots of customisation so you can often set preferences to enforce these, but there is an alternative named Editor Config which I am going to explain here. This tool has a simple approach which involves using a small text configuration file in your project directory hierarchy that contains directives for how to handle things such as line endings, character sets and other useful common features. The nice thing is that many editors either recognise it natively or can do so using a small plugin. For this example, I am going to be using PHP Storm (which requires a plugin) and creating a basic config file which handles some of the standards of the two PSRs I mentioned. Specifically:

PSR-1

  • Files MUST use only UTF-8 without BOM for PHP code.

PSR-2

  • All PHP files MUST use the Unix LF (linefeed) line ending.
  • All PHP files MUST end with a single blank line.
  • There MUST NOT be trailing whitespace at the end of non-blank lines.
  • Code MUST use an indent of 4 spaces, and MUST NOT use tabs for indenting.
  • All PHP files MUST end with a single blank line.

First, crack open a text editor (any will do) and create a file named “.editorconfig“, saved in the root of your PHP project. Next, add the following:

charset: utf-8
end_of_line: lf
trim_trailing_whitespace: true
indent_style: space
indent_size: 4
insert_final_newline: true

The above is all self-explanatory but there are a couple of other features we can enable which make this even more useful.

Using Multiple Config Files

It is possible to use more than one config file. If you place a “.editorconfig“ in one of your subdirectories, it will use that to enfore the standards you specify on the files at that location. Another cool thing is that if it can’t find any config files in that directory, it will head on up the directory tree till it finds one which brings us to this option:

root: true

If you place that in your file, that will stop it dead in its tracks and it won’t look any further.

Specifiying File Types

Using our above example means that even if we don’t edit PHP files, we will still employ these settings. By using a file wildcard just above the setting group on its own line, we can ensure that this only applies to PHP files like so:

[*.php]
charset: utf-8
end_of_line: lf
trim_trailing_whitespace: true
indent_style: space
indent_size: 4
insert_final_newline: true

Installing the PHP Plugin for Editor Config

Firstly, go here for details about how to install (and remove) plugins. The one you will want to use however can be found here. That’s it! You are now ready to be standards compliant (almost!)


Hi! Did you find this useful or interesting? I have an email list coming soon, but in the meantime, if you ready anything you fancy chatting about, I would love to hear from you. You can contact me here or at stephen ‘at’ logicalmoon.com