From e650d7b1ef767a08c12a3220aad10ebef57ebd2b Mon Sep 17 00:00:00 2001 From: Craig Davis Date: Thu, 3 Dec 2015 18:32:48 -0700 Subject: [PATCH] Add new php-md commit hook --- README.md | 12 ++++++ hooks.yaml | 7 ++++ pre_commit_hooks/php-md.sh | 76 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100755 pre_commit_hooks/php-md.sh diff --git a/README.md b/README.md index 6dc9c96..4c6955c 100644 --- a/README.md +++ b/README.md @@ -122,3 +122,15 @@ Similar pattern as the php-cs hook. A bash script that will run the appropriate The tool will fail a build when it has made changes to the staged files. This allows a developer to do a `git diff` and examine the changes that it has made. Remember that you may omit this if needed with a `SKIP=php-cs-fixer git commit`. +## php-md +```yaml +- repo: git@github.com:hootsuite/pre-commit-php.git + sha: 1.1.0 + hooks: + - id: php-md + files: \.(php)$ + args: ["codesize,controversial,design,naming,unusedcode"] +``` +A bash script that will run the appropriate [PHP Mess Detector](http://phpmd.org/) executable and report issues as configured. + +The tool will fail a build when it has found issues that violate the configured code rules. Please note that the code rule list must be the first argument in the `args` list. diff --git a/hooks.yaml b/hooks.yaml index 48ef745..e5c3448 100644 --- a/hooks.yaml +++ b/hooks.yaml @@ -40,3 +40,10 @@ entry: pre_commit_hooks/php-cs-fixer.sh language: script files: \.php$ + +- id: php-md + name: PHP Mess Detector + description: Examine code for potential problems. + entry: pre_commit_hooks/php-md.sh + language: script + files: \.php$ diff --git a/pre_commit_hooks/php-md.sh b/pre_commit_hooks/php-md.sh new file mode 100755 index 0000000..c73aec4 --- /dev/null +++ b/pre_commit_hooks/php-md.sh @@ -0,0 +1,76 @@ +#!/bin/bash +################################################################################ +# +# Bash PHP Mess Detector +# +# This will prevent a commit if the tool has detected violations of the +# rulesets specified +# +# Exit 0 if no errors found +# Exit 1 if errors were found +# +# Requires +# - php +# +################################################################################ + +# Plugin title +title="PHP Mess Detector" + +# Possible command names of this tool +local_command="phpmd.phar" +vendor_command="vendor/bin/phpmd" +global_command="phpmd" + +# Print a welcome and locate the exec for this tool +DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +source $DIR/helpers/colors.sh +source $DIR/helpers/formatters.sh +source $DIR/helpers/welcome.sh +source $DIR/helpers/locate.sh + +# Build our list of files, and our list of args by testing if the argument is +# a valid path +args="" +files=() +for arg in ${*} +do + if [ -e $arg ]; then + files+=("$arg") + else + args+=" $arg" + fi +done; + +# Run the command on each file +echo -e "${txtgrn} $exec_command${args}${txtrst}" +php_errors_found=false +error_message="" +for path in "${files[@]}" +do + OUTPUT="$(${exec_command} ${path} text ${args})" + RETURN=$? + if [ $RETURN -eq 1 ]; then + # Return 1 means that PHPMD crashed + error_message+=" - ${bldred}PHPMD failed to evaluate ${path}${txtrst}" + error_message+="${OUTPUT}\n\n" + php_errors_found=true + elif [ $RETURN -eq 2 ]; then + # Return 2 means it ran successfully, but found issues. + # Using perl regex to clean up PHPMD output, trimming out full file + # paths that are included in each line + error_message+=" - ${txtylw}${path}${txtrst}" + error_message+="$(echo $OUTPUT | perl -pe "s/(\/.*?${path}:)/\n line /gm")" + error_message+="\n\n" + php_errors_found=true + fi +done; + +if [ "$php_errors_found" = true ]; then + echo -en "\n${txtylw}${title} found issues in the following files:${txtrst}\n\n" + echo -en "${error_message}" + echo -en "${bldred}Please review and commit.${txtrst}\n" + exit 1 +fi + +exit 0