Setting Up PHP CodeSniffer in Sublime Text 3 for CakePHP and Other Standards

This is a kind of follow-up to my recent article on Essential Sublime Text 3 Plugins for PHP Developers to explain how to install and configure Sublime-phpcs for the CakePHP code standard. Although I’m describing how to configure phpcs specifically for Cake it will hopefully be of use to anyone wanting to get started with phpcs in Sublime Text and start developing better code.

I’m going to assume you’re familiar with Composer and have it already installed.

Why Use a Code Sniffer

Code sniffers check your code against a predefined coding standard. There are many coding standards out there like PSR-2, Zend and CakePHP PSR. Using a coding standard and code sniffer can help achieve the following:-

  • Consistency in code, which in turn reduces distractions
  • Warns against potential bug resulting typos
  • More maintainable code
  • Reduces version control merge conflicts
  • Can generally help you write better code

Install the CakePHP Code Sniffer

So let’s get started. First we need to install a sniffer for CakePHP PSR using Composer.

For CakePHP 3:-

composer global require --dev cakephp/cakephp-codesniffer:2.*

CakePHP 3 uses PSR-2 compatible rulesets, but has some additional rules for documentation and naming conventions.

For CakePHP 2:-

composer global require --dev cakephp/cakephp-codesniffer:1.*

The sniffer for CakePHP 2 uses an older version of phpcs; Composer will handle this for you.

After installing cakephp-codesniffer via Composer, phpcs should also have been installed as it is a requirement of the CakePHP code sniffer. You should find it in your (global) composer directory in vendor/bin (CakePHP 2 users will probably have to look in vendor/squizlabs/php_codesniffer/scripts for the executable); locate it and test it is working by running phpcs -i:-

~/.composer/vendor/bin/phpcs -i

This should return all the installed coding standards that phpcs is currently configured for:-

The installed coding standards are MySource, PEAR, PHPCS, PSR1, PSR2, Squiz and Zend

You’ll notice that CakePHP is not listed as one of the coding standards despite having just installed it. We need to let phpcs know about the new CakePHP code sniff (make sure you update the path to cakephp-codesniffer to where it is located on your own machine):-

~/.composer/vendor/bin/phpcs --config-set \
installed_paths ~/.composer/vendor/cakephp/cakephp-codesniffer

Running phpcs -i again should now show CakePHP as one of the available coding standards.

Install phpcs

If you’ve just followed the installation instructions for the CakePHP code sniffer then you should have phpcs already installed and can skip to the next section. Otherwise, one of the easiest way of installing phpcs is via Composer:-

composer global require squizlabs/php_codesniffer=*

Install the phpcs Sublime Text Package

Using Package Control in Sublime Text search for phpcs and install it. If you’ve not got Package Control installed go get it first. It’s one of my essential Sublime Text packages and makes installing other packages incredibly simple.

With the phpcs Sublime Text package installed the next thing to do is configure it…

Configuring phpcs in Sublime Text

In Sublime Text open the default preferences for PHP Code Sniffer and copy these to the PHP Code Sniffer User Preferences. We’ll be editing the user preferences for the package and keeping the defaults as a backup/reference.

The first thing we need to do is tell Sublime-phpcs where the PHP Code Sniffer is on our computer:-

"phpcs_executable_path": "/home/andy/.composer/vendor/bin/phpcs",

This needs to be the full absolute path to the phpcs executable. If you’ve used another method for installing phpcs OSX and Linux users can use which phpcs to try and locate it on your system.

We want to set our code standard to sniff for CakePHP (or which ever standard you are using):-

"phpcs_additional_args": {
    "--standard": "CakePHP",
    "-n": ""
},

If you now go and edit a PHP file you should see phpcs run when you save with the quick panel popping up with any errors it found. The idea of this is to help you navigate to the errors so that you can fix them. This can be either really useful or just an annoyance, You can disable it with this setting:-

"phpcs_show_quick_panel": false,

If disabled the errors will still get highlighted in the file and clicking on them will reveal the reported error in the status bar at the bottom of the window.

If it looks like phpcs is not running then there’s a handy debug setting for the package that you can enable:-

"show_debug": true,

Set this to true and then open the Sublime Text Console (‘Show Console’ in the ‘View’ menu). Try saving a PHP file and you should see any errors with the package listed. It’s likely that it can’t find the phpcs executable.

Changing the Coding Standard

I tend to work with various coding standards depending on the project. Obviously for CakePHP code I use Cake’s own standard, but if it’s a Laravel project I use PSR-2 (which comes preinstalled with phpcs). To change the standard you can either edit the package preferences or use ‘PHP Code Sniffer > Switch coding standard’ by right-clicking in a file.

PHP Linter

As well as checking for coding standards it’s also useful to check the syntax for bugs. You can enable the phpcs package to provide an interface for php -l:-

"phpcs_linter_run": true,

If you’re already using a linting package like SublimeLinter you probably want this disabled.

PHP Mess Detector

PHP Mess Detector (phpmd) checks your source code for overly complex functions/methods; if it finds issues it will let you know so that you can examine your code and make improvements.

If you’ve not already got it installed you can download it using Composer:-

composer global require --dev phpmd/phpmd

You’ll then need to tell Sublime Text where the phpmd executable is (pretty much the same thing as we did with phpcs):-

"phpmd_executable_path": "/home/andy/.composer/vendor/bin/phpmd",

Final Words

There’s a great deal of value in using a coding standard and the use of phpcs makes sticking to one a lot easier. Integrating it with Sublime Text does take a few steps, but once you’ve got it setup you’re well on your way to writing better quality code.

Happy coding!

Related Content

Published on


Comments

  1. Apostol |

    Hi there, I followed the tutorial, but when I hit save in a php file I get that in the sublime console:
    http://pastebin.com/KqeriJ9T
    Any ideas?

Leave a Comment
  • You will need to preview your comment before you can submit it.