views:

548

answers:

5

I'm working on a PHP project with several developers, each who has their own style. The styles differ significantly, and can make the code really hard to read, and everyone is in agreement that we need to standardize our style.

The issue I'm having is that I can't find any good program or system to help enforce standards for PHP. I've used a number of programs in the past for C++ and Java, and I'm a huge checkstyle fan, but I can't find anything so elegant for PHP.

What program, tool, or system would you recommend to help developers adhere to a common set of PHP coding standards? Additionally, it would be great if the solution also handled PHP embedded in XHTML, but that is not a requirement.

+11  A: 

PHP_CodeSniffer seems like a great tool. A project I was affiliated with used the Zend Framework coding standard as a basis for our own, and we looked into using it.

However, our deadline did not allow much leeway for experimenting with new tools, so we never got into using it past initial testing.

Jukka Dahlbom
+4  A: 

I used in the past phpCodeBeautifier. It does not have lots of options, but it gets the job done to certain point and helps keeping standard code.

It's run from command line and, provided your IDE supports external formatters, you could integrate with that, or create a script to run on all your files.

Alternatively, if you use Eclipse or you don't but plan to run this occasionally, you could use Eclipse with its PHP plugin. You can re-format code inside folders with just a couple of clicks.

Seb
+1  A: 

I've used coding standard part of Zend in the past and it has worked pretty good for me. You can find it at Zend Coding Standard providing you are using Zend to develop your code.

Mark
I find the ZF coding standard very useful even if you don't use ZF otherwise. It has good arguments for most of its cases, and moreover: it's already there: lot less trouble getting your teammates to using it, compared to fighting over your own standard.
Jukka Dahlbom
Good point Jukka. I have written coding standards in the past and it took over two years to get it adopted by the department because people argued about everything I put in there. I even had a few people argue that it was not a good idea to put a default in every case statement...
Mark
+3  A: 

I faced the same problem before, but not after I use Zend Studio for Eclipse

http://www.zend.com/en/products/studio/

Using Zend Studio gives the following benefits:

  1. Code that is 100% free from syntax errors

  2. Code that is formatted using PHP or Zend Standard. Just Press Ctrl+Shift+F and everything formats nicely.

  3. Detect potential warning, such as security warning for variable files inclusion, such as:

    include $file;

You can find a lot more details on their website. Now I can't work without Zend Studio.

uuɐɯǝʃǝs
+2  A: 

If you have several developers, you must be using some sort of version control and continuous integration, right? I use PHP_CodeSniffer (with Zend rules and automatic tab-to-spacing changes) to watch for coding violations and report them using phpUnderControl. If you aren't using continuous integration, there is a great guide over at iBuildings about setting it up along with PHP_CodeSniffer, phpUnit, and phpDocumetor: http://techportal.ibuildings.com/2009/03/03/getting-started-with-phpundercontrol/

dragonmantank
+1, good point about combining codesniffer with UnderControl. Will have to give it a go someday soon.
Jukka Dahlbom