<file_sep><?php

namespace Naoned\PhpConvention;

use Composer\Script\Event;
use Symfony\Component\Process\Process;

class GitHook
{
    const GIT_HOOK_PATH         = '.git/hooks';
    const GIT_HOOK_SCRIPTS_PATH = '/githooks';

    public static function install(Event $event)
    {
        if (!is_dir(self::GIT_HOOK_PATH)) {
            $event->getIO()->write('Ignore GitHook Script because it isn\'t launch in a git repository.');
            return;
        }

        // Install pre-commit git hook
        $cmd = sprintf(
            'cp %1$s/%3$s %2$s/ && chmod +x %2$s/%3$s',
            __DIR__ . self::GIT_HOOK_SCRIPTS_PATH,
            self::GIT_HOOK_PATH,
            '*'
        );
        $process = new Process($cmd);
        $process->run(function ($type, $buffer) use ($event) { $event->getIO()->write($buffer, false); });
        if (!$process->isSuccessful()) {
            throw new \RuntimeException('CMD failed: '. $cmd);
        }
    }
}
<file_sep># php-convention
FROM php:5.6-cli

MAINTAINER <NAME> "<EMAIL>"

# Install git
RUN apt-get update && apt-get install -y \
        git \
        unzip

# Install composer
RUN curl -sS https://getcomposer.org/installer | php \
    && mv composer.phar /usr/local/bin/composer

RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

COPY composer.json /usr/src/app/
RUN composer install

COPY . /usr/src/app
<file_sep>#!/bin/bash
# Naoned - Php Convention pre-commit hook for git
#
# @author <NAME> <<EMAIL>>
# @author <NAME> <<EMAIL>>
#

function commit_valid {
	# Ascii generator : http://patorjk.com/software/taag/#p=display&h=0&f=ANSI%20Shadow&t=YEAH%20BABY%2C%20You%20rock%20!%0A%0A
	# echo -e "${COLOR_VALID}----------------------------------------------------------------------------------------------------------------------------------------------------\033[0m"
	echo -e "${COLOR_VALID}██╗   ██╗███████╗ █████╗ ██╗  ██╗    ██████╗  █████╗ ██████╗ ██╗   ██╗       ██╗   ██╗ ██████╗ ██╗   ██╗    ██████╗  ██████╗  ██████╗██╗  ██╗    ██╗\033[0m"
	echo -e "${COLOR_VALID}╚██╗ ██╔╝██╔════╝██╔══██╗██║  ██║    ██╔══██╗██╔══██╗██╔══██╗╚██╗ ██╔╝       ╚██╗ ██╔╝██╔═══██╗██║   ██║    ██╔══██╗██╔═══██╗██╔════╝██║ ██╔╝    ██║\033[0m"
	echo -e "${COLOR_VALID} ╚████╔╝ █████╗  ███████║███████║    ██████╔╝███████║██████╔╝ ╚████╔╝         ╚████╔╝ ██║   ██║██║   ██║    ██████╔╝██║   ██║██║     █████╔╝     ██║\033[0m"
	echo -e "${COLOR_VALID}  ╚██╔╝  ██╔══╝  ██╔══██║██╔══██║    ██╔══██╗██╔══██║██╔══██╗  ╚██╔╝           ╚██╔╝  ██║   ██║██║   ██║    ██╔══██╗██║   ██║██║     ██╔═██╗     ╚═╝\033[0m"
	echo -e "${COLOR_VALID}   ██║   ███████╗██║  ██║██║  ██║    ██████╔╝██║  ██║██████╔╝   ██║   ▄█╗       ██║   ╚██████╔╝╚██████╔╝    ██║  ██║╚██████╔╝╚██████╗██║  ██╗    ██╗\033[0m"
	echo -e "${COLOR_VALID}   ╚═╝   ╚══════╝╚═╝  ╚═╝╚═╝  ╚═╝    ╚═════╝ ╚═╝  ╚═╝╚═════╝    ╚═╝   ╚═╝       ╚═╝    ╚═════╝  ╚═════╝     ╚═╝  ╚═╝ ╚═════╝  ╚═════╝╚═╝  ╚═╝    ╚═╝\033[0m"
	exit 0
}

function commit_not_valid {
	# Ascii generator : http://patorjk.com/software/taag/#p=display&h=0&f=ANSI%20Shadow&t=you%20shall%20not%20pass%20!%0A
	# echo -e "${COLOR_NOT_VALID}--------------------------------------------------------------------------------------------------------------------------------------------------\033[0m"
	echo -e "${COLOR_NOT_VALID}██╗   ██╗ ██████╗ ██╗   ██╗    ███████╗██╗  ██╗ █████╗ ██╗     ██╗         ███╗   ██╗ ██████╗ ████████╗    ██████╗  █████╗ ███████╗███████╗    ██╗\033[0m"
	echo -e "${COLOR_NOT_VALID}╚██╗ ██╔╝██╔═══██╗██║   ██║    ██╔════╝██║  ██║██╔══██╗██║     ██║         ████╗  ██║██╔═══██╗╚══██╔══╝    ██╔══██╗██╔══██╗██╔════╝██╔════╝    ██║\033[0m"
	echo -e "${COLOR_NOT_VALID} ╚████╔╝ ██║   ██║██║   ██║    ███████╗███████║███████║██║     ██║         ██╔██╗ ██║██║   ██║   ██║       ██████╔╝███████║███████╗███████╗    ██║\033[0m"
	echo -e "${COLOR_NOT_VALID}  ╚██╔╝  ██║   ██║██║   ██║    ╚════██║██╔══██║██╔══██║██║     ██║         ██║╚██╗██║██║   ██║   ██║       ██╔═══╝ ██╔══██║╚════██║╚════██║    ╚═╝\033[0m"
	echo -e "${COLOR_NOT_VALID}   ██║   ╚██████╔╝╚██████╔╝    ███████║██║  ██║██║  ██║███████╗███████╗    ██║ ╚████║╚██████╔╝   ██║       ██║     ██║  ██║███████║███████║    ██╗\033[0m"
	echo -e "${COLOR_NOT_VALID}   ╚═╝    ╚═════╝  ╚═════╝     ╚══════╝╚═╝  ╚═╝╚═╝  ╚═╝╚══════╝╚══════╝    ╚═╝  ╚═══╝ ╚═════╝    ╚═╝       ╚═╝     ╚═╝  ╚═╝╚══════╝╚══════╝    ╚═╝\033[0m"
	echo ""
	echo -e "\e[41m\e[97mYour commit can not be validated. Fix the errors above and try to commit again.\033[0m"
	exit 1
}

TMP_STAGING=".tmp_staging"

COLOR_VALID="\e[32m"
COLOR_ERROR="\e[37m\e[43m"
COLOR_NOT_VALID="\e[31m"

PHP_FILE_PATTERN="\.(php|phtml)$"

PHPCS_BIN=bin/phpcs
PHPCS_CODING_STANDARD="$PWD/vendor/naoned/php-convention/phpcs/Iadvize/ruleset.xml"
PHPCS_IGNORE=
PHPCS_IGNORE_WARNINGS=1
PHPCS_ENCODING=utf-8

PHPMD_BIN=bin/phpmd
PHPMD_RULESET="$PWD/vendor/naoned/php-convention/phpmd/phpmd.xml"
PHPMD_FORMAT="text"


# check if phpcs binary exist
if [ ! -x $PHPCS_BIN ]; then
    echo -e "${COLOR_ERROR}PHP CodeSniffer bin not found or executable -> $PHPCS_BIN\033[0m"
    exit 1
fi

# check if phpmd binary exist
if [ ! -x $PHPMD_BIN ]; then
    echo -e "${COLOR_ERROR}PHP CodeSniffer bin not found or executable -> $PHPCS_BIN\033[0m"
    exit 1
fi

# stolen from template file
if git rev-parse --verify HEAD
then
    against=HEAD
else
    # Initial commit: diff against an empty tree object
    against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
fi

# this is the magic:
# retrieve all files in staging area that are added, modified or renamed
# but no deletions etc
FILES=$(git diff-index --name-only --cached --diff-filter=ACMR $against -- )

if [ "$FILES" == "" ]; then
    exit 0
fi



# match files against whitelist
FILES_TO_CHECK=""
for FILE in $FILES
do
    echo "$FILE" | egrep -q "$PHP_FILE_PATTERN"
    RETVAL=$?
    if [ "$RETVAL" -eq "0" ]
    then
        FILES_TO_CHECK="$FILES_TO_CHECK $FILE"
    fi
done

if [ "$FILES_TO_CHECK" == "" ]; then
    exit 0
fi

# create temporary copy of staging area
if [ -e $TMP_STAGING ]; then
    rm -rf $TMP_STAGING
fi
mkdir $TMP_STAGING

# Copy contents of staged version of files to temporary staging area
# because we only want the staged version that will be commited and not
# the version in the working directory
STAGED_FILES=""
for FILE in $FILES_TO_CHECK
do
  ID=$(git diff-index --cached HEAD $FILE | cut -d " " -f4)

  # create staged version of file in temporary staging area with the same
  # path as the original file so that the phpcs ignore filters can be applied
  mkdir -p "$TMP_STAGING/$(dirname $FILE)"
  git cat-file blob $ID > "$TMP_STAGING/$FILE"
  STAGED_FILES="$STAGED_FILES $TMP_STAGING/$FILE"
done


# Test PHPCS

# Install PhpCS Symfony 2 code standard
PHPCS_CODE_STANDARD_SYMFONY="$PWD/vendor/squizlabs/php_codesniffer/CodeSniffer.conf"
if [ ! -f "$PHPCS_CODE_STANDARD_SYMFONY" ]; then
	$PHPCS_BIN --config-set installed_paths $PWD/vendor/escapestudios/symfony2-coding-standard
fi

# execute the code sniffer
if [ "$PHPCS_IGNORE" != "" ]; then
    IGNORE="--ignore=$PHPCS_IGNORE"
else
    IGNORE=""
fi

if [ "$PHPCS_ENCODING" != "" ]; then
    ENCODING="--encoding=$PHPCS_ENCODING"
else
    ENCODING=""
fi

if [ "$PHPCS_IGNORE_WARNINGS" == "1" ]; then
    IGNORE_WARNINGS="-n"
else
    IGNORE_WARNINGS=""
fi

OUTPUT_PHPCS=$($PHPCS_BIN -s --colors $IGNORE_WARNINGS --standard=$PHPCS_CODING_STANDARD $ENCODING $IGNORE $TMP_STAGING)
RETVAL_PHPCS=$?

# Test PHPMD

OUTPUT_PHPMD=$($PHPMD_BIN $TMP_STAGING $PHPMD_FORMAT $PHPMD_RULESET)
RETVAL_PHPMD=$?

# delete temporary copy of staging area
rm -rf $TMP_STAGING

if [ $RETVAL_PHPCS -ne 0 ] || [ $RETVAL_PHPMD -ne 0 ]; then
	if [ $RETVAL_PHPCS -ne 0 ]; then
		echo -e "${COLOR_ERROR} You have PHPCS errors. Fix them ! \033[0m"
		echo "$OUTPUT_PHPCS"
		echo ""
	fi

	if [ $RETVAL_PHPMD -ne 0 ]; then
		echo -e "${COLOR_ERROR} You have PHPMD errors. Fix them ! \033[0m"
		echo "$OUTPUT_PHPMD"
		echo ""
	fi

    commit_not_valid
fi

exit 0
