Posted on April 3rd, 2011 by Neil Crosby. Filed under Blog Posts.
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
rhinoscript 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
rhinopassthrough 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
rhinoshell script and the
js.jarfile (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.
Tell PHP Code Sniffer where your
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:
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
If you enjoyed this post, subscribe to The Code Train and read more when I write more.