If you are working in a collaborative environment, coding standards are important. When you are reading through code that you haven’t written, it helps if it follows a standard.
As I work with Automattic, the keepers of WordPress.com, I get the opportunity to work on the WordPress Platform. WordPress have their own standards that are sensible without being pedantic and are similar to the PEAR standard. The emphasis of the standard (in my opinion), is to present your code so collaboration is as easy as possible. It would prefer if you wrote code that is easier to read and understand, even if it is a little longer, than some compressed, ubersmart block of code that only you can follow.
Sometimes I look back at old code I did, like when I first wrote a WordPress plugin, and see places where it is not consistent with the WordPress standards… and I’d to get the itch to fix it, line by line, which is time consuming and dull work.
So I looked into ways to automate this in my text editor but alas no feature or option existed. While its not really possible to automate for all the standards, it should be possible to correct the formatting.
What can be automated?, well…
- Braces style. It should use K&R style.
- Indentation. Need to remove spacing and use tabs instead.
- Spacing. Add spacing to make code more readable.
- No shorthand PHP. So no
<?
allowed.
If you are using Coda, there is an excellent plugin called Coda PHP & Web Toolkit written by Mario Fischer. This is easy to install, and all you have to do to tidy your HTML, PHP, Javascript and CSS is right click on your code and select the plugin’s Tidy option.
The best thing about this, it meets WordPress PHP formatting standards with only a couple of exceptions. To fix them, you need to locate the phptidy-coda.php file, usually found here…
/Users/[your user name]/Library/Application Support/Coda/Plug-ins/PhpPlugin.codaplugin/Contents/Resources/phptidy-coda.php
If you can’t locate it, go to root and use the following search command.
sudo find . -name "*.php" -exec grep -H "phptidy" {} \;
The first exception is, that for functions and classes, it pushes the opening curly bracket, {, to the next line.
To fix this, open phptidy-coda.php in an editor and change the line
// from $curly_brace_newline = array(T_CLASS, T_FUNCTION); // to... $curly_brace_newline = false;
The other exception is, it does not add spacing after a start round bracket (, or before an end round bracket, ).
In phptidy-coda.php, find the phptidy function and add a default for this formatting with the other defaults…
// Enable the single cleanup functions $fix_token_case = true; $fix_builtin_functions_case = true; $replace_inline_tabs = true; $replace_phptags = true; $replace_shell_comments = true; $fix_statement_brackets = true; $fix_separation_whitespace = true; $fix_comma_space = true; //add new default in here $fix_round_bracket_space = true; $add_file_docblock = false; $add_function_docblocks = false; $add_doctags = false; $fix_docblock_format = true; $fix_docblock_space = false; $add_blank_lines = false; $indent = true;
Then look for the phptidy function and add a check for this default to format the spacing for round brackets.
// Simple formatting if ( $GLOBALS['fix_token_case'] ) fix_token_case( $tokens ); if ( $GLOBALS['fix_builtin_functions_case'] ) fix_builtin_functions_case( $tokens ); if ( $GLOBALS['replace_inline_tabs'] ) replace_inline_tabs( $tokens ); if ( $GLOBALS['replace_phptags'] ) replace_phptags( $tokens ); if ( $GLOBALS['replace_shell_comments'] ) replace_shell_comments( $tokens ); if ( $GLOBALS['fix_statement_brackets'] ) fix_statement_brackets( $tokens ); if ( $GLOBALS['fix_separation_whitespace'] ) fix_separation_whitespace( $tokens ); if ( $GLOBALS['fix_comma_space'] ) fix_comma_space( $tokens ); // Add in you call to new function here if ( $GLOBALS['fix_round_bracket_space'] ) fix_round_bracket_space( $tokens );
Lastly, add in the function to format the code to add in spacing after the start round bracket and before the end round bracket where appropriate.
/** * Adds one space after a start round bracket and before an end round bracket * * @param array $tokens (reference) */ function fix_round_bracket_space( &$tokens ) { foreach ( $tokens as $key => &$token ) { if ( !is_string( $token ) ) continue; if ( // If the current token is a start round bracket... $token === "(" and // ...and the next token is no whitespace !( isset( $tokens[$key+1][0] ) and $tokens[$key+1][0] === T_WHITESPACE ) and // ...and the next token is not an end round bracket !( isset( $tokens[$key+1][0] ) and $tokens[$key+1][0] === ')' ) ) { // Insert one space array_splice( $tokens, $key+1, 0, array( array( T_WHITESPACE, " " ) ) ); } else if ( // If the current token is an end round bracket... $token === ")" and // ...and the previous token is no whitespace !( isset( $tokens[$key-1][0] ) and $tokens[$key-1][0] === T_WHITESPACE ) and // ...and the previous token is a start round bracket !( isset( $tokens[$key-1][0] ) and $tokens[$key-1][0] === '(' ) ) { // Insert one space array_splice( $tokens, $key, 0, array( array( T_WHITESPACE, " " ) ) ); } } }
Then save the file. You may need to edit this file using sudo in order to change it.
Restart Coda and tidy your PHP.
UPDATE:: Mario has told me that he will include these changes in a future update But in case it is not in the next update, you should make a backup of the phptidy-coda.php file, as the next plugin update may overwrite it.
UPDATE 2:: Mario has indeed included these changes into the lastest version of the plugin. He has also made the format configurable so if you don’t want your curly brackets on the same line, you can set it so.
Now that is all good and well if you have Coda, but what of the TextWrangler folk. Well the plugin for Coda uses an open sourced library called phptidy, written by Magnus Rosenbaum, which is impressive not least for the brevity of the documentation.
To get it to work with TextWrangler is fairly straightforward. Follow these steps and if I have left any out place highlight them in the comments below.
Note: I had to make some minor changes to the phptidy library to get it to work with TextWrangler. As well as the 2 changes above, I also had to suppress any text echo’d by the library.
- Download my phptidy library.
- Uncompress the zip file and copy the Tidy PHP.sh file to the Filters directory, you can find out how to do that here
- Open terminal and copy the phptidy.php file to your
/usr/bin
folder - Go to the
/usr/bin
folder and change the file permissions of the phptidy.php by typing,sudo chmod 755 phptidy.php
Now you should be ready to try out the filter in TextWrangler.
At a later date, I plan to use John Godley’s CodeSniffer code, to validate the code and get a detailed explanation of where the code does not follow the WordPress standard.
Filed under: Shortcuts Tagged: coda, coding standards, format, php, phptidy, plugin, textwrangler, tidy, wordpress
