From 7ee9e17f17a0e7287f4d058cffdbc5b0990846a0 Mon Sep 17 00:00:00 2001 From: Craig Davis Date: Thu, 3 Dec 2015 19:32:35 -0700 Subject: [PATCH] Add new php-cpd commit hook --- README.md | 25 +++++++++++++++++ hooks.yaml | 14 ++++++++++ pre_commit_hooks/php-cpd.sh | 54 +++++++++++++++++++++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100755 pre_commit_hooks/php-cpd.sh diff --git a/README.md b/README.md index 6dc9c96..7adbbd3 100644 --- a/README.md +++ b/README.md @@ -122,3 +122,28 @@ 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. + +## php-cpd +```yaml +- repo: git@github.com:hootsuite/pre-commit-php.git + sha: 1.1.0 + hooks: + - id: php-cpd + files: \.(php)$ + args: ["--min-tokens=10"] +``` +A bash script that will run the appropriate [PHP Copy Paste Detector](https://github.com/sebastianbergmann/phpcpd) executable and report on duplicate code. + +The tool will fail a build when it has found issues that violate the configured code rules. This will accept all arguments, in particular you'll want to tune for `----min-lines=`, `--min-tokens=`, and `--fuzzy`. diff --git a/hooks.yaml b/hooks.yaml index 48ef745..74e2797 100644 --- a/hooks.yaml +++ b/hooks.yaml @@ -40,3 +40,17 @@ 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$ + +- id: php-cpd + name: PHP Copy Paste Detector + description: Examine code for duplicate code and reject if found + entry: pre_commit_hooks/php-cpd.sh + language: script + files: \.php$ diff --git a/pre_commit_hooks/php-cpd.sh b/pre_commit_hooks/php-cpd.sh new file mode 100755 index 0000000..29b8b96 --- /dev/null +++ b/pre_commit_hooks/php-cpd.sh @@ -0,0 +1,54 @@ +#!/bin/bash +################################################################################ +# +# Bash PHP Copy Paste Detector +# +# This will prevent a commit if the tool has detected duplicate code +# +# Exit 0 if no errors found +# Exit 1 if errors were found +# +# Requires +# - php +# +################################################################################ + +# Plugin title +title="PHP Copy Paste Detector" + +# Possible command names of this tool +local_command="phpcpd.phar" +vendor_command="vendor/bin/phpcpd" +global_command="phpcpd" + +# 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 with the full list of files +echo -e "${txtgrn} $exec_command --no-interaction --ansi${args}${files}${txtrst}" +OUTPUT="$($exec_command${args}${files})" +RETURN=$? +if [ $RETURN -ne 0 ]; then + echo -en "\n${txtylw}${title} found copied lines in the following files:${txtrst}\n " + echo -en "$OUTPUT" | awk -v m=3 -v n=2 'NR<=m{next};NR>n+m{print line[NR%n]};{line[NR%n]=$0}' + echo -en "\n${bldred}Please review and commit.${txtrst}\n" + exit 1 +fi +exit 0