Skip to content

Tooling

Vincent Garcia edited this page Mar 13, 2024 · 1 revision

Pre commit git hook

To be sure to pass the github actions of CS Fixer & PHPStan, we encourage you to use the pre commit hook.

It will :

  • Run CS Fixer, and make updates on your files
  • Run PHP Stan and cancel your commit if you need to do some improvement
  • Run the autoindex command to add the index.php file in new folders
  • Be sure header headers are correctly set with the correct license

ℹ️ You can add this snippet in your project structure ps_mbo/.git/hooks/pre-commit

#!/usr/bin/env bash

echo "php-cs-fixer pre commit hook start"

PHP_CS_FIXER="vendor/bin/php-cs-fixer"
PHP_STAN="vendor/bin/phpstan"
INDEX_FILE="php vendor/bin/autoindex prestashop:add:index"
HEADER_STAMP="php vendor/bin/header-stamp --exclude=vendor,node_modules,composer.json --license=assets/afl.txt"

$INDEX_FILE
$HEADER_STAMP

git add -A '*index.php'

FILES=$(git status --porcelain | grep -e '^\s*[AM].*\.php' | cut -c 3-)
if [ -z "$FILES" ]
then
  echo "No php files found in commit."
else
  echo "${FILES}"
  $PHP_CS_FIXER fix --config=.php-cs-fixer.dist.php --verbose "${FILES}"
  git add "${FILES}"
  test=$($PHP_STAN analyse --error-format=raw -c tests/phpstan/phpstan-8.0.4.neon --level=5 --memory-limit=-1 "${FILES}")

  if [ "$test" = "" ]; then
    echo 'PHPStan passed successfully'
  else
    echo "$test"
	 exit 1;
  fi
fi

echo "pre commit hook finish"
Clone this wiki locally