By David Mytton

Coding Style

By David Mytton

Last year, I wrote an article on Coding Standards. That is all very well with your own work, but what happens when you are using code from another programmer who uses a different style from your own? The answer is phpCodeBeautifier.

Although this is an old (2003) app, I have found it useful at least once. Basically, you set a number of options in response to statements about how you want your code to look, and then run it on your file(s) and it will change the code to match your style. This means you can convert any PHP code from any style of coding to your preferred style.

For example, you can change brace styles (on the same line or on a new line), add spaces after brackets and change comment styles.

The best thing is that it is completely free and comes with GUI and command line versions.

Download here and view the manual here.

Do you use a tool, script or library in PHP that you think would be worth me blogging about? Let me know!

  • Hmm no updates since 2003… I wonder how it copes with PHP 5-specific syntax.

  • Vee
  • I don’t use the phpCodeBeautifier… for a couple of reasons.

    First, Zend Studio has a nice “Reformat Code” option, and secondly, there is a pretty decent pear package to allow this:

    Im surprised that you didn’t mention either of these David.

    ~~ Eric

  • AussieDan

    Other disadvantages of this kind of wholesale reformatting are:

    1. It does not generally play nicely with revision management systems like CVS or Subversion.

    2. It makes applying patches or updates from the upstream author difficult or impossible.

    In general you should aim to make as few changes as possible to code being maintained outside the scope of your project. It may not be pretty, but in the long term it will keep your life as simple as possible.

  • Katana

    Updates to PHP Code Beautifier are planned for this year, including PHP5 support and lots of enhancements.

    A very useful side of phpCodeBeautifier is that it can also be used from command line and could be integrated as _ for instance _ a post-commit hook on subversion or something, so that any user can work with his own layout while the repository is kept clean :)

    A big time saver when you have to enter into old projects no one else is maintaining.

    And it can be ran from PHPEdit…

  • Any of these will help me as I’m use to just recoding it all by hand. Lol.

  • I guess you could always change the style to yours, make your mods, then change it back to the other style when you’re done. Should alleviate most of the source control issues

  • Ryan Wray

    I was tempted to make one of these a while back, I even made an automatic indenter once to indent those annoying code posts in forums which lacked proper indention. It is good to know there are more options available.

  • Dr Livingston

    Don’t know just how useful this could be to me? At times I need to accommodate a clients internal organisational standard, such as

    class XmlWalker { …

    Has to be fomatted to this instead,

    class xml_walker { …

    Which is annoying and difficult to maintain, so could this help me I ask? Doubtful :eek:

  • If you are going to use the style with braces under the method then you want to indent the braces also, since they need to be part of the code block.

    Code here;

    Code Complete, Second Edition has a good chapter about this.

  • Oops, looks like that didn’t turn out as I expected…

  • Here is an example, its called Whitesmiths style:

    Sorry about the triple post btw.

  • yosoyminero

    For Dr Livingston:

    If you work with Windows, try this:

    Neat and simple.

  • binjured

    I much prefer Polystyle ( myself. It has more features, it’s more customizable, is newer, integrates with a number of IDEs, and allows for entire directory parsing (biggest thing). It certainly costs more than phpCodeBeautifier, but you gotta spend money to make money and all that rot!

  • I find the formatting of html docs most annoying. It’s difficult to find an unclosed tag for example in the mess made by wysiwyg editors. So looking for a solution to this (not hand made by me) I found tidy which I think does a good job at formatting/fixing html errors.

  • Anonymous
Get the latest in Front-end, once a week, for free.