Essential Visual Studio Code Extensions for PHP Developers
Visual Studio Code is hot stuff right now and is quickly becoming the editor of choice for developers worldwide. It’s available for Mac, Windows and Linux and best of all it’s free.
Personally I was a little skeptical when I first learnt of Microsoft’s latest editor. I have been a long time user of Sublime Text, but VS Code has gradually won me over. The automation it offers with code completion as well as it’s superb git and debugging integration is a real winner. You can even trigger a terminal directly from within VS Code which is great for when you need to do something on the command-line without breaking out of the editor; I find this really helps with my workflow.
A few years ago I wrote about the Sublime Text plugins I considered essential for PHP developers. Now that I am increasingly choosing to load up VS Code instead I thought it was time to share my essential Visual Studio Code extensions for PHP developers. So here are my favourite extensions for improving productivity when writing PHP code.
This is the must have extension for PHP development and goes a long way to making Visual Studio Code feel more like a traditional IDE. PHP Intelephense will massively improve your efficiency by offering intelligent code completion, workspace wide definition support, parameter help for function calls and a whole lot more.
I’ve been using this extension for a while now and have been really impressed by what it offers. It is very rapid. At the time of writing it has an impressive 5 out of 5 rating. If you don’t install any other extension from this post, at least make sure you add this one to your editor.
PHP Mess Detector
This extension is a great way of monitoring your code and trying to keep it lean and bug free. PHP Mess Detector will analyse your code as you type and flag potential bugs and unused code. It will also help you identify overcomplicated expressions and suboptimal code. Unlike some of the following extensions there is no need to install anything extra as long as you have PHP installed, but you can easily customise the rules it runs. It also supports custom rules.
Well written code should use a consistent coding standard, i.e. type of indentation, naming conventions, use of whitespace, bracket placement, etc.. Whatever standard you are working to, phpcs will help you to conform to them. To use this extension you’ll also need to ensure you’ve got PHP CodeSniffer installed; this can be easily done using Composer:-
composer global require squizlabs/php_codesniffer
If you work with more than one coding standard between projects then you’ll want to configure each workspace with the relevant standard. This is easily done by setting
phpcs.standard in your workspace’s settings.
PHP CS Fixer
So following on from phpcs, if we’re conforming to some coding standards it can also be useful to be able to quickly fix the formatting of code. That’s where PHP CS Fixer comes in handy. This provides a command that can be run to fix any breaches in standards which can also be switched on to automatically run on save. Like phpcs you will need to have PHP CS Fixer installed on your machine in addition to this Code extension.
This next one may seem a little similar to phpcs, but seeing as many open source projects use this I feel it is well worth having installed too. EditorConfig provides a way of maintaining some coding style consistency between different editors and IDEs. This is particularly valuable when collaborating with others, some of whom may not be using Visual Studio Code to code with.
To use EditorConfig an .editorconfig file is placed in the root of a project and usually committed to the project’s repository so everyone working on it conforms to the rules. This is useful for defining tab styles and line endings among other things. Most editors have plugins available to support EditorConfig including Atom, PhpStorm, Sublime Text and now Visual Studio Code.
You can find out more about EditorConfig and what you can define in the .editorconfig file over on the official website. Details of the Visual Studio Code EditorConfig extension can be found on the Visual Studio Marketplace.
Good code should be well documented. PHPDoc is a pretty much universally accepted means of doing this in PHP; although many developers find documenting their code tedious. PHP DocBlocker simplifies the process of adding docblock comments by autocompleting
/** by pressing Enter or tab taking away some of the pain. This extension really helps by partially automating the process of documenting your functions.
I’m an advocate for testing code. I’ve spoken on the topic twice this year at user groups in Sheffield and Leeds, and written a blog on the topic of PHPUnit. So being able to run tests directly from within my editor is super useful. Better PHPUnit not only makes it easy to quickly run tests, but makes running specific tests as simple as placing the cursor within the test method.
If you unit test, then this is one for you.
If you’re still debugging your code by adding
die statements here and there along with some
print_r calls, stop! Install XDebug and then grab this excellent extension for VS Code. PHP Debug will enable you to add breakpoints to your code so that you can pause the running of your app and investigate the variables currently set before proceeding to the next breakpoint. This all integrates nicely with VS Code’s built in debug mode.
Breakpoints offer greater control of how you debug, without littering your codebase with debugging methods. Once you start debugging like this you will wonder how you put up with all those
die statements for so long.
Visual Studio Code already comes with some great Git integration right out of the box, but GitLens takes it to the next level. This extension adds a lot of functionality to your editor with the aim of helping you gain a better insight into the history of your repositories. It adds a Git explorer for navigating your branches, some powerful comparison commands, inline git blame annotations and an easy to navigate file history panel among many other features.
GitLens is very customisable. It’s well worth loading up the extension’s settings after installing it as you may not want everything enabled to begin with. Personally I find the Code Lens a little distracting so disabled this early on.
After PHP Intelephense this is probably my next must have extension.
Markdown All in One
As alluded to previously, documentation is an important part of dev work. It’s always a good idea to have a readme file in the root of each of your projects. Markdown has become a familiar standard for these files.
So my last essential VS Code extension for PHP developers is Markdown All in One. It provides some helpful keyboard shortcuts, a live preview and many more features to aid you when writing in Markdown. It even supports GitHub flavoured Markdown which is great for those open-source projects you are working on.