Created
June 29, 2017 01:26
-
-
Save imvaskii/bbe77497ae633346d693af3942e95582 to your computer and use it in GitHub Desktop.
Runs php code sniffer via bash script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
set -u | |
set -o pipefail | |
# installs composer if required | |
function maybe_install_composer() { | |
# If composer exists then return | |
if type composer > /dev/null; then | |
return | |
else | |
# install composer | |
curl -sS https://getcomposer.org/installer | sudo php -- --install-dir=/usr/local/bin --filename=composer | |
fi | |
return | |
} | |
# installs PHPCS and WPCS via composer | |
function maybe_install_dependencies { | |
# Checking available coding standards. | |
if type phpcs > /dev/null; then | |
# standards=$(phpcs -i) | |
if [[ $(phpcs -i) == *"WordPress"* ]]; then | |
return | |
fi | |
fi | |
# install phpcs: | |
composer global require "squizlabs/php_codesniffer=${PHPCS_VER}" | |
# install wordpress coding standard rulesets: | |
composer global require "wp-coding-standards/wpcs=${WPCS_VER}" | |
# set phpcs installed path to wp-coding-standards/wpcs. | |
phpcs --config-set installed_paths $HOME/.composer/vendor/wp-coding-standards/wpcs | |
return | |
} | |
function php_code_sniff() { | |
if [ -z ${PHP_STANDARD+x} ]; then | |
PHP_STANDARD='WordPress' | |
fi | |
#Enable php-codesniffing only if there are php modified files. | |
phpfiles=$(git diff --name-only master | grep .php); | |
if [ ! -z "$phpfiles" ]; then | |
maybe_install_composer | |
maybe_install_dependencies | |
# do sniffing | |
phpcs --standard=${PHP_STANDARD} --extensions=php -p -n -s --colors $phpfiles | |
else | |
exit 0 | |
fi | |
} | |
php_code_sniff | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment