4

can anyone recommend tools/plugins that are capable of "normalizing" the coding style of a php document?

Oftentimes I find myself working on outdated codebase by different authors following different or no coding style at all. Before working on those documents, I often find I have to clean up the code and for quite a few things I just do a regexp search & replace, like fixing brace indentation, new lines, dot-connectors, spaces around operators and so forth...

I use Aptana Studio and like the default php formatting it offers, which is great for writing new code that conforms to a coding style. What I am looking for would ideally be some sort of tool or plugin that could take EXISTING code and even just slightly adopt it to a given coding style (or even set of regexp search & replace's).

Anyone got any hints?

P.S.: Please do note that I am not per se asking for advice on enforcing coding standards, but rather on how to automatize the adaptation of old code base to a coding standard.

Edit: I found this topic Aptana auto format: Aptana supports formatting of code to the active coding style via shortcut cmd + shift + f. From what I've tried so far, it behaves a bit weird and not exactly producing the kind of output I'd expect. So, while this might be something I could get to work, and would still be interested in other solutions to this.

Edit #2: Also found this topic How to format code on aptana 3? stating that folder/project wide reformatting is not possible in Aptana as of yet.

Edit #3: Awared the link to the PEAR Code Beautifier with the correct answer. It requires some reading yourself into :) Personally, I find most closely to what I am looking for was Aptana's / Net Beans' source formatting tools. For completeness sake, I also found a windows command line tool which looked promissing: http://www.waterproof.fr/products/phpCodeBeautifier/

3
  • 1
    Don't know how helpful it will be, but NetBeans has configurable auto-code stuff (like where to put spaces, braces on new lines or same and so on), and it can take a file and format it according to your settings. (Only one file at a time though.) Commented Mar 20, 2012 at 8:32
  • Aptana should be able to handle all of that at least on a per file basis. If it can't you may be using the wrong ide. I use Netbeans and can format a whole file with 'alt shit f'. I'm not sure I'd want to automate the process but I'm sure someone has an ant script or plugin to do automated formating over a whole project or code base. Also Netbeans has pretty good find and replace that does support various search criteria including regex. I've not used any of the pay for play Ide's, they're just too expensive for a non-pro, so I have no comparison. Commented Mar 20, 2012 at 9:20
  • Thanks for your comments. Aptana does have editable coding style preference settings and I love them. My problem is applying the same coding style to existing documents of messy/different coding style. Aptanas search and replace is nice and does feature regexp - the only thing here that might be something of a solution is if an IDE would make it possible for me to run x number of save reg exp search & replace on a file. Commented Mar 20, 2012 at 9:51

3 Answers 3

2

You can have a look at the PEAR Code Beautifier: http://pear.php.net/package/PHP_Beautifier

Sign up to request clarification or add additional context in comments.

3 Comments

Tried getting this to work on my xampp installation on my pc. While the installation says the PEAR package is installed and I can locate it, I can't find a php_beautifier.bat that the package documentation mentions for the command line usage - and I guess command line usage is what I am looking for here, not the script usage :?
On my installation the .BAT file is located in xampp/php.
Finally got around to taking a moment and sorting out how to use, upgrade and install pear packages and use this from command line. While I love Aptana for the file-editing level formatting, I just had PHP_Beautifier bring the codebase of a whole project into line and my jaw dropped :) Thanks again.
0

I found a php script, which can reformat php, it is called PHP tidy:

http://phptidy.berlios.de/

I prepared a list of all php files in a project, and it did its job beautifully.

1 Comment

Gave this one a test run, but considering what I am looking for the result it gave me is only minor improvement. From a quick check it did fix a little indenting here or there, but overall, I don't think this script can be seen to enforce a given, let alone, editable, coding style. Thanks for the answer.
0

See our PHP Formatter.

It will format a single file or a set of files.

5 Comments

Your trail registration form offers a radiobuttons for company or personal use, however, when selecting personal use, the form won't submit unless I feed a company name. Would love to give it a try, though.
@kontur: Sorry about the radio button. Lie to it while we fix that.
Thanks, Foobar Ltd now tested your software. After a bit of a bumpy installation I got it to run and feel like it won't particularly well achieve what I described in my question. I did not use the obfuscation tool, nor can I judge much on the actual under the hood ongoings, but while the code formatting seems enforced consistently, I did not see any options on how to have the code tranformed according to coding style of MY choice. Without that possibility, it does not really help me that much.
@kontur Did you read the docs? They list a set of configuration switches. Interested in the bumpiness; please send notes by private email but we like to hammer bumps flat when we can.
When evaulating the program I did glance at the docs and the console instructions for manipulating the formatting. What I am looking for would be more ease of use. Even though I am looking for an alternative to Aptana's code formatter, I keep refering to the positive sides it has - for instance an intuitive gui that let's you preview the coding style enforced. (see osde8info.files.wordpress.com/2008/10/…)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.