Php Beautifier

Posted on Sat 23 January 2010 in Tech

Why?

When you're faced with an ugly PHP file(or files) with little to do but trawl through it, there is a solution.

PHP beautifier is a pear package that will process PHP files and reformat them in a (hopefully) nicer format.

Installation

You can install it using pear with a very simple

pear install PHP_Beautifier

Usage

Usage is pretty dependent on your needs. What's nice is, it takes formatting rules as arguments, so if you pass in Pear() as an argument your code will be formatted to the pear standard.

Here's a bash alias I used to fire an entire folder(and recursively through all sub-folders) through the beautifier with the Pear standard coding conventions and outputted to a src2 folder.

alias beautify="php_beautifier -l "Pear()" -r "*.php" ./src2/"

Or if your using vim here is a mapping to call the beautifier on the current file when Ctrl-F2 is pressed(all my function keys are currently mapped to vim'sxdebug plugin).

map :% ! php_beautifier

Results

The results are pretty nice actually. The author of it notes that on 40,000 lines of PHP it hasn't broken a single file. I have noted that it can make certain files look like crap however. An example would be our locales file, that has large array mappings of terms to languages. Unless you're totally certain your whole project is going to look awesome I would suggest using this on a file by file basis, as I have with vim.