Last active
August 13, 2020 14:53
-
-
Save akrabat/138951fc3f0ef85e3d5f8ff4672fbf39 to your computer and use it in GitHub Desktop.
RRecursive php lint
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
set -o errexit
will cause thephp -l ...
to exit the script when parsing fails.if [ -z $1 ]
does not work for me,if [ $# -lt 1]
did.The rest is beautiful, thanks!