Lewis' Blog Tales from the trenches of information technology

26Mar/140

WP-phptidy – a tool to clean up WordPress plugin and theme code

WP-phptidyWhat is it?

WP-phptidy is based on phptidy, by Magnus Rosenbaum. Eoin Gallagher made some adjustments to phptidy (2.9, circa November, 2009) so that it would better conform to the WordPress PHP Coding Standards, which are similar in most respects to at least one set of PEAR Coding Standards.

Silviu-Cristian Burcă (aka scribu) kindly placed Eoin's work on github in January of 2012. As I have a natural aversion to social media and github's quasi-social media paradigm, I chose not to fork the project there, but rather to pull it and put it in my own Subversion repository, hosted on my server, and to make my changes freely available to others. Code contributions and patches are most welcome.

Where to download it

The latest build is available via anonymous FTP from my server. I presently do not allow access to my Subversion repository, and I have not yet added this project to my Mantis bugtracker (though I will be sure to update this support page if and when I do).

Requirements

From the phptidy readme:

- PHP 5
    phptidy does not work on PHP 4, because some functions introduced in PHP 5
    are used, and there are also some differences in the tokens. But it should
    be not too difficult to port it to PHP 4.

- PHP CLI SAPI
    http://php.net/manual/en/features.commandline.php
    phptidy expects PHP in /usr/bin/php. If your PHP executable is in a
    different path, you will have to edit the first line of phptidy.php.

- PHP extension: Tokenizer
    http://php.net/manual/en/book.tokenizer.php

 Optional:

- diff
    The standard Unix diff utility or similar
    Default is to use colordiff.
    Only required if you want to use phptidy's "diff" command.

- PHP extension: mbstring
    http://php.net/manual/en/book.tokenizer.php
    Only required if you want to use the configration variable $encoding to
    check the encoding of your PHP files.

 phptidy should work on most operation systems where PHP works.

Operating system compatibility

Out of the box, WP-phptidy should run on any platform capable of running phptidy (see above). I am also taking steps to ensure compatibility with OS/2.

How to use it

Unzip the contents of the zip (single directory) wherever convenient. Running WP-phptidy with no arguments yields a brief syntax reference:

Usage: phptidy.php command [files|options]
Commands:
  suffix   Write output into files with suffix .phptidy.php
  replace  Replace files and backup original as .phptidybak
  diff     Show diff between old and new source
  source   Show processed source code of affected files
  files    Show files that would be processed
  tokens   Show source file tokens
  help     Display this message
Options:
  -v       Verbose messages
If no files are supplied on command line, they will be read from the config file.
See README and source comments for more information.

I've included the original readme from phptidy as a more complete command reference, as well as the README.markdown from scribu's github submission, and my own readme and changelog. Between these references, a working understanding of the tool should be readily obtained.

My changes to the original WP-phptidy

So far, I've added/updated the following:

  • Updated phptidy to 2.11.
  • Added new function: fix_logical_space, which defaults to true. This adds a space after "!" in statements such as !isset, for example.

Look for these enhancements which are on my roadmap:

  • Better detection of missing braces (curly brackets).
  • Space alignment for arrays, e.g.:

     

    $my_array = array(
    [tab]'foo'   => 'somevalue',
    [tab]'foo2'  => 'somevalue2',
    [tab]'foo3'  => 'somevalue3',
    [tab]'foo34' => 'somevalue3',
    );

License

WP-phptidy is released under GPL v2, as is phptidy. A copy of the license is included in the distribution.