PHP pre-push
CI scripts / code quality that will run on every
git push
CI tools that can be run:
- PHP_CodeSniffer
- PHP Lint
- PHPMD - PHP Mess Detector
- PHPStan - PHP Static Analysis Tool
- PHPUnit – The PHP Testing Framework
Must be cloned to a folder in the project root:
$ git clone https://github.com/allysonsilva/php-pre-push code-quality
Note: Folder name in above command is called code-quality
Copy the pre-push
executable to the GIT hooks folder:
# Create new hooks folder:
mkdir .git/hooks
# Move pre-push to GIT hooks folder:
mv ./code-quality/pre-push .git/hooks/pre-push
# Don't forget to make the pre-push file executable:
chmod +x .git/hooks/pre-push
A file .env.prepush
can be created in the project root to have some environment variables that will be used in the execution of each script.
The following variables can be used:
run_phpcs
: Booleantrue
orfalse
value that will indicate whether the phpcs script will be executed. Default valuetrue
.run_phpmd
: Booleantrue
orfalse
value that will indicate whether the phpmd script will be executed. Default valuetrue
.run_phpstan
: Booleantrue
orfalse
value that will indicate whether the phpstan script will be executed. Default valuetrue
.run_phpunit
: Booleantrue
orfalse
value that will indicate whether the phpunit script will be executed. Default valuetrue
.diff_filter
: Value of the--diff-filter
argument of thegit diff
command. Default valueACM
.parent_branch
: Source/parent branch, which will be used to compare/recover files that have changed. Default valuemain
.
This will work automatically before every push.
To execute each command individually it is possible, for example: ./code-quality/phpstan.sh
. Use the arguments of --diff-filter
or --branch
.
A bash script that runs php -l
against stage files that are php. Assumes php
is a global executable command. Will exit when it hits the first syntax error.
The following command is executed with PHP Lint:
php -l -d display_errors=On
PHP_CodeSniffer is a set of two PHP scripts; the main
phpcs
script that tokenizes PHP, JavaScript and CSS files to detect violations of a defined coding standard, and a secondphpcbf
script to automatically correct coding standard violations. PHP_CodeSniffer is an essential development tool that ensures your code remains clean and consistent.
The following command is executed with PHPCS:
php ./vendor/bin/phpcs {file}
PHPMD is a spin-off project of PHP Depend and aims to be a PHP equivalent of the well known Java tool PMD. PHPMD can be seen as an user friendly frontend application for the raw metrics stream measured by PHP Depend.
The following command is executed with PHPMD:
php ./vendor/bin/phpmd {files} ansi phpmd.xml.dist --suffixes php
To generate the PHPMD baseline, use the phpmd_generate_baseline=true
environment variable in the .env.pre-push
file.
PHPStan scans your whole codebase and looks for both obvious & tricky bugs. Even in those rarely executed if statements that certainly aren't covered by tests.
The following command is executed with PHPStan:
php ./vendor/bin/phpstan analyse --error-format=table --ansi {files}
To generate the PHPStan baseline, use the phpstan_generate_baseline=true
environment variable in the .env.pre-push
file.
PHPUnit is a programmer-oriented testing framework for PHP. It is an instance of the xUnit architecture for unit testing frameworks.
The following command is executed with PHPUnit:
php ./vendor/bin/phpunit {files}
The MIT License (MIT). Please see License File for more information.