Last Updated 2017-04-29
This guide is meant to provide all of the steps necessary to easily get up and running with PHP CodeSniffer, the Joomla Coding Standard ruleset, and Visual Studio Code.
All of the resources used in this guide are linked at the bottom. This guide is also licensed MIT. If you'd like to contribute, then please feel free to open issues or issue pull requests. I'll be happy to merge them and also add your username to CONTRIBUTING.
If you're looking for a corresponding blog post, please see this page.
As always, don't forget to checkout the CHANGELOG to track everything that's changed since the initial release of this guide.
The following steps assume you have PHP installed and globally accessible on your system. You can test this by entering the following command in the terminal:
$ php -v
And you should see something like this:
PHP 5.6.25 (cli) (built: Sep 6 2016 16:37:16)
Copyright (c) 1997-2016 The PHP Group
Zend Engine v2.6.0, Copyright (c) 1998-2016 Zend Technologies
If you're looking for how to use a different version of PHP installed elsewhere on your system, this is not the guide for that. If, however, you're curious as to where the version of PHP you're using is stored, you can enter:
$ which php
And you should see something similar to this:
/usr/bin/php
That should give you enough information for the rest of this guide.
Installing Composer globally means that you'll be able to access it from anywhere on your system (that is, in any directory regardless of where you are). To do this, you can read the manual or follow the quick steps below (which summarize the manual, anyway):
- Grab the latest snapshot of Composer. Save it somewhere you'll remember.
- Move the file you just downloaded to the
/usr/local/bin/
directory on your machine.
To do this, open your terminal navigate to the directory where you downloaded composer.phar
. Move the file to the diretory mentioned above by issuing the following command:
$ mv composer.phar /usr/local/bin/composer
And now you can access Composer from anywhere in your system. To do try it out, enter the following command in your terminal:
$ composer about
You should see something like this:
Composer - Package Management for PHP
Composer is a dependency manager tracking local dependencies of your projects and libraries.
See https://getcomposer.org/ for more information.
With Composer globally installed, you can now install the Joomla Coding Standards rules.
For the purposes of this document, we're installing PHP CodeSniffer on a project-by-project basis. To do this, we're going to be using Composer.
From the integrated terminal within Visual Studio Code, enter the following command:
$ composer require "squizlabs/php_codesniffer=^2.8.1"
This will create composer.json
, tell it where to locate the PHP CodeSniffer, and install it in a vendor
directory. Once this is done, we need the Joomla Coding Standard ruleset.
I recommend placing the rules in a directory you can refer to often. Personally, I use a projects
directory in my Dropbox directory to manage all of my work because it obviously provides backups automatically (and no, I don't recommend storing secure files there).
From within your directory of choice, say dropbox/projects
, enter the following command in the terminal:
$ composer create-project joomla/coding-standards:dev-master --no-dev
This will create a coding-standards
directory in your projects
directory and it makes it esay to tell each project where the Joomla Coding Standards are stored because, remember, we'll be using Composer on a project-by-project basis.
From within Visual Studio's integrated terminal, make sure that you're in your project's directory and then issue the following command:
$ ./vendor/bin/phpcs --config-set installed_paths /path/to/dropbox/projects/coding-standards
And this will tell your project's copy of PHPCS where the Joomla Coding Standards are.
Finally, we need to let Visual Studio what we're going to be using to sniff out the code in our project and what rules to use. This is really easy to do. In Visual Studio, hit the ⌘,
(or whatever your operating system uses) command to open settings.json
.
Make sure the file looks like the following (though you may need to tweak based on your existing settings)
// Place your settings in this file to overwrite the default settings
{
// PHPCS
"phpcs.enable": true,
"phpcs.standard": "Joomla",
}
And this will enable PHPCS and will also tell it to use the standard Joomla ruleset. If this doesn't start working on the code your have automatically, then restart Visual Studio Code.