预提交钩子,用于检查Symfony的PHP代码覆盖率和代码嗅探器

How can I do a pre-commit hook for PHP Symfony code to analyze code coverage and code sniffer report? What are commands and tools I should use?

As of now, I can generate a code coverage report using PHPUnit in clover format:

#!/bin/bash

echo "##################################################################################################"
echo "Starting PHPUnit tests : "`date "+%y-%m-%d %H-%M-%S"`
echo "##################################################################################################"

php app/console -e=dev doctrine:database:drop --force
php app/console -e=dev doctrine:database:create
php app/console -e=dev doctrine:schema:create
php app/console -e=dev -n doctrine:fixtures:load
#phpunit -c app --coverage-html build/html
phpunit -c app --log-junit build/unit.xml
'[' -f build/coverage.xml ']'
phpunit -c app --coverage-clover build/coverage.xml
php app/console -e=dev doctrine:schema:drop --force
php app/console -e=dev doctrine:database:drop --force
echo "Finishing Cron at "`date "+%y-%m-%d %H-%M-%S"`
echo "Cron Task Complete"
echo "##################################################################################################"

If your bash script works as expected, you just need to name it pre-commit and put it in your git hooks: /path/to/repo/.git/hooks. You'll find some sample in this directory.

For more information about git hooks: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

Then, for you code sniffer, I recommend https://github.com/squizlabs/PHP_CodeSniffer.

There is also https://github.com/phpro/grumphp that will do everything for you.

No need to use grumphp when you can easily accomplish it with client side git hooks. Example below runs php-cs-fixer and prevents pushing broken code to github repository when you run git push origin ......

your_project_folder/.git/hooks/pre-push

#!/bin/sh

if [ -f ./bin/php-cs-fixer ]
then
    ./bin/php-cs-fixer fix --dry-run --verbose --diff src
    if [ $? -ne 0 ]
    then
        printf "
\t\033[1;31m[PHP-CS-Fixer] Push Aborted

\033[0m"

        return 1
    fi
fi

Grant permissions

chmod +x .git/hooks/pre-push

Test

git push origin .....

See example here.