Extension:CodeTidy
I've been having a lot of trouble finding a decent free formatter for PHP, especially one that matches MediaWiki's conventions, there are many online and command-line tools but they just don't seem to work for some reason, the results are all still full of bad indenting with combinations of tabs and spaces, and they never get the braces the way you want them!
So I've had a go at writing a simple one. My one one just uses all regular expressions, no fancy compiler magic, so it's possible that it could break things, and is still very beta and being tested on various messy code. There is a basic online interface in the form of a Special page which is very basic at the moment too, but will improve over time.
It's hardwired for MediaWiki's conventions, I may add more different options later.
The extension code is linked at the top of the page, but it refers to the tidy.php script in our tools repository which defines a class that can be used by extensions or other PHP programs, but can also be run from the command-line taking the messy code's filename as its only parameter and outputting the formatted result to STDOUT so it can be copied to the clipboard or directed to a new file.
Example command-line usage:
php tidy.php ugly.php > nice.php
To do
- Problems with outdenting "break" that don't belong to their "case"
- Case statements should be at same indent level as their switch
- Needs to change for/if/else etc with unbraced statements to braced statements
- Allow wildcard in filename command-line arg
See also
- Special:CodeTidy - the special page interface to use the script on line
- tidy.php - the core class that does the tidying
- Extension:HighlightJS - the client-side syntax highlighter we use in this wiki