First, we need a development dependency specified to install phpcs. It looks something like this:
{
"require-dev": [
"squizlabs/php_codesniffer": "2.0.*@dev"
]
}
Composer has a handy schema entry called scripts. It supports a script hook post-install-cmd. We will use this to install a git pre-commit hook. Adding to our example above:
{
"require-dev": [
"squizlabs/php_codesniffer": "2.0.*@dev"
],
"scripts": {
"post-install-cmd": [
"bash contrib/setup_hooks.sh"
]
}
}
This will run a bash script called setup.sh when the command composer install is run.
In our contrib/setup_hooks.sh
, we will need to copy a pre-commit script into the .git/hooks directory
This will copy our pre-commit script from the contrib directory to the hooks section of the special git directory and make it executable.
Whenever a contributing developer attempts to commit their code, it will run our pre-commit script. Now all we need to do is run the code sniffer rules on relavent files specific to this commit.
contrib/pre-commit
Based on this tutorial http://tech.zumba.com/2014/04/14/control-code-quality/ with some changes.
https://github.com/stevegrunwell/wp-enforcer