Skip to content

Instantly share code, notes, and snippets.

@relipse
Last active April 25, 2024 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save relipse/735e22e9cfb477cc5a11dc4613e3c4b7 to your computer and use it in GitHub Desktop.
Save relipse/735e22e9cfb477cc5a11dc4613e3c4b7 to your computer and use it in GitHub Desktop.
##
# Generated by multiple prompts and iterations to PHPStorm's JetBrains AI #
# with some personal modifications of error messages. #
# 2024-04-25 #
# @see php_syntax_check --help
##
function php_syntax_check() {
verbose=false
binary="php"
recursive=false
isfile=false
dir="."
while (( "$#" )); do
var="$1"
case "$var" in
-h|--help)
echo "Usage: php_syntax_check [-r] [-v] [-b, --binary] [directory]"
echo ""
echo "Checks PHP files for syntax errors."
echo ""
echo "OPTIONS:"
echo " -r check files recursively"
echo " -v verbose mode, lists all files checked"
echo " -b, --binary choose php binary version, default is 'php'"
echo " file/directory file or directory to check (current directory is default)"
echo " -h, --help show this help message"
return 0
;;
-v|--verbose)
verbose=true
shift
;;
-b|--binary)
binary="$2"
shift 2
;;
-r)
recursive=true
shift
;;
*)
if [[ -d $var ]]; then
dir=$var
elif [[ -f $var ]]; then
dir=$var
isfile=true
else
echo "Invalid argument: $var"
return 1
fi
shift
;;
esac
done
syntax_error_found=false
check_syntax () {
local file=$1
output=$($binary -l "$file" 2>&1)
local status=$?
if [[ $status -ne 0 ]]; then
syntax_error_found=true
fi
if [[ "$verbose" = true && $status -eq 0 ]]; then
echo "No syntax errors detected in $file"
else
echo "$output"
fi
}
if [[ "$isfile" = true ]]; then
check_syntax $dir
elif [ "$recursive" = true ]; then
if [[ $(find "$dir" -type f -name '*.php' | wc -l) -eq 0 ]]; then
echo "There are no PHP files in the directory: $dir"
else
for file in $(find "$dir" -type f -name '*.php'); do
check_syntax "$file"
done
fi
else
if [[ $(ls "$dir"/*.php 2>/dev/null | wc -l) -eq 0 ]]; then
echo "There are no PHP files in the directory: $dir"
else
for file in "$dir"/*.php
do
check_syntax "$file"
done
fi
fi
if [ "$syntax_error_found" = false ]; then
echo "No syntax errors detected."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment