The Code Train

Where Neil Crosby talks about coding on the train…

RSS Entries

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:

  1. Have a working installation of Java.

  2. Have a working copy of PHP Code Sniffer, installed via PEAR.

  3. 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 the js.jar file (the only file we need from the download) into the same directory.

  4. Create a JSLint file that Rhino can use.

    For this, you’ll need to travel back in time to before Crockford stopped supporting Rhino.

    1. Download the last version of the rhino.js file from GitHub.

    2. Download the latest version of fulljslint.js from Github.

    3. Concatenate the two files together.

      cat fulljslint.js rhino.js > ultimatejslint.js
      
  5. Tell PHP Code Sniffer where your rhino and ultimatejslint.js files are:

    phpcs --config-set rhino_path /path/to/rhino
    phpcs --config-set jslint_path /path/to/jslint.js
    
  6. 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.

Tags: , , , ,

If you enjoyed this post, subscribe to The Code Train and read more when I write more.

3 Responses to “Making PHP CodeSniffer use JSLint via Rhino”

  1. Just discovered you via a search for table sorting. Great work, and I’m adding the Code Train to my must-reads.

  2. Just what I needed. Thanks, Neil.

  3. 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

Comments RSS

Leave a Reply

TheCodeTrain Theme by Neil Crosby, Powered by WordPress