Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add new php-cpd commit hook #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: [email protected]: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: [email protected]: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`.
14 changes: 14 additions & 0 deletions hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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$
54 changes: 54 additions & 0 deletions pre_commit_hooks/php-cpd.sh
Original file line number Diff line number Diff line change
@@ -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