Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Benjamin Delespierre
Benjamin Delespierre

Posted on

     

How to setup Git commit hooks for PHP

This very simple script will run these checks each time you commit:

  • PHP Internal Linter to check your code for syntax errors
  • PHP Code Sniffer to check for coding standard (PSR-2)
  • PHPStan to analyse the code and find bugs

Overall it takes a few seconds and it analysesonly the staged files - those in green when you do agit status.

If a single check fails the error message is displayed and the commit is aborted.

Example:

[1/3] php lint            OK![2/3] code sniffer        OK![3/3] phpstan             NOK! 1/1 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100% ------ ---------------------------------------------------------------------   Line   CanSendInvitation.php                                                 ------ ---------------------------------------------------------------------   14     Parameter $lol of method App\Rules\CanSendInvitation::__construct()           has invalid typehint type App\Rules\Noob.                             ------ ---------------------------------------------------------------------  [ERROR] Found 1 error
Enter fullscreen modeExit fullscreen mode

Step 1: requirements

You're going to need some packages to validate your code so go ahead and install:

composerinstall--dev phpstan/phpstancomposerinstall--dev squizlabs/php_codesniffer
Enter fullscreen modeExit fullscreen mode

Step 2: create the hook

In.git/hooks/pre-commit write:

#!/usr/bin/env bash# get bash colors and styles here:# http://misc.flogisoft.com/bash/tip_colors_and_formattingC_RESET='\e[0m'C_RED='\e[31m'C_GREEN='\e[32m'C_YELLOW='\e[33m'function__run()#(step, name, cmd){localcolor output exitcodeprintf"${C_YELLOW}[%s]${C_RESET} %-20s""$1""$2"output=$(eval"$3" 2>&1)exitcode=$?if[[ 0==$exitcode|| 130==$exitcode]];thenecho-e"${C_GREEN}OK!${C_RESET}"elseecho-e"${C_RED}NOK!${C_RESET}\n\n$output"exit1fi}modified="git diff --diff-filter=M --name-only --cached  | grep\".php$\""ignore="resources/lang,resources/views,bootstrap/helpers,database/migrations,bin"phpcs="vendor/bin/phpcs--report=code--colors--report-width=80--standard=PSR2--ignore=${ignore}"__run "1/3" "php lint" "${modified} | xargs-r php-l"__run "2/3" "code sniffer" "${modified} | xargs-r${phpcs}"__run "3/3" "phpstan" "${modified} | xargs-r vendor/bin/phpstan analyse"
Enter fullscreen modeExit fullscreen mode

The line

ignore="resources/lang,resources/views,bootstrap/helpers,database/migrations,bin"
Enter fullscreen modeExit fullscreen mode

is project specific. It's the comma separated list of paths to ignore in the PHP Code Sniffer analysis. Typically, you add the paths for which the PSR-2 makes no sense, like configuration or translation files.

You may change it to whatever you want.


My team has been using and refining this script for years and it has saved us countless hours in refactoring. The sooner you detect errors the better.

Our real script includes more tests that are specific to our app like unit & behavioral test to ensure you broke nothing. I'm sure with a little imagination you are capable to adapt the script to your project.

I suggest you play around with it and share what you find out in the comments.


A usual don't forget to like and comment, that keeps me motivated to write more content for you!

Happy coding!

Top comments(5)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
mmayboy_ profile image
O ji nwayo e je
  • Location
    Nigeria
  • Joined

1) What's the difference between code sniffer and editor config? Is code sniffer a more language-specific form of formatting i.e. Looks into language specific things like naming convention?

2) Why do we need to runphp lint first? Isn't that what the first level of php-stan/psalm does?

CollapseExpand
 
elcotu profile image
Daniel Coturel
Java and PHP programmer.
  • Location
    Buenos Aires
  • Joined

Good post!

CollapseExpand
 
creativedevs profile image
CreativeDev
  • Joined

I have tried this but getting below error

xargs: ./vendor/bin/phpcs: No such file or directory

it is working directly but not from pre-commit

CollapseExpand
 
bdelespierre profile image
Benjamin Delespierre
I do all sorts of mischiefs with Laravel
  • Location
    Paris, France
  • Joined

Add PHPCS as a dev dependency withcomposer require --dev squizlabs/php_codesniffer.

CollapseExpand
 
hailong profile image
Hailong
  • Joined

It's a great sample to start with.

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

I do all sorts of mischiefs with Laravel
  • Location
    Paris, France
  • Joined

More fromBenjamin Delespierre

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp