PHP Code Sniffer, Eclipse and WordPress

Or: Steady progress on WordPress and it’s coding standards and getting things working together

Have I said previously that I personally really hate the WordPress coding standard? All these spaces in there. For some coders apparently it makes it easier to read – I really dislike those whitespace everywhere. But as with any coding standard, someone is not confident with it – ever. And I’m not such a person that can not adopt to a project’s style. So the current standard we have is not a blocker to me. Infact, I even can help to improve it.

Since we have made our minds a lot about which standard to adopt and to put the most serious issues upfront (currently the code-base is very inconsistent in it’s style) I wanted to make progress to practically adopt the style. The coding-standard-ticket in trac does not have much activity yet, so it’s about time. As weapon of choice I’ve selected PHP_CodeSniffer; A command-line application (PHPCS) that can check PHP code against a definition of coding standard and lists the problematic lines afterwards. It is available in the PEAR repository.

After the decision to use PHP Code Sniffer (I did not find any alternative and Installation is done quickly via PEAR) I was looking for the appropriate standard definition. To my surprise, there already was one available for download. Ca. one year ago, John Godley made it public on his blog, with a mention by Matt Mullenweg. So I installed the PHP_CodeSniffer_Standards_WordPress-0.0.1. Since commandline tools are nice for automating stuff (e.g. checks on changes of a file) I like to have it more comfortable within my editor.

For a better integration of PHP CodeSniffer into Eclipse I’m usind PHPsrc – Eclipse PHP Tool Integration. It ships with it’s own copy of PEAR and PHP_CodeSniffer so that it does work out of the box. You can find the Eclipse Update Site on that link, so it is a breeze to install. It does highlight the violations directly with your code then – very comfortable and good for ongoing quality control.

I wondered a bit – since there was already a Coding Standard defintion for PHP Sniffer – why it was not that well adopted and more used. After the first run, I understood why: It was throwing an immense amount of errors on me, nearly seven thousand. I can understand that every developer who want to give that some attention would say: well folks, why should I take care? There is a psychological dimension in teamwork and Coding Standards is something that is closely connected with collaborative development.

New WP Test-Sniffs

Then I took a closer look why there were so many errors. A view inside the sniff revealed that many definitions were taken over from projects like PEAR whereas in the current discussion many developers expressed their dislike of the PEAR coding standards. Also it is checking for so many things which aren’t really clear in the definition. When I would create patches based on those rules, I’m pretty sure Nacin would become pretty angry to me ;)

Wordpress Sniff compiled in April 2009

So I started to compile a new set of sniffs. It concentrates more or less on the basic rules that everybody agreed to, so a first level can be applied easily without getting overwhelmed. This is backed up with the second idea that has been proposed: create reference files. I created three files: style_bad_coding.php, style_good_coding and style_undefined_coding.php. It’s quite simple, good coding contains good code examples that should not raise error. Bad coding contains only bad code, so every case must raise an error. In undefined I started to document those cases where the current standard is not that strict or where even no definition is available. The wordpress codesniffer standard should not do errors on that file.

PHP CodeSniffer with WP sniffs in Eclipse

With this principle I was able to create the first Sniffs. I’ve uploaded some screenshots which show what I’ve just written about. And there is one big screenshot that shows the whole picture of the integration of PHP Code-sniffer. There are things to come the next days, so wether you’re using Eclipse, the commandline or some other IDE that integrates with PHP CodeSniffer, let me know. The sniffs will be ready to test soon.


Previous: Coding Standards Summary of the last Week (28 Jan 2010; by hakre)

About these ads
This entry was posted in Hacking The Core, PHP Development, Tools and tagged , , , , , , , , , , , , , , . Bookmark the permalink.

7 Responses to PHP Code Sniffer, Eclipse and WordPress

  1. @Hakre I am very impressed with this, I was just embarking on the same path as you, found PHP CodeSniffer, found the intial Sniffs that had been done, Matt’s post, saw the same sad results, started to make my own Sniffs, then realized, *someone* has had to have done this before, started hunting again, and found this post. Congratulations on going down this path and making it as far as you have from the screenshots. I am really excited to get your Sniffs rules! When can you make them publicly available? I have a major deviation from the WP coding standards, I use Whitesmiths indententation, and would like to continue, so I will have to make changes to any sniffs to allow for that.

    • hakre says:

      I still have the plan to release that. One thing I wanted to do first is to take care of the whitespace inside core code otherwise it would not make much sense to actually release the sniffer – it just blows eclipse in my case with tons of errors. That would be contra-productive.

      Can you say something about your whitespace decision?

      • @hakre I think that while the early release of your Sniffs might be premature in regards to running against the core with its current miasma of irregular use of the WP standards as this would result in innumerable errors, that plugin and theme development can be improved by the early availability of the WP Sniffs. Theme and plugin developers will be diving back into their codebases to test and ready their projects for WP3.0 and if they have an additional tool that can help improve their code that would be a boon.

        As for my leaning to Whitesmiths style, a nice visual introduction to its benefits is here: http://activeclickweb.com/whitesmiths/index.html

        Other than that, theres been so much discussion on wp-hackers list in on WP Coding Standards which I have not yet read because the glut of posts started to troll around the beginning of line tab/space quandary that I gave up. As that matter goes, if your not working in an editor that can convert tabs to spaces and back again then you are using the wrong editor.

        I think it would also help to mention here that PHPCodeSniffer unlike some other static code analyzers does not modify any code automatically so is not a code beautifier. Any recommendations for a code beautifier which can be configured to use WP Coding Standards?

  2. Pingback: Coding Standards that ain’t one | hakre on wordpress

  3. Eric Marden says:

    Have you made any more progress with this? I am about to introduce Code Sniffing into our dev workflow and will be applying it specifically to our theme and plugin work (just as R’phael Spindel suggested). If you can share the code, I’d be interested in peeking at it, even just to use it as a spring board for a full implementation.

  4. Pingback: Joomla CodeSniffer : News Of Open Source!

  5. Pingback: Joomla CodeSniffer

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s