Skip to content

Instantly share code, notes, and snippets.

@westonruter
Last active December 20, 2015 11:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save westonruter/6123121 to your computer and use it in GitHub Desktop.
Save westonruter/6123121 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Run php files in the repo through phpcs, opting for a subset of only checking staged or modified files
# By @westonruter
status_filter='^.M'
status_filter_description="Linting modified files only"
while getopts ":sa" opt; do
case $opt in
s)
status_filter='^M'
status_filter_description="Linting staged files only"
;;
a)
status_filter=''
status_filter_description="Linting everything"
;;
esac
done
echo $status_filter_description
shift $((OPTIND-1))
if [ $# == 0 ]; then
paths=$(pwd)
else
paths=$@
fi
echo "Linting path(s) $paths"
if [ -z $status_filter ]; then
php_files=$(find $paths -type f -name '*.php')
else
php_files=$(
git status --porcelain -s $paths |
grep -E "$status_filter" |
cut -c4- |
grep -E '\.php$'
)
fi
project_dir=$(dirname $0)
if [ -n "$php_files" ]; then
phpcs --standard=$project_dir/codesniffer.ruleset.xml $php_files
else
echo "Nothing to lint"
fi
  • Rewrite wrapper script to be in PHP instead of Bash, for Windows compat.
  • Potentially write a separate PHP wrapper/replacement for phpcs, one that instantiates PHP_CodeSniffer and does custom handling of the results.
  • Allow subset to be done on a file level, to only check lines that have been staged or edited.
  • Improve method for obtaining the ruleset.xml, and making it optional.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment