PHP is old and ubiquitous. It is completely bizarre that there are almost no options to format your PHP. Javascript has eighteen formatters and evaluaters and lint-ers. PHP is really lame by comparison.
In fact, other than online versions, I only found one. (Your comments will be appreciated!)
http://www.phpformatter.com/ is the only one I could find that I can run on my own computer. It is a pear component. I hate pear. It has all the worst aspects of php - inconsistent calls, structures and everything else.
But, I'm desperate. After a year of primarily NodeJS, I'm returning to PHP having developed a strong reliance on being able to clean up my code at the press of a button. Further, the project I'm returning to did not benefit from that technology so it's got a million lines of code that is only as pretty as I had energy that particular day. Since I'm also trying to read an old project, this is an urgent need.
Since I have hated pear for much longer than I've had my current computer, I had to install it. That was infinitely more hassle than it should have been (I know there are people who dislike NPM but it works infinitely better than anything PHP, including composer). But, done.
After that, installing php_formatter was no big deal. It works, too. Except...
1) Php_formatter actually sets the PHP error_level explictly. The PHP authors, in another example of what's bad about PHP, has changed the meaning of E_ALL. Now it includes E_STRICT. This is really stupid.
2) Php_formatter strips all empty lines from the code. I use empty lines. A lot.
So, here's the thing. I had to edit the php_formatter files to comment out their error specification. Amateurs. It was two files. I don't remember which so do a multi-file search.
I use bbedit by Bare Bones Software (a great program). I want this to operate as a Text Filter. That's not so hard. bbedit passes the contents of the window (or selection) to the program and, bingo!, you are in business. In fact, php_formatter worked correctly once I got it working at all.
The problem is the stripping of blank lines. Turns out that php_formatter supposedly has a filter that preserves them but I could not make that work.
I made a NodeJS program that turns blank lines into comments. Then I beautify. Then I remove the comment characters. Voila! Blank lines are retained.
For the record, here's the Text Filter:
#!/bin/bash
~/Scripts/bin/js/commentUnComment.js -c | php /Users/tqwhite/Documents/webdev/pear/bin/php_beautifier -t | ~/Scripts/bin/js/commentUnComment.js -u
And here's commentUncomment.js:
[NOTE: This is updated as of 5/29/15. The second regex failed in some circumstances.]
//=========================================================
#!/usr/local/bin/node
var inString='';
var cmdLineParameters={};
for (var i = 0, len = process.argv.length; i < len; i++) {
var element = process.argv[i];
if (element=='-c'){
cmdLineParameters.c=true;
}
if (element=='-u'){
cmdLineParameters.u=true;
}
}
var writeStuff = function() {
var outString='';
if (cmdLineParameters.c){
outString=inString.replace(/\n\s*\n/gm, '\r//\r');
}
if (cmdLineParameters.u){
outString=inString.replace(/^\s*\/\/\s*$/gi, '');
outString=inString.replace(/^\t*\/\/\s*$/gmi, '');
}
process.stdout.write(outString);
};
//the rest ========================================================
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data){
inString+=data;
});
process.stdin.on('end', writeStuff);
In fact, other than online versions, I only found one. (Your comments will be appreciated!)
http://www.phpformatter.com/ is the only one I could find that I can run on my own computer. It is a pear component. I hate pear. It has all the worst aspects of php - inconsistent calls, structures and everything else.
But, I'm desperate. After a year of primarily NodeJS, I'm returning to PHP having developed a strong reliance on being able to clean up my code at the press of a button. Further, the project I'm returning to did not benefit from that technology so it's got a million lines of code that is only as pretty as I had energy that particular day. Since I'm also trying to read an old project, this is an urgent need.
Since I have hated pear for much longer than I've had my current computer, I had to install it. That was infinitely more hassle than it should have been (I know there are people who dislike NPM but it works infinitely better than anything PHP, including composer). But, done.
After that, installing php_formatter was no big deal. It works, too. Except...
1) Php_formatter actually sets the PHP error_level explictly. The PHP authors, in another example of what's bad about PHP, has changed the meaning of E_ALL. Now it includes E_STRICT. This is really stupid.
2) Php_formatter strips all empty lines from the code. I use empty lines. A lot.
So, here's the thing. I had to edit the php_formatter files to comment out their error specification. Amateurs. It was two files. I don't remember which so do a multi-file search.
I use bbedit by Bare Bones Software (a great program). I want this to operate as a Text Filter. That's not so hard. bbedit passes the contents of the window (or selection) to the program and, bingo!, you are in business. In fact, php_formatter worked correctly once I got it working at all.
The problem is the stripping of blank lines. Turns out that php_formatter supposedly has a filter that preserves them but I could not make that work.
I made a NodeJS program that turns blank lines into comments. Then I beautify. Then I remove the comment characters. Voila! Blank lines are retained.
For the record, here's the Text Filter:
#!/bin/bash
~/Scripts/bin/js/commentUnComment.js -c | php /Users/tqwhite/Documents/webdev/pear/bin/php_beautifier -t | ~/Scripts/bin/js/commentUnComment.js -u
And here's commentUncomment.js:
[NOTE: This is updated as of 5/29/15. The second regex failed in some circumstances.]
//=========================================================
#!/usr/local/bin/node
var inString='';
var cmdLineParameters={};
for (var i = 0, len = process.argv.length; i < len; i++) {
var element = process.argv[i];
if (element=='-c'){
cmdLineParameters.c=true;
}
if (element=='-u'){
cmdLineParameters.u=true;
}
}
var writeStuff = function() {
var outString='';
if (cmdLineParameters.c){
outString=inString.replace(/\n\s*\n/gm, '\r//\r');
}
if (cmdLineParameters.u){
outString=inString.replace(/^\s*\/\/\s*$/gi, '');
outString=inString.replace(/^\t*\/\/\s*$/gmi, '');
}
process.stdout.write(outString);
};
//the rest ========================================================
process.stdin.resume();
process.stdin.setEncoding('utf8');
process.stdin.on('data', function(data){
inString+=data;
});
process.stdin.on('end', writeStuff);