phptidy

Long, long ago, Steve Hancock gave us PerlTidy, which allowed me to quickly and reliably convert raw Perl code into code that abided by all of my coding standards, whatever they might be for a particular project. This was much goodness.

I even have a vim macro that allows me to select a block of text, type a key sequence, and have that code reformatted so that it is more easily readable, evenly indented, and just prettier.

For many years, I’ve looked for a similar tool for PHP. I’ve seen references to something that Coogle did, but I can’t actually find it, and it doesn’t completely sound like what I’m looking for anyways.

Apparently the problem is that the intermixing of form and function (html and php) that happens in php code, makes this a more difficult problem than just parsing and reassembling source code. I naively think that there must be a simple way to treat HTML as comments and ignore it, but seeing as I have not the vaguest idea of how to go about this, it’s kinda of condescending of me to make such claims. What do I know?

This morning I discovered that PerlTidy works, some of the time, to tidy up PHP code. And, of course, sometimes it doesn’t, because part of what PerlTidy does is inspect the Perl code itself, in order to determine how to format things, and PHP code doesn’t generally parse as valid Perl, except when it does.

When I was learning PHP, I observed repeatedly that it was almost exactly the same as Perl, in most respects, most of the time. This gained me many dirty looks from the “Perl is evil” PHP programmers I encountered, but, since language bigotry has never struck me as a survival strategy, I continue to maintain this belief. Indeed, this belief allowed me to uncover features of PHP that I don’t frequently see used, and aren’t very well documented, but I discovered blindly by assuming that PHP should behave the same as Perl if I need it to. And most of the time it does.

Ahem. Back to the point.

I find numerous postings on various blogs and mailing lists, where people are bemoaning the lack of a usable PHPTidy tool. I’m really curious why it’s a hard problem. I don’t understand what’s involved in doing this, in the least, either in PHP or Perl, so I have no real notion of whether it is hard or not. What gives?