InfluxOW/laravel_ddd_ecommerce

View on GitHub
.githooks/pre-commit

Summary

Maintainability
Test Coverage
#!/usr/bin/env bash

#########################
#                       #
#     Initializing      #
#                       #
#########################

SAIL_BIN=./tools/sail/vendor/bin/sail

function sail() {
    $SAIL_BIN "$@" >/dev/null 2>&1
}

#########################
#                       #
#       Starting        #
#                       #
#########################

COMPOSER=$(git diff --name-only --diff-filter=ACMR HEAD | grep composer)

if [ "$COMPOSER" != "" ]; then
    # Composer Validation

    echo "[PRE-COMMIT] Checking Composer..."

    sail composer validate --strict --quiet

    if [ $? != 0 ]; then
        echo "[PRE-COMMIT] Invalid composer.json have been detected."
        echo "[PRE-COMMIT] Run Composer Validate manually and fix existing errors."
        exit 1
    fi

    sail composer normalize
    git add $COMPOSER
fi

# All PHP files in staging area (no deletions)
FILES=$(git diff --name-only --diff-filter=ACMR HEAD | grep \\.php)

if [ "$FILES" != "" ]; then
    # Code Quality - Rector

    echo "[PRE-COMMIT] Checking Rector..."

    sail composer exec rector process -- --dry-run $FILES

    if [ $? != 0 ]; then
        echo "[PRE-COMMIT] Rector errors have been detected."
        echo "[PRE-COMMIT] Running Rector fixer..."

        sail composer exec rector process -- $FILES

        echo "[PRE-COMMIT] All errors are fixed automatically."
        git add $FILES
    fi

    # Code Style - PHPCS

    echo "[PRE-COMMIT] Checking PHPCS..."

    sail composer exec phpcs -n $FILES

    if [ $? != 0 ]; then
        echo "[PRE-COMMIT] Coding standards errors have been detected."
        echo "[PRE-COMMIT] Running PHP Code Beautifier and Fixer..."

        sail composer exec phpcbf -- -n $FILES

        echo "[PRE-COMMIT] Checking PHPCS again..."

        sail composer exec phpcs -- -n $FILES

        if [ $? != 0 ]; then
            echo "[PRE-COMMIT] PHP Code Beautifier and Fixer wasn't able to solve all problems."
            echo "[PRE-COMMIT] Run PHPCS manually and fix existing errors."
            exit 1
        fi

        echo "[PRE-COMMIT] All errors are fixed automatically."
        git add $FILES
    fi

    # Code Style - Laravel Pint

    echo "[PRE-COMMIT] Checking Laravel Pint..."

    sail composer exec pint -- --test

    if [ $? != 0 ]; then
        sail composer exec pint
        echo "[PRE-COMMIT] All errors are fixed automatically."
        git add $FILES
    fi

    # Code Style - PHPInsights

    echo "[PRE-COMMIT] Checking PHPInsights..."
    sail php artisan insights --quiet

    if [ $? != 0 ]; then
        echo "[PRE-COMMIT] PHPInsights found problems."
        echo "[PRE-COMMIT] Run PHPInsights manually and fix existing errors."
        exit 1
    fi

    # Code Quality

    echo "[PRE-COMMIT] Checking PHPStan..."
    sail composer exec phpstan analyse -- --memory-limit=-1

    if [ $? != 0 ]; then
        echo "[PRE-COMMIT] PHPStan found problems."
        echo "[PRE-COMMIT] Run PHPStan manually and fix existing errors."
        exit 1
    fi
fi

DEPTRACK=$(git diff --name-only --diff-filter=ACMR HEAD | grep deptrac)

if [ "$DEPTRACK" != "" ] || [ "$FILES" != "" ]; then
    # Architecture Validation

    echo "[PRE-COMMIT] Checking Deptrac..."

    sail composer exec deptrac -- --quiet

    if [ $? != 0 ]; then
        echo "[PRE-COMMIT] Deptrac found problems."
        echo "[PRE-COMMIT] Run Deptrac manually and fix existing errors."
        exit 1
    fi
fi

echo "[PRE-COMMIT] No errors found."

exit $?