Making PHP CodeSniffer use JSLint via Rhino
Posted on April 3rd, 2011 by Neil Crosby. Filed under Blog Posts.
Last week at work, we started writing some JavaScript for the project we’re working on. Up until now we’ve been very much focusing on core functionality, but we’re at the stage now where writing some JavaScript makes sense. One of the things that we’ve been doing up un til this point, and has been making sense for us, is to use PHP Code Sniffer to help make sure that the PHP and CSS we’re writing adheres to known standards and has a maintainable shape.
So, since PHP Code Sniffer can also run JSLint, we thought we’d turn on JavaScript sniffing as well. It all sounds so easy, doesn’t it?
Ultimately, it is pretty easy to get PHP Code Sniffer running JSLint, but there were a whole bunch of hurdles that ended up taking me a couple of days to get over. So, I thought a blog post was in order. Here’s what you need to do:
Have a working installation of Java.
Have a working copy of PHP Code Sniffer, installed via PEAR.
Have a working installation of Rhino.
Depending on your system you’ll want to do one of two things:
The simplest option is to install a Rhino binary, by doing something like the following on your system:
apt-get install rhino
This will put a
rhino
script on your executable path, and you’re good to go.Alternatively, you’ll need to download Rhino from Mozilla’s website. Unfortunately, this download doesn’t contain a
rhino
passthrough script (which PHP Code Sniffer will later need), so we’ll need to create one. Thankfully it’s pretty simple, and can look like this:#!/bin/sh java -jar `dirname $0`/js.jar $@
Here, we’ve made the assumption that we’ve put the
rhino
shell script and thejs.jar
file (the only file we need from the download) into the same directory.
Create a JSLint file that Rhino can use.
For this, you’ll need to travel back in time to before Crockford stopped supporting Rhino.
Download the last version of the
rhino.js
file from GitHub.Download the latest version of
fulljslint.js
from Github.Concatenate the two files together.
cat fulljslint.js rhino.js > ultimatejslint.js
Tell PHP Code Sniffer where your
rhino
andultimatejslint.js
files are:phpcs --config-set rhino_path /path/to/rhino phpcs --config-set jslint_path /path/to/jslint.js
Run PHP Code Sniffer with a standard that knows about JSLint.
Either use the Squiz standard:
phpcs --Standard=Squiz .
Or, add the Squiz JSLint Sniff to your own standard.
In your standard’s
getIncludedSniffs()
function, add the following Sniff to the returned array:'Squiz/Sniffs/Debug/JSLint.php'
And that’s it. There’s a few steps, but once you’ve done them once, JSLint issues will show up as warning in your reporting. If you want to change the flags that JSLint has turned on, you’ll need to update the array at the start of the rhino.js
file.
If you enjoyed this post, subscribe to The Code Train and read more when I write more.
Just discovered you via a search for table sorting. Great work, and I’m adding the Code Train to my must-reads.
Just what I needed. Thanks, Neil.
I’ve spent so much time trying to find how to specify global JSLint options through command line, and it appeared that rhino.js file does not support it.
I’ve modified rhino.js https://gist.github.com/alexdesignworks/7243578#file-rhino-js
Hi, Perhaps, you need to update the link of fulljslint file to https://raw.githubusercontent.com/douglascrockford/JSLint/master/jslint.js
PHP code sniffer in window operating system with wamp server Step by Step video guide
visit here goo.gl/5u2KP3