When programming, lint checking your source code can be useful to detect low level issues like syntax errors, undefined or unused variables. The PHP command line interpreter has an option -l
/--syntax-check
to do a syntax check of the provided PHP file, instead of executing it:
$ php -l file_with_syntax_error.php
PHP Parse error: syntax error, unexpected T_VARIABLE in file_with_syntax_error.php on line 5
Errors parsing file_with_syntax_error.php
Unfortunately, it only allows to check one file at a time. For example, the following does not work as expected:
$ php -l file1.php file2.php file_with_syntax_error.php
No syntax errors detected in file1.php
If you want to lint-check (a complete tree of) multiple PHP files, you have to use some kind of loop (e.g. in Bash) to check them one by one.
I recently was faced with this issue, but also wanted to have some parallelism, to speed up the process by putting multiple cores a work, instead of checking one file at a time. After googling a bit I found the following (command line) solutions.
Use find + xargs's parallel mode
With the appropriate options, we can set up a find+xargs combo to achieve what we want: xargs option -n
to only provide one argument to php
at a time and xargs option -P
to dispatch multiple jobs (here eight) in parallel:
$ find . -name "*.php" -print0 | xargs -0 -n1 -P8 php -l
Makefile trick
In case you want to integrate the lint check in a Makefile work flow, you could consider the following Makefile trick:
files := $(shell find . -name \*.php)
.PHONY: ${files}
${files}:
php -l $@
.PHONY: lint
lint: ${files}
echo Lint finished
Invoke this with the option -j
/--jobs
for parallel jobs, for example:
$ make -j 8 lint