Setup pre-commit hook in php projects for lint, code standard, test cases etc.
- php >= 5.6
- composer
- git
You must add the following line to the composer.json
file:
{
"require-dev": {
"bruli/php-git-hooks": "~4.1"
}
}
Or you can write in your console:
composer require bruli/php-git-hooks --dev
First, you will need to add the following lines to your composer.json
"scripts": {
"post-install-cmd": [
"PhpGitHooks\\Infrastructure\\Composer\\ConfiguratorScript::buildConfig"
],
"post-update-cmd": [
"PhpGitHooks\\Infrastructure\\Composer\\ConfiguratorScript::buildConfig"
]
}
Then, launch composer install
and composer should ask you about configuration
Note: If you have php-git-hooks.yml
then you can skip above step.
php-git-hooks.yml
looks like follow:
pre-commit:
enabled: true
execute:
php-cs-fixer:
enabled: true
levels:
psr0: true
psr1: true
psr2: true
symfony: true
options: "--fixers=short_array_syntax --diff"
phpunit:
enabled: true
random-mode: true
options: '<some options>'
strict-coverage:
enabled: true
minimum: 90
guard-coverage:
enabled: true
message: 'WARNING!!, your code coverage is lower.'
phplint: true
phpcs:
enabled: true
standard: PSR2
phpmd:
enabled: true
options: '<some options>'
composer: true
message:
right-message: 'HEY, GOOD JOB!!'
error-message: 'FIX YOUR CODE!!'
commit-msg:
enabled: true
regular-expression: '#[0-9]{2,7}'
pre-push:
enabled: true
execute:
phpunit:
enabled: true
random-mode: true
options: '<some options>'
strict-coverage:
enabled: true
minimum: 90
guard-coverage:
enabled: true
message: 'WARNING!!, your code coverage is lower.'
message:
right-message: 'PUSH IT!!'
error-message: 'YOU CAN NOT PUSH CODE!!'
If you want use phpunit tool, you must create a phpunit.xml.dist in your project root directory. Alternatively you can copy from vendor/bruli/php-git-hooks/phpunit.xml.dist in your project root directory.
The same case that phpunit. You must create a PmdRules.xml in your project root directory or copy from php-git-hook directory.
If your project doesn't have a "bin/" directory, you can add this in your composer.json
file.
"config": {
"bin-dir": "bin"
}
The most easy way to enable hook is copy hook file into your .git/hooks directory.
You can enable this hooks with composer or manually executing
$ cp vendor/bruli/php-git-hooks/src/PhpGitHooks/Infrastructure/Hook/pre-commit .git/hooks
$ cp vendor/bruli/php-git-hooks/src/PhpGitHooks/Infrastructure/Hook/commit-msg .git/hooks
$ cp vendor/bruli/php-git-hooks/src/PhpGitHooks/Infrastructure/Hook/pre-push .git/hooks
Now, Git pre-commit has been sent. you can change configuration by changing `php-git-hooks.yml.
Sound good. Thanks.