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-md commit hook #9

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
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: [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.
7 changes: 7 additions & 0 deletions hooks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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$
76 changes: 76 additions & 0 deletions pre_commit_hooks/php-md.sh
Original file line number Diff line number Diff line change
@@ -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