The PHP Code Sniffer helps identity violations again coding standards (ie. PSR) as well as syntax errors in your code. The following tutorial explains how to setup the PHP Code Sniffer in Sublime Text (on OSX). This has been tested in Sublime Text 2 and Sublime Text 3.
You can use Pear, but that's just confusing, and requires you to have Pear installed. Move on to the next option.
I much prefer to install it as a global Composer dependency. This is also a great way to install other global dependencies like PHPUnit. From Terminal, run:
composer global require 'squizlabs/php_codesniffer=*'
This will install phpcs
in your ~/.composer/vendor/bin
folder.
If you'd like all your global Composer dependencies available without the full path (which I recommend), add ~/.composer/vendor/bin/
to your PATH. For those who don't know how to do that, here's how in Terminal:
# Go to your home folder
cd ~
# Edit your bash profile
vim .bash_profile
# Add the following to the bottom of .bash_profile:
PATH=~/.composer/vendor/bin:$PATH
export PATH
Now close Terminal and reopen it (required to enable the bash changes). Test this by running:
# Get the current phpcs version
phpcs --version
# Should output something like:
# PHP_CodeSniffer version 1.4.7 (stable) by Squiz (http://www.squiz.net)
This is the easy part, assuming you have the package manager installed. In Sublime Text run the command palette (Shift-Command-P) > Install Package > Phpcs.
Once completed, edit the package user settings (Sublime Text > Preferences > Package Settings > PHP Code Sniffer > Settings - User). Add the following:
Note: JSON "comments" do not show up properly on Github, but they do work fine in Sublime Text. Start by copying and pasting. ;)
{
// Set this to your full local PHP path
// On Mavericks, PHP 5.4 is available here at "/usr/bin/php"
// If you run MAMP set this to "/Applications/MAMP/bin/php/php5.4.19/bin/php"
// You will have to change the PHP version accordingly
"phpcs_php_prefix_path": "/usr/bin/php",
// Set this to your full local phpcs path
// We know this is available at ~/.composer/vendor/bin/phpcs
// However, that will not work, you cannot use the "~"
// Get the real path by running "which ~/.composer/vendor/bin/phpcs" from Terminal
// It should look something like this:
"phpcs_executable_path": "/Users/jonathan/.composer/vendor/bin/phpcs",
// The default coding standard is PEAR
// I use PSR2 (see below)
// To see a list of available coding standards, run "phpcs -i" from Terminal
"phpcs_additional_args": {
"--standard": "PSR2",
"-n": ""
},
// Again, set this to your full local PHP path
// See above instructions
"phpcs_php_path": "/usr/bin/php"
}
Save, restart Sublime Text, and it should now be working!
To test that it's actually working in Sublime Text, create a new PHP file, add a syntax error, and save it to your desktop. Note, it has to be set as a PHP file in Sublime Text, otherwise code sniffing will not occur. Either save it as a .php file, or set the syntax to PHP. Here is a failing code example:
<?php
fail
This should throw a parse error.
Next, change the file to something non-PSR2 compliant. For example:
<?php
function test(){}
Hit save, and you should get some complaints about braces.
Done!
I know this is a bit old but thanks! I did something different for #1. Instead of using
.bash_profile
, I made a symlink:ln -s ~/.composer/vendor/bin/phpcs /usr/local/bin/phpcs