views:

190

answers:

4

I'm using a pre-commit hook to lint-check PHP source submitted by our staff, and everyone's really happy with the results. The staff have agreed it would be useful to have the code re-formatted to follow (customizable) style rules on commit. Are there any FOSS scripts which can do this from the command-line?

+3  A: 

PHP_CodeSniffer is a nice PEAR package, but it only sniffs the PHP documents for detecting violations of coding standards - it doesn't fix the violations or re-formats the documents. However, it's a great package that every PHP-developer should at least know about - and maybe you can get your fellow staff members to sanitize their code on their own.

You can edit the sniffing-filter in PHP_CS the way you want it to work. I on the other hand likes the default settings, which is at least a good start for writing better code.

Björn
A: 

Not on the command-line but perhaps useful: PHP/Eclipse can do auto source formatting and I think you can run it on an entire project in one go. http://www.phpeclipse.com/ .

+1  A: 

Not quite an answer, but since you bring it up, there should be a command line PHP beautifier. Until then you're stuck with sed to fix tabs.

The problem is, its very difficult to write something like what you want without writing a parser for the PHP language. So, if someone were to write such a tool, it should also provide lint functionality, be platform independent and released under a very unrestrictive license.

Considering the above, I think that's why one doesn't (currently) exist (that I could find). I'm posting this as an answer to say thanks for the idea, which is also why this is CW.

Tim Post
+2  A: 

Was looking for the same thing, just came across the PHP Beautfier package in the PEAR repository:

http://pear.php.net/package/PHP%5FBeautifier

Hope this helps!

Gummy