|
#!/usr/bin/env bash |
|
# -------------------------------------------------------------# |
|
# Quality assurance checks # |
|
# # |
|
# for a composer based PHP project # |
|
# -------------------------------------------------------------# |
|
# Sergio Vaccaro <sergio.vaccaro@istat.it> |
|
|
|
# Note: add the following to your project |
|
# composer require --dev "phpmd/phpmd": "2.6.0" |
|
# composer require --dev "squizlabs/php_codesniffer": "2.9.1" |
|
# Create your own phpmd ruleset: https://phpmd.org/documentation/creating-a-ruleset.html |
|
|
|
# PHPMD rules file |
|
PHPMDRULES="phpmd.xml" |
|
|
|
function qa { |
|
# Set the exit status |
|
local EXIT=0 |
|
|
|
# PHPMD rules file |
|
PHPMDRULES="phpmd.xml" |
|
|
|
# Failures |
|
local NOPHPMD="" |
|
local NOPHPCS="" |
|
|
|
for i in $@; do |
|
echo "File ${i}" |
|
|
|
# phpmd, https://phpmd.org/ |
|
# ------------------------- |
|
# To run phpmd manually, type: |
|
# vendor/bin/phpmd . text phpmd.xml --suffixes php --exclude vendor,local |
|
echo -n 'PHP Mess Detector... ' |
|
if vendor/bin/phpmd $i text "$PHPMDRULES"; then |
|
echo 'ok' |
|
else |
|
echo 'failed' |
|
if [ -z "$NOPHPMD" ]; then |
|
NOPHPMD="$i" |
|
else |
|
NOPHPMD="${NOPHPMD},${i}" |
|
fi |
|
fi |
|
|
|
# phpcs https://github.com/squizlabs/PHP_CodeSniffer/wiki |
|
# ------------------------------------------------------- |
|
# To run phpcs manually, type: |
|
# vendor/bin/phpcs -s --ignore=vendor --ignore=web --ignore=local --report=summary . |
|
echo -n 'PHP CodeSniffer... ' |
|
if vendor/bin/phpcs -s "${i}"; then |
|
echo 'ok' |
|
else |
|
echo 'failed' |
|
if [ -z "$NOPHPCS" ]; then |
|
NOPHPCS="$i" |
|
else |
|
NOPHPCS="${NOPHPCS},${i}" |
|
fi |
|
fi |
|
done |
|
|
|
if [ -n "$NOPHPMD" ]; then |
|
echo |
|
echo '[QA] Error(s) found in code quality' |
|
echo ' Check the quality rules at https://phpmd.org/rules/index.html' |
|
echo " The following file(s) have error(s): ${NOPHPMD}" |
|
echo " Please run 'vendor/bin/phpmd _filename_ text ${PHPMDRULES}' for each file" |
|
echo |
|
EXIT=1 |
|
fi |
|
|
|
if [ -n "$NOPHPCS" ]; then |
|
echo |
|
echo '[QA] Error(s) found in conding style' |
|
echo " The following file(s) have error(s): ${NOPHPCS}" |
|
echo " Please run 'vendor/bin/phpcs -s --report=diff _filename_' for each file" |
|
echo |
|
if [ $EXIT -eq 0 ]; then |
|
EXIT=2 |
|
fi |
|
fi |
|
|
|
if [ "$EXIT" -ne 0 ]; then |
|
return "$EXIT" |
|
fi |
|
|
|
return 0 |
|
} |