I’m currently working on a legacy PHP application which has a variety of partial HTML files using this style of code:
I think it looks nicer using the short code version, but only for simple statements like the above. Given a choice, I would rather it looked like this:
Changing it manually is a bit fiddly and error prone, as you can imagine. Moreover, I was rather surprised that PHPStorm doesn’t already cater for this with it’s reformatting rules which I wrote about in a previous blog post.
There is a solution though: CS Fixer, and this brief blog post will show you how to install it in PHPStorm and automatically correct those kinds of code styles. Once installed, you can of course get it to do much, much more.
There are only a few short steps but let’s start with where we’re going to install the fixer. Begin by changing directory to your project root folder. For this, let’s assume mine is:
Now use composer to install CS Fixer:
$ composer require --working-dir=c:/temp/project friendsofphp/php-cs-fixer
I’ve chopped the above output a little for brevity at the three elipsis, of course. Running this will produce much more logging.
We’ve got the fixer installed but need to now tell it how to handle those short tags. For this, create a file named:
.php-cs-fixer.dist.php in the project’s root folder. This configuration was based on the one from the original repo and should be placed inside it:
There’s a lot of information there, but for now, focus your attention on this part:
Inside the rule array, we’ve added one which CS Fixer will recognise related to short tags (
echo_tag_syntax). You can find out the full options on this page, but basically, we are choosing to replace the long form tags with the short, and only in cases where we are simply echoing items.
Everything is ready to use - we just need to tell PHPStorm all about it, which is what we’ll do now.
- Go to settings via
File > Settings...or
- Click on
Tools > External Tools
- Click on the
+icon at the top-left of the panel on the right. The following screen (albeit unfilled) will appear:
Now set the following fields as shown:
Fixes files automatically– not essential
Program> Path to the CS Fixer program. For me, that’s:
--verbose --config=c:\temp\project\.php-cs-fixer.dist.php fix "$FileDir$/$FileName$"
- Finally click
We’re almost done. To make things a little easier, let’s bind some keys to run CS Fixer automatically. In PHPStorm, do the following:
File > Settings...
- Click on
Keymap > External Tools > External Tools > CS Fixer
- Press Enter (or click the pencil icon above)
Add Keyboard Shortcut
- Press your key combination you would like to use:
- I chose
- PHPStorm will warn you if you choose one that is already assigned, though, so press away
- I chose
- Click OK.
Open up a PHP file which uses the long tag echo format, and press your key combo. An output window will pop-up, followed by a lot of verbose output and then CS Fixer will hopefully do its magic!
Here’s some example output when I ran it last.
c:\temp\project\vendor\bin\php-cs-fixer.bat --verbose --config=c:\temp\project\.php-cs-fixer.dist.php fix C:\temp\project\partial.phtml
In this example, I installed CS Fixer into the project folder, but you might choose to install it somewhere centrally for all projects. Just adapt the commands above when you run
composer and remember to point the configuration file parameter to somewhere central, too.
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