3

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?

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Phillip B Oldham
  • 18,807
  • 20
  • 94
  • 134

4 Answers4

3

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
  • 29,019
  • 9
  • 65
  • 81
2

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

http://pear.php.net/package/PHP_Beautifier

Hope this helps!

Gummy
  • 46
  • 4
1

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
  • 33,371
  • 15
  • 110
  • 174
0

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

sanbikinoraion
  • 752
  • 2
  • 8
  • 12