A php code beautifier that works

2008-05-11

A tool to reformat your php code and make it more readable

After searching for a php code formatter, I failed finding any good tool that will not break my code.

I wanted a tool that reformats php code to match my coding guidelines, so I coded my own solution.

So, here it is. A php code reformat tool that just works, at least on the code I tested. I'm releasing the code under the GPL license and everyone is invited to submit code on which this tool fails so I can improve it and make it better.

Test the Php code beautifier

The code is implemented as a class, a php file beautify.php is provided, so you can use it as a command line tool.

php beautify.php /myproject/sourcecode/ /myproject/beautifiedcode/

Or you can implement the class and use it however you want

	include 'PhpBeautifier.inc';
	
	$beautify = new PhpBeautifier();
	$beautify -> tokenSpace = true;//put space between tokens
	$beautify -> blockLine = true;//put empty lines between blocks of code (if, while etc)
	$beautify -> optimize = true;//optimize strings (for now), if a double quoted string does not contain variables of special characters transform it to a single quoted string to save parsing time
	$beautify -> file( 'test.php', 'beautified.php' );

The beautifier strips all lines and leading spaces from your source code and then adds spaces and new lines only where is needed. You can modify the beautifier class code to suit your taste.

I will try to make everything optional. For now, you can specify only if you want to put space between tokens ( parenthesis ) or empty lines between code blocks ( loop structures, if structures etc).

You can even choose to optimize your code by putting double quotes to strings only where necessary. Every double quoted string that does not contain a variable or special character will be transformed to a single quoted string.

For the next version I plan to make a indenter, or maybe a reformatter for html insides php code. For now, inline html remains untouched.

I also intend to reformat multidimensional arrays so they can be easier to read. For now, they are put on single line :(.

You can download the class, the command line tool and the example from here

Share this with the world

Related

Comments

Stefan

what about the new Zend Studio for Eclipse? It has an extensive code formatter built-in that you can configure to have every detail match your coding guidelines. Then, you can export those settings and have other people in your team import it. The formatting while working on a file is then only a single keyboard shortcut away.

Sure, it costs money, but Zend Studio (for Eclipse) has been worth a lot of money for ages in my humble opinion. It supports development in PHP in so many useful ways that it's worth its price.

Posted on 2008-05-12 07:09:29
Andreas

Hey there, been looking for this. The online tool seems to work quite nice, but i can't download you class. Maybe you should take a look. Thanks

Posted on 2008-05-14 00:39:57
Benjamin Kuz

This is a pretty nice piece of work - with only a few beefs.

1) You should include the option to use spaces instead of tabs - some places require that kind of thing since editors sometimes do funky things, like include half-tabs or other strange quirks which make the code come out of alignment. I hacked this in manually in order to make it work.

2) the first line of a comment sometimes \"stacks\" with the line above it, giving the effect of:

some_code();
# my comment

becoming:

some_code();# my comment

Posted on 2008-07-04 16:51:26
Einars

Well done! A minor glitch with heredoc, though:

printf(<<<XXX
lalala
XXX
);

shouldn't join the last lines to "XXX);" but leave them as they are.

Posted on 2008-09-18 10:50:53

Make yourself heard

Categories

Subscribe

All Posts

Php posts

All Comments

This post comments

© Copyright CodeAssembly

All code is licensed under GPL, unless otherwise noted