Skip to content

Instantly share code, notes, and snippets.

@dprevite
Created May 9, 2012 16:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dprevite/2645980 to your computer and use it in GitHub Desktop.
Save dprevite/2645980 to your computer and use it in GitHub Desktop.
PHP Farm Linting for each installed version of PHP
#!/usr/bin/php
<?php
define('PHPFARM_BIN_PATH', '/home/dprevite/phpfarm/inst/bin/'); // WITH trailing slash
define('NL', "\n");
/**
* undocumented function
*
* @return void
**/
function get_installed_php_versions() {
$installed_versions = array();
$files = new DirectoryIterator(PHPFARM_BIN_PATH);
foreach($files as $file) {
#if(preg_match('/\Aphp-([0-9]+)\.([0-9]+)\.([0-9]+)\Z/im', (string)$file->getFilename())) {
if(preg_match('/\Aphp-([5-9]+)\.([0-9]+)\.([0-9]+)\Z/im', (string)$file->getFilename())) { // Version 5 and higher
$installed_versions[] = (string)$file->getFilename();
}
}
return $installed_versions;
}
$versions = get_installed_php_versions();
print_r($versions);
$versions = array('php-5.1.6');
foreach($versions as $version) {
echo NL . NL . 'Linting with ' . $version . NL;
$PHP = PHPFARM_BIN_PATH . $version;
echo $PHP . NL;
// ===========
// = Globals =
// ===========
$count = 0; // total files checked
$errors = array();
$options = setOptions(array(
'quiet' => false,
'recurse' => true,
));
if($options['quiet']) {
ob_start();
}
// =============
// = Scan path =
// =============
$files = getPipedFiles();
$path = $_SERVER['PWD']; // Default to execution directory
// Piped files present
if($files) {
foreach($files as $file) {
checkFile("$path/$file");
}
} else { // Use arguments
if ($_SERVER['argc'] > 1) {
$last = end($_SERVER['argv']);
if (substr($last, 0, 1) != '-') {
$path = $last; // snag last argument, if it wasn't an option switch
}
}
if(is_dir($path)) {
checkDirectoryContents($path);
} elseif (is_file($path)) {
checkFile($path);
} else {
echo "$path is not a file or directory.\n";
showHelp() AND exit(1);
}
}
if($options['quiet']) {
ob_end_clean();
}
echo "\n$count files checked, " . count($errors) . ' errors.';
echo "\n", implode($errors,'');
} // end foreach versions
function checkDirectoryContents($dir) {
global $options, $i, $errors, $count;
$contents = scandir($dir);
foreach($contents as $content) {
if ($content == '.' || $content == '..') {
continue;
}
$path = "$dir/$content";
// Recurse into directories
if (is_dir($path) && $options['recurse']) {
checkDirectoryContents($path);
} // if is_dir
else {
checkFile($path);
} // !is_dir
} // foreach
} // function checkDirectoryContents
function checkFile($path) {
global $count, $errors, $PHP;
// echo "$path\n";
// Skip non-php files
if (substr($path, -4) != '.php') {
return false;
}
if (($count % 60 == 0)) {
echo "\n";
}
$error = `$PHP -l $path 2>&1 1> /dev/null`;
if ($error) {
$errors[] = $error;
echo 'E';
}
else {
echo '.';
}
$count++;
}
function getPipedFiles() {
$files = array();
stream_set_blocking(STDIN,FALSE);
while ($line = trim(fgets(STDIN))) {
$files[] = $line;
}
return $files;
}
function setOptions($options) {
$args = array_keys(getopt('qRh'));
foreach ($args as $arg) {
switch ($arg) {
case 'q':
case 'quiet':
$options['quiet'] = true;
break;
case 'R':
case 'recursive':
$options['recurse'] = true;
break;
#case 'm':
#case '--major':
# $options['major'] = (int)$major;
#break;
case 'h':
case 'help':
default:
showHelp() AND exit(0);
} // Switch
} // Foreach args
return $options;
} // function setOptions
function showHelp() {
echo <<<HELP
usage: lint [-qR] [path]
options:
-q, --quiet: disable verbose output
-R, --recursive: recurse into subdirectories
-h, --help: display this help screen
-m, --major: major version number to run against
HELP;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment