Last active
February 5, 2020 16:09
-
-
Save biggianteye/410effd843ff88bda4b1e662c5defe5f to your computer and use it in GitHub Desktop.
Run PHP code sniffer over the files that are about to be committed. Takes into account both PHP 5 and 7.
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
<?xml version="1.0"?> | |
<!-- Location: $HOME/config/phpcs.xml --> | |
<ruleset name="burhan"> | |
<rule ref="Squiz.Commenting.FunctionComment"> | |
<!-- excluded because they can't be used for both php 5 and 7 --> | |
<!-- PHP 7 will make this complaint if scalar type hints (eg int) are missing --> | |
<exclude name="Squiz.Commenting.FunctionComment.ScalarTypeHintMissing" /> | |
<!-- PHP 5 will make this complaint if scalar type hints (eg. int) exist --> | |
<exclude name="Squiz.Commenting.FunctionComment.InvalidTypeHint" /> | |
</rule> | |
</ruleset> |
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
#!/bin/sh | |
# Run PHP code sniffer over the files that are about to be committed. | |
# Add to $GITROOT/.git/hooks/pre-commit | |
# Make sure the command can be run regardless of where you are in the | |
# project tree. | |
gitroot=$(git rev-parse --show-toplevel) | |
standards="phpcs.user.xml,phpcs.xml" | |
phpcs="docker run --rm -v $gitroot:/data -v $HOME/config/phpcs.xml:/data/phpcs.user.xml cytopia/phpcs -s --runtime-set ignore_warnings_on_exit true -p --colors -n --cache=cache/phpcs/precommit --extensions=php --ignore=*/vendor/* --standard=$standards" | |
# Work out what files are queued up for committing. | |
filesToCheck=$(git diff-index --name-only --cached --diff-filter=ACMR HEAD -- ) | |
# If there are any files to check, then check them. | |
# If there are any deleted files and phpcs passes, they will be included in | |
# the commit. | |
if [[ -n $filesToCheck ]] | |
then | |
$phpcs $filesToCheck | |
exit | |
fi |
Revision 9: Removed debug because I fail.
Revision 10: Use a dockerised phpcs. Saves having to install phpcs everywhere via composer.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revision 8: Removed deleted file checking because it was breaking
git commit --amend
.