diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b3f6c54..3d5d9a9 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,7 +1,8 @@ # Pre-commit # @see http://pre-commit.com/ +repos: - repo: git://github.com/pre-commit/pre-commit-hooks - sha: master + rev: v1.3.0 hooks: - id: check-yaml files: \.(yaml|yml)$ \ No newline at end of file diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml new file mode 100644 index 0000000..48ef745 --- /dev/null +++ b/.pre-commit-hooks.yaml @@ -0,0 +1,42 @@ +- id: php-lint-all + name: PHP Syntax Check (Comprehensive) + description: Check PHP Syntax on ALL PHP staged files with user friendly messages and colors + entry: pre_commit_hooks/php-lint.sh + language: script + files: \.php$ + args: [-s all] + +- id: php-lint + name: PHP Syntax Check (Quick) + description: Runs php -l on all staged files. Exits when it hits the first errored file + entry: php -l + language: system + files: \.php$ + +- id: php-unit + name: PHP Unit + description: Run the full php unit test. Checks which PHPUnit executable is available first and then runs it. Preference order is vendor/bin, phpunit and phpunit.phar. + entry: pre_commit_hooks/php-unit.sh + language: script + files: \.php$ + +- id: php-cs + name: PHP Codesniffer + description: Run php codesniffer against all staged files. + entry: pre_commit_hooks/php-cs.sh + language: script + files: \.php$ + +- id: php-cbf + name: PHP Codesniffer (Code Beutifier and Formatter) + description: Run php codesniffer against all staged files. + entry: pre_commit_hooks/php-cbf.sh + language: script + files: \.php$ + +- id: php-cs-fixer + name: PHP Coding Standards Fixer + description: Run php coding standards fixer against all staged files. + entry: pre_commit_hooks/php-cs-fixer.sh + language: script + files: \.php$ diff --git a/pre_commit_hooks/helpers/locate.sh b/pre_commit_hooks/helpers/locate.sh index 2b3db55..464e12e 100644 --- a/pre_commit_hooks/helpers/locate.sh +++ b/pre_commit_hooks/helpers/locate.sh @@ -15,7 +15,7 @@ if [ -f "$vendor_command" ]; then elif hash $global_command 2>/dev/null; then exec_command=$global_command elif [ -f "$local_command" ]; then - phpcsfixer_command=$prefixed_local_command + exec_command=$prefixed_local_command else echo -e "${bldred}No valid ${title} found!${txtrst}" echo "Please have one available as one of the following:" diff --git a/pre_commit_hooks/php-cbf.sh b/pre_commit_hooks/php-cbf.sh index 2b6a02f..f5944bd 100755 --- a/pre_commit_hooks/php-cbf.sh +++ b/pre_commit_hooks/php-cbf.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh # Bash PHP Code Beautifier and Fixer Hook # This script fails if the PHP Code Beautifier and Fixer output has the word "ERROR" in it. @@ -19,7 +19,7 @@ msg_color_yellow='\e[0;33m' msg_color_none='\e[0m' # No Color # Loop through the list of paths to run PHP Code Beautifier and Fixer against -echo -en "${msg_color_yellow}Begin PHP Code Beautifier and Fixer ...${msg_color_none} \n" +echo -e "${msg_color_yellow}Begin PHP Code Beautifier and Fixer ...${msg_color_none} \n" phpcbf_local_exec="phpcbf.phar" phpcbf_command="php $phpcbf_local_exec" @@ -50,7 +50,7 @@ echo "Running command $phpcbf_command" command_result=`eval $phpcbf_command` if [[ $command_result =~ ERROR ]] then - echo -en "${msg_color_magenta}Errors detected by PHP Code Beautifier and Fixer ... ${msg_color_none} \n" + echo -e "${msg_color_magenta}Errors detected by PHP Code Beautifier and Fixer ... ${msg_color_none} \n" echo "$command_result" exit 1 fi diff --git a/pre_commit_hooks/php-cs-fixer.sh b/pre_commit_hooks/php-cs-fixer.sh index d95bf3c..09c2965 100755 --- a/pre_commit_hooks/php-cs-fixer.sh +++ b/pre_commit_hooks/php-cs-fixer.sh @@ -1,4 +1,5 @@ -#!/bin/bash +#!/usr/bin/env sh + ################################################################################ # # Bash PHP Coding Standards Fixer @@ -62,9 +63,9 @@ done; # There is currently debate about exit codes in php-cs-fixer # https://github.com/FriendsOfPHP/PHP-CS-Fixer/issues/1211 if [ "$php_errors_found" = true ]; then - echo -en "\n${txtylw}${title} updated the following files:${txtrst}\n" - echo -en "${error_message}" - echo -en "\n${bldred}Please review and commit.${txtrst}\n" + echo -e "\n${txtylw}${title} updated the following files:${txtrst}\n" + echo -e "${error_message}" + echo -e "\n${bldred}Please review and commit.${txtrst}\n" exit 1 fi diff --git a/pre_commit_hooks/php-cs.sh b/pre_commit_hooks/php-cs.sh index 47060c5..fd753a1 100755 --- a/pre_commit_hooks/php-cs.sh +++ b/pre_commit_hooks/php-cs.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh # Bash PHP Codesniffer Hook # This script fails if the PHP Codesniffer output has the word "ERROR" in it. @@ -19,7 +19,7 @@ msg_color_yellow='\033[0;33m' msg_color_none='\033[0m' # No Color # Loop through the list of paths to run php codesniffer against -echo -en "${msg_color_yellow}Begin PHP Codesniffer ...${msg_color_none} \n" +echo -e "${msg_color_yellow}Begin PHP Codesniffer ...${msg_color_none} \n" phpcs_local_exec="phpcs.phar" phpcs_command="php $phpcs_local_exec" @@ -49,9 +49,9 @@ echo "Running command $phpcs_command" command_result=`eval $phpcs_command` if [[ $command_result =~ ERROR ]] then - echo -en "${msg_color_magenta}Errors detected by PHP CodeSniffer ... ${msg_color_none} \n" + echo -e "${msg_color_magenta}Errors detected by PHP CodeSniffer ... ${msg_color_none} \n" echo "$command_result" exit 1 fi -exit 0 \ No newline at end of file +exit 0 diff --git a/pre_commit_hooks/php-lint.sh b/pre_commit_hooks/php-lint.sh index 40ecf5a..5187175 100755 --- a/pre_commit_hooks/php-lint.sh +++ b/pre_commit_hooks/php-lint.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh # Bash PHP Linter for Pre-commits # @@ -51,7 +51,7 @@ while getopts ":s:" optname done # Loop through the list of paths to run php lint against -echo -en "${msg_color_yellow}Begin PHP Linter ...${msg_color_none} \n" +echo -e "${msg_color_yellow}Begin PHP Linter ...${msg_color_none} \n" parse_error_count=0 for path in ${*:$arg_lookup_start} @@ -69,8 +69,8 @@ do done; if [ "$php_errors_found" = true ]; then - echo -en "${msg_color_magenta}$parse_error_count${msg_color_none} ${msg_color_yellow}PHP Parse error(s) were found!${msg_color_none} \n" + echo -e "${msg_color_magenta}$parse_error_count${msg_color_none} ${msg_color_yellow}PHP Parse error(s) were found!${msg_color_none} \n" exit 1 fi -exit 0 \ No newline at end of file +exit 0 diff --git a/pre_commit_hooks/php-unit.sh b/pre_commit_hooks/php-unit.sh index b028df0..31782f1 100755 --- a/pre_commit_hooks/php-unit.sh +++ b/pre_commit_hooks/php-unit.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env sh # Bash PHP Unit Task Runner # @@ -17,7 +17,7 @@ msg_color_yellow='\e[0;33m' msg_color_none='\e[0m' # No Color # Loop through the list of paths to run php lint against -echo -en "${msg_color_yellow}Begin PHP Unit Task Runner ...${msg_color_none} \n" +echo -e "${msg_color_yellow}Begin PHP Unit Task Runner ...${msg_color_none} \n" phpunit_local_exec="phpunit.phar" phpunit_command="php $phpunit_local_exec" @@ -48,4 +48,4 @@ then echo "$command_result" exit 1 fi -exit 0 \ No newline at end of file +exit 0