Skip to content

Instantly share code, notes, and snippets.

@lexoyo
Forked from k3n/lint-all-files-recursive.php
Created June 12, 2015 16:04
Show Gist options
  • Save lexoyo/9c1270e1811988b08e67 to your computer and use it in GitHub Desktop.
Save lexoyo/9c1270e1811988b08e67 to your computer and use it in GitHub Desktop.
<?php
/**
* Recurses each directory and runs PHP's lint function against each file
* to test for parse errors.
*
* @param string $dir the directory you would like to start from
* @return array the files that did not pass the test
*/
function lint( $dir = 'C:\dev\\' )
{
static $failed = array();
foreach ( new RecursiveDirectoryIterator($dir) as $path => $objSplFileInfo )
{
// recurse if dir
if ( $objSplFileInfo->isDir() )
{
if ( stristr( $objSplFileInfo->getFileName(), '.' ) !== false )
{
continue;
}
lint( $objSplFileInfo->getPathName() );
continue;
}
// are there any non-dirs that aren't files?
if ( !$objSplFileInfo->isFile() )
{
throw new UnexpectedValueException( 'Not a dir and not a file?' );
}
// skip non-php files
if ( preg_match( '#\.php$#', $objSplFileInfo->getFileName() ) !== 1 )
{
continue;
}
// perform the lint check
$result = exec( 'php -l '. escapeshellarg($objSplFileInfo) );
if ( preg_match( '#^No syntax errors detected in#', $result ) !== 1 )
{
$failed[ $objSplFileInfo->getPathName() ] = $result;
echo $failed, ' = ', $result;
}
}
return $failed;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment