There are many scripts that recursively execute php -l on a set of files or directories. This is mine:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
#!/usr/bin/env bash set -o nounset # Recursively call `php -l` over the specified directories/files if [ -z "$1" ] ; then printf 'Usage: %s <directory-or-file> ...\n' "$(basename "$0")" exit 1 fi ERROR=false SAVEIFS=$IFS IFS=$'\n' while test $# -gt 0; do CURRENT=${1%/} shift if [ ! -f $CURRENT ] && [ ! -d $CURRENT ] ; then echo "$CURRENT cannot be found" ERROR=true continue fi for FILE in $(find $CURRENT -type f -name "*.php") ; do OUTPUT=$(php -l "$FILE" 2> /dev/null) # Remove blank lines from the `php -l` output OUTPUT=$(echo -e "$OUTPUT" | awk 'NF') if [ "$OUTPUT" != "No syntax errors detected in $FILE" ] ; then echo -e "$FILE:" echo -e " ${OUTPUT//$'\n'/\\n }\n" ERROR=true fi done done IFS=$SAVEIFS if [ "$ERROR" = true ] ; then exit 1 fi echo "No syntax errors found." exit 0 |
I store it in ~/bin and usually run it like this:
|
$ cd project $ phplint . No syntax errors found. |
There are a few interesting bash tricks that I picked up when I wrote this. Firstly, you need to set IFS to break on new line rather than space otherwise the find command doesn't work with spaces in file names. I also discovered… continue reading.
Posted on
13 August 2020 in Development, PHP