php source reformat script for braces

Discuss general development subjects that are not specific to a particular version like the versioning control system we use or other infrastructure.
Post Reply
Oleg
Posts: 1150
Joined: Tue Feb 23, 2010 2:38 am
Contact:

php source reformat script for braces

Post by Oleg »

Lacking a general discussion forum I'm going to plop this topic in 3.1 discussion.

This is a script I'm using to convert the correct brace style ([1]) to the phpbb one ([2]).

It only does the braces. There's probably a generic reformatter out there somewhere but 1) I'm too lazy to search for it, and 2) who knows whether it will change something other than braces.

It also kills whitespace from lines containing only whitespace, which is what my editor does.

http://gist.github.com/366837

Usage: phpbb-reformat.sh /path/to/file.php

[1]

Code: Select all

if (foo) {
    bar;
} else {
    baz;
}
[2]

Code: Select all

if (foo)
{
    bar;
}
else
{
    baz;
}
ToonArmy
Registered User
Posts: 335
Joined: Fri Mar 26, 2004 7:31 pm
Location: Bristol, UK
Contact:

Re: php source reformat script for braces

Post by ToonArmy »

nn- wrote:This is a script I'm using to convert the correct brace style ([1]) to the phpbb one ([2]).
'Correct' is entirely subjective. ;)
Chris SmithBlogXMOOhlohArea51WikiNo support via PM/IM
Image
Oleg
Posts: 1150
Joined: Tue Feb 23, 2010 2:38 am
Contact:

Re: php source reformat script for braces

Post by Oleg »

ToonArmy wrote: 'Correct' is entirely subjective. ;)
While there have been many imposters inventing all kinds of indentation styles and conspiring with search engines everywhere to push their petty styles up in rankings, the correct indentation style is the one I clearly marked "correct" above. There is nothing subjective about it.

Once upon a time I too committed the heresy of using an incorrect indentation style. But I had seen the truth, and amended my ways. (Much easier done with git.)

This little gem converts camelcase in a php file into underscores:

Code: Select all

for f in `cat filelist`; do cat $f |perl -pe 's/([a-z]+([A-Z][a-z]+)+)/$x=$1;$x=~s:([A-Z]):_\1:g;lc $x/ge && s/\$php_ex/\$phpEx/g' >t && mv t $f; done
ToonArmy
Registered User
Posts: 335
Joined: Fri Mar 26, 2004 7:31 pm
Location: Bristol, UK
Contact:

Re: php source reformat script for braces

Post by ToonArmy »

nn- wrote:
ToonArmy wrote: 'Correct' is entirely subjective. ;)
While there have been many imposters inventing all kinds of indentation styles and conspiring with search engines everywhere to push their petty styles up in rankings, the correct indentation style is the one I clearly marked "correct" above. There is nothing subjective about it.
The fact that there are many styles would suggest there is no one correct style, and I find the Allman style more readable than K&R/NIX kernel or KNF as your definition is ambiguous.
Chris SmithBlogXMOOhlohArea51WikiNo support via PM/IM
Image
Oleg
Posts: 1150
Joined: Tue Feb 23, 2010 2:38 am
Contact:

Re: php source reformat script for braces

Post by Oleg »

I actually came up with a serious reply, but I am convinced it will take this thread way off topic. If you wish to continue discussing indentation we could do so in another thread.
User avatar
imkingdavid
Registered User
Posts: 1050
Joined: Thu Jul 30, 2009 12:06 pm

Re: php source reformat script for braces

Post by imkingdavid »

nn- wrote:I actually came up with a serious reply, but I am convinced it will take this thread way off topic. If you wish to continue discussing indentation we could do so in another thread.
I agree that correct is subjective, but I won't go into that any further. I like the phpBB way because it makes things easier to read imo.

In any case, this is a handy tool to do this, so thanks for sharing. :)
I do custom MODs. PM for a quote!
View My: MODs | Portfolio
Please do NOT contact for support via PM or email.
Remember, the enemy's gate is down.
Michaelo
Registered User
Posts: 106
Joined: Thu Apr 01, 2004 7:56 am
Location: Dublin

Re: php source reformat script for braces

Post by Michaelo »

I have been using the phpBB format style since the early seventies... I never understood the logic of method 1...
Having said that, I never understood the logic of lower case true/false but I guess I can't have everything... :mrgreen:

Simply said, following the phpBB code guidelines makes code much easier to read... I hope nothing changes...
I did fear the move to a framework would result in the method 1 above being adopted, but I am very pleased it did not...
Mike
Mods: Forum Icons Enhancement, Kiss Portal Engine
Links:
Kiss Portal Engine (dev site) Stargate Portal (archive site) ...
Styles: Technika
Oleg
Posts: 1150
Joined: Tue Feb 23, 2010 2:38 am
Contact:

Re: php source reformat script for braces

Post by Oleg »

Bump for a new version. Dependency on gnu sed replaced with perl, some oversights fixed, supports both 2- and 8-space indentation (heuristically).

Besides helping to keep myself sane I also found this script useful for fixing up patches submitted to the tracker by non-developers.

It also produces a non-empty diff on phpbb tree (develop).
Post Reply