PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Quality Assurance on PHP projects - PHPLint

Note: This article was originally published at Planet PHP on 15 July 2011.
Planet PHP
PHP LintPHP Lint is probably the easiest way to validate your code on syntax errors, but it's also the most overlooked feature of PHP on command line.
Provides a convenient way to perform only a syntax check on the given PHP code. On success, the text No syntax errors detected in is written to standard output and the shell return code is 0. On failure, the text Errors parsing in addition to the internal parser error message is written to standard output and the shell return code is set to -1.
This option won't find fatal errors (like undefined functions). Use the -f to test for fatal errors too.Quote taken fromAhttp://www.php.net/manual/en/features.commandline.options.php.
Example of detecting failures onlyuser@machine: $ /usr/bin/php -l /path/to/myfile.php
Example of detecting failures and fatal errorsuser@machine: $ /usr/bin/php -lf /path/to/myfile.php
Since this tool also returns numeric return codes, you can use it immediately as a pre-commit hook of your favorite revision control system like Git or Subversion. And with PHP on command line, you can create your own hooks with PHP.A
Example of a Git pre-commit hook taken fromATravis Swicegood's article "Don't submit that error" on the PHP Advent pages.#!/usr/bin/php// author: Travis Swicegood// link:Ahttp://phpadvent.org/2008/dont-commit-that-error-by-travis-swicegood

$output = array();$return = 0;$php = '/usr/bin/php';
exec('git rev-parse --verify HEAD 2 /dev/null', $output, $return);$against = $return == 0 ? 'HEAD' : '4b825dc642cb6eb9a060e54bf8d69288fbee4904';
exec("git diff-index --cached --name-only {$against}", $output);
$filename_pattern = '/\.php$/';$exit_status = 0;
foreach ($output as $file) {AAA if (!preg_match($filename_pattern, $file)) {AAAAAAA // don't check files that aren't PHP"/

Truncated by Planet PHP, read more at the original (another 15213 bytes)