TL;DR: There are a few PHP Code Sniffer extensions for Visual Studio Code. The one I prefer is PHP Sniffer & Beautifier by Samuel Hilson. Here’s where to get it and how to configure it.


Though this isn’t directly related to the material I’m writing about in my series on Ray on WordPress, it’s relevant enough to share at this point because:

  • the series is only going to include more code and i use this extension for writing said code,
  • over the last few months, I’ve found this extension to be really good in comparison to others that are available.

There are some other ones out that that are really good, and I’ve used them, but this is the one I’ve settled on using.

PHP Sniffer & Beautifier for Visual Studio Code

As mentioned, I used to use a different extension and I wrote about it some time ago. But as time went on and PHP CodeSniffer continued to evolve, I wanted an extension that kept up with it and with how I work in my projects.

So now, I use something different. I recommend it, of course, but as the saying goes: Your mileage may vary.

PHP Sniffer & Beautifier

If you want to clone the repository directly, you can do so from GitHub; however, when it comes to Visual Studio Code extensions, I’m a fan of installing them from directly within the IDE.

So if you click on the extensions icon in the left pane and search for PHP Sniffer & Beautifier it should come up quickly in the results. Note this extension is authored by Samuel Hilson.

This linter plugin for Visual Studio Code provides an interface to phpcs & phpcbf. It will be used with files that have the “PHP” language mode. This extension is designed to use auto configuration search mechanism to apply rulesets to files within a workspace. This is useful for developers who work with many different projects that have different coding standards.

visual studio code marketplace

I can’t speak to the quality of other extensions and the configuration options I’m going to share will not work with others (and if they do, it’s sheer luck).

Installing PHP CodeSniffer

Before configuring the aforementioned extension, make sure you have PHP Code Sniffer installed via Composer. You can do this easily with the require-dev directive:

"require-dev": {
  "squizlabs/php_codesniffer": "^3.5"
},

After that, run the usual $ composer install command (or $ composer update depending on where you are in your project) and you should be good to go.

Configuring PHP Sniffer & Beautifier

Next, to take advantage of the extension along with the code sniffer, you’ll need to define a few settings. You can do this at a project level or at the IDE level. It doesn’t really matter as the values will be the same – it’s just in which file you opt to place them. 🙂

"phpsab.executablePathCBF": "./vendor/bin/phpcbf",
"phpsab.fixerEnable": true,
"phpsab.snifferShowSources": true,
"phpsab.standard": "PSR12",

Note the above options:

  • executablePathCBF tells the extension for phpcbf is located. The path is relative and if you install PHP CodeSniffer via Composer then it will be in your project’s vendor directory.
  • fixerEnable allows us to run CBF within the IDE. I recommend this as it makes resolving the errors much easier.
  • snifferShowSources is important because you can see where the source of the problem lies. This helps not only in identifying it but also in helping you to learn from your mistakes. That is, if you make mistakes. 🙃
  • standard simply tells the extensions which standard by which you want to evaluate your code. So if you have WP Coding Standards installed, use that. If you have another standard to use, use that.

There are other options that you can define and they are well-documented.

Other Things

There are other configuration options you can add that will make your life easier. The ones above are the ones I find most useful and the ones that provide a great foundation off of which to write PSR12 – or whatever standard you opt to use – code along with the package installed via Composer.


⚠️ There’s an important note about this extension, though, and it’s worth pointing out here before closing. From the repository page:

A while ago I moved on from php and now work in dotnet. I do currently have the bandwidth to work on this project. As far as I can tell this project is still fully functional in its current state.

Github

It’s disappointing in so far so this has been my favorite extension for sometime but perhaps someone will come in and take over it or a previous extension will end up suiting me better. And if that’s the case, then I’ll be happy to share more about it here.