Skip to content

Instantly share code, notes, and snippets.

@k3n
Created February 16, 2012 16:26
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save k3n/1846220 to your computer and use it in GitHub Desktop.
Save k3n/1846220 to your computer and use it in GitHub Desktop.
Applies PHP's lint check to all PHP files in a directory.
<?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(), '.svn' ) !== 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;
}
?>
@igbanam
Copy link

igbanam commented Feb 1, 2016

There is a more beautiful _parallel _one-liner

find . -name "*.php" -print0 | xargs -0 -n1 -P8 php -l

@thomasplevy
Copy link

@igravity this is beautiful thank you

@vdwrcarlson
Copy link

@igbanam note that xargs will stop executing on the first error. I use this instead:

find . -type f -name '*.php' -exec php -l {} \; |grep -v "No syntax errors detected"

@fbrinker
Copy link

You may want to invert the return code of grep so a CI build can fail, if grep found some errors:
find . -type f -name '*.php' -exec php -l {} \; | (! grep -v "No syntax errors detected" )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment