Skip to content

Instantly share code, notes, and snippets.

@sdavara
Last active July 25, 2023 16:36
Show Gist options
  • Save sdavara/154d02c65fde507c797d3779fe9efd35 to your computer and use it in GitHub Desktop.
Save sdavara/154d02c65fde507c797d3779fe9efd35 to your computer and use it in GitHub Desktop.
Git-pre-commit-hooks for PHP/Laravel Project

Pre Commit Setup

Setup pre-commit hook in php projects for lint, code standard, test cases etc.

Prerequisites

  • php >= 5.6
  • composer
  • git

Installation

Step 1: Composer

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

Step 2: Configuration

Using Composer

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!!'

Config file for phpunit.

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.

Config file for phpmd.

The same case that phpunit. You must create a PmdRules.xml in your project root directory or copy from php-git-hook directory.

Bin directory configuration.

If your project doesn't have a "bin/" directory, you can add this in your composer.json file.

"config": {
    "bin-dir": "bin"
}

Step 3: Enabling hooks.

The most easy way to enable hook is copy hook file into your .git/hooks directory.

For pre-commit hook:

You can enable this hooks with composer or manually executing

$ cp vendor/bruli/php-git-hooks/src/PhpGitHooks/Infrastructure/Hook/pre-commit .git/hooks

For commit-msg hook:

$ cp vendor/bruli/php-git-hooks/src/PhpGitHooks/Infrastructure/Hook/commit-msg .git/hooks

For pre-push hook:

$ 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.

@decodedmrq
Copy link

Sound good. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment