Showing posts with label syntax check. Show all posts
Showing posts with label syntax check. Show all posts

Tuesday, September 15, 2009

How to recursively check syntax of PHP files

The executable of PHP supports the '-l' option, that checks the syntax instead of parsing the file.
Using the command 'find', it's possibile to do a interesting operation: syntax checking of all the files recursively, to avoid parse errors in some script !!

find ./ -type f -name \*.php -exec php -l {} \; ";

the result will be a list of files, example:

No syntax errors detected in ./codebase/controller/competition.inc.php
No syntax errors detected in ./codebase/controller/feed_data.inc.php
Errors parsing ./codebase/controller/site/contact.inc.php
No syntax errors detected in ./codebase/controller/compare_prices.inc.php


We can improve the script and print only the file with suntax errors using 'grep'

find ./ -type f -name \*.php -exec php -l {} \; | grep "Errors parsing ";

To launch it from a PHP script

passthru('find ./ -type f -name \*.php -exec php -l {} \; | grep "Errors parsing " ');

Updated: To skip .svn directories add the option :
-not -regex '.*/.svn/*.*'
 

PHP and tips|PHP