Skip to content

Instantly share code, notes, and snippets.

@nfreear
Created May 8, 2012 14:43
Show Gist options
  • Select an option

  • Save nfreear/2635750 to your computer and use it in GitHub Desktop.

Select an option

Save nfreear/2635750 to your computer and use it in GitHub Desktop.
Nick's quick php-lint
<?php
/**
* Nick's quick php-lint.
*
* > php ../../../../../phplint.php
*
* Copyright 2012-05-08 N.D.Freear.
*/
$dir = getcwd();
$count = php_lint($dir);
echo "> php-lint checked $count files.". PHP_EOL;
function php_lint($dir) {
static $count = 0;
$file_list = scandir($dir);
$log_file = str_replace('.php', '', __FILE__) .'.log';
foreach ($file_list as $file) {
if ($file[0] == '.') continue;
if (is_dir($dir .'/'. $file)) {
echo "> Recursing $file". PHP_EOL;
php_lint($dir .'/'. $file); // Recurse.
continue;
}
$ext = pathinfo($dir .'/'. $file, PATHINFO_EXTENSION);
if (preg_match('/diff|patch|txt/', $ext)) {
echo "> Ignoring $file". PHP_EOL;
continue;
}
$res = my_system('php -l '. $dir .'/'. $file);
if (FALSE === strpos($res, 'No syntax')) {
echo '> '. $res .PHP_EOL;
exit (1);
}
$count++;
}
return $count;
}
function my_system($cmd ) {
#echo '> '. $cmd .PHP_EOL;
$handle = popen(escapeshellcmd($cmd), 'r'); //2>&1
$result = fread($handle, 2096);
pclose($handle);
//echo $result .PHP_EOL;
// Todo: need to check stderr!
if (FALSE !== strpos($result, 'fatal')) {
echo '> Fatal error. Aborting..'.PHP_EOL;
exit (1);
}
return $result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment