Creating a Coda Plugin that uses Pear's Tools - PHP_Beautifier, PHP_CodeSniffer and others
I wanted a plugin to use Pear's CodeSniffer Package and PHP_Beautifier's Package, both using Pear's standard. Both are mainly used from the command line but I wanted to use them inside Coda through a shortcut so I dived in to create my first Coda plugin!
First and foremost here's the download: http://dl.dropbox.com/u/21516048/PearPHP.zip
but BE ADVISED of the requirements:
- PEAR needs to be in your path (in doubt? check here) and
- all packages (PHP_CodeSniffer, PHP_Beautifier, PHP_DocBlockGenerator and PHPMD) need to be installed for all commands to work
If you have any trouble with the path you can change the php file and simply hardcode the path. Plugins are stored in /Library/Application Support/Coda/Plug-ins/.
Basically both files are very simple. Here's the code for beautifier.php:
$path = 'PHP/Beautifier.php'; //pear should be in your path.
if (!file_exists($path)) { die(); }
require($path);
$source = '';
while ($inp = fread(STDIN,8192)) {
$source .= $inp;
}
$oToken = new PHP_Beautifier();
$oToken->addFilter('Pear');
$oToken->addFilter('DocBlock');
$oToken->addFilter('ArrayNested');
$oToken->addFilter('EqualsAlign');
//add other filters here
$oToken->setInputString($source);
$oToken->process(); // required
echo $oToken->show();
In Coda's Plugin Editor I set the Input/STDIN (what Coda sends to the script) to be the contents of the document and the output to replace the contents of the current document, the shortcut to Ctrl+Alt+B and that was it :)
CodeSniffer is a package that analyses php source code and provides a report of all violations to a given code standard (eg. lines longer than 85 characters). Here's the code for CodeSniffer:
//USER CONFIGURABLE OPTIONS///
$values['verbosity'] = 0;
$values['tabWidth'] = 4; //indent with 4 spaces
$values['encoding'] = 'utf-8';
$values['standard'] = 'PEAR'; //Squiz or a custom standard
$values['generator'] = '';
//END OF USER CONFIGURABLE OPTIONS///
$path = 'PHP/CodeSniffer/CLI.php'; //pear should be in your path.
if (!file_exists($path)) { die(); }
$file = getenv('CODA_FILEPATH'); //Coda sets this variable to the path of the current file being edited
$files = array($file);
$values['files'] = $files;
$values['reportFile'] = '/tmp/checkstyle.txt';
$values['interactive'] = false; //do not set this to true, Coda will crash
$values['sniffs'] = array();
$values['extensions'] = array();
$values['ignored'] = array();
$values['reports'] = array();
$values['errorSeverity'] = '';
$values['warningSeverity'] = '';
$values['local'] = '';
$values['reportWidth'] = '';
$values['showProgress'] = '';
$values['showSources'] = '';
require($path);
$phpcs = new PHP_CodeSniffer_CLI();
$phpcs->checkRequirements();
$numErrors = $phpcs->process($values);
echo file_get_contents($values['reportFile']);
In Coda's Plugin Editor I set the Input/STDIN to be none (I'm getting the file path from the special Coda environment variable CODA_FILEPATH) and the output to open a new document from the output - where Code_Sniffer will show its report -, the shortcut to Ctrl+Alt+S and that was it :)
I also added PHP Mess Detector and DocBLockGenerator into the mix. To be strict PHPMD is not a pear package but it does rely heavily on php_depend which is a pear package by the same author.
Anyway as you can see it's really simple to create a Coda Plugin. The only gotcha here is if Pear is not in your path (check this page for help) but you can always try to edit the files yourself, change the path and try your own settings. If you wanna do that go to /Library/Application Support/Coda/Plug-ins/Pear PHP, right click-> Show Package Contents and you'll see some folders with strange names inside Contents-Resources - there you'll find the php files themselves.
This plugin has been really useful to me - it's too easy to stray from the standard and to forget documentation, so this helps a bit by automating stuff.
Happy coding :)
