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.
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 ;)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.
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)