From 3ede06c36fb31159ea6012c8b7fb069fb5823ded Mon Sep 17 00:00:00 2001 From: Jason Oettinger Date: Sun, 16 Jul 2017 17:58:11 -0400 Subject: [PATCH] Improved Code Sniffing (#928) * Adding strict PSR2 enforcement for new code ci now brings in phpcs globally so it can be used by devs as well without polluting their openemr packages * Using `composer global exec` to ensure accurate COMPOSER_HOME * Adding lint_style_staged as an option, allowing pre-commit checks for PSR generalized sniffing to function in ci.sh * Fixing environment variable in .travis.yml * Fixing multiline issue * Allowing detection of `COMPOSER_HOME` without `composer exec` * Actually installing phpcs on * Removing Travis' strict interpretation of new commits --- .travis.yml | 1 - ci/ci.sh | 81 +++++++++++++++++++++++++++++++++++------------------ ci/phpcs_strict.xml | 22 +++++++++++++++ 3 files changed, 75 insertions(+), 29 deletions(-) create mode 100644 ci/phpcs_strict.xml diff --git a/.travis.yml b/.travis.yml index d5cbdb4df..63bf0a0a6 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,5 @@ php: env: - CI_JOB=lint_syntax - CI_JOB=lint_style - script: - bash ci/ci.sh --dir ./ diff --git a/ci/ci.sh b/ci/ci.sh index 81fadd824..f59ecde20 100755 --- a/ci/ci.sh +++ b/ci/ci.sh @@ -15,32 +15,57 @@ if [ -z "$1" ] || [ "$1" == "-h" ] || [ "$1" == "--help" ] ; then exit 0 fi -case "$CI_JOB" in +#takes list of files/folders to sniff as its only argument(s) +function sniff { + INI=$HOME/.phpenv/versions/$(phpenv version-name 2> /dev/null)/etc/php.ini + if [ -f $INI ]; then + grep -q "extension = ldap.so" $INI || echo "extension = ldap.so" >> $INI + fi + BIN_DIR=$HOME/.composer/vendor/bin + if [ -d $HOME/$XDG_CONFIG_HOME/composer ]; then + BIN_DIR="$HOME/$XDG_CONFIG_HOME/composer/vendor/bin" + fi + if [ -d $HOME/.config/composer ]; then + BIN_DIR="$HOME/.config/composer/vendor/bin" + fi + composer global require "squizlabs/php_codesniffer=3.0.*" + cd $DIR + $BIN_DIR/phpcs -p -n --extensions=php,inc --report-width=120 $@ +} -"lint_syntax") - if [ "$1" == "-d" ] || [ "$1" == "--dir" ] ; then - cd $2 - find . -type d \( -path ./vendor \ - -o -path ./interface/main/calendar/modules \ - -o -path ./interface/reports \ - -o -path ./contrib/util \ - -o -path ./library/html2pdf/vendor/tecnickcom \ - -o -path ./library/classes/fpdf \ - -o -path ./library/html2pdf \ - -o -path ./gacl \ - -o -path ./library/edihistory \) -prune -o \ - -name "*.php" -print0 | xargs -0 -n1 -P8 php -l - fi - ;; -"lint_style") - if [ "$1" == "-d" ] || [ "$1" == "--dir" ] ; then - cd $2 - echo "extension = ldap.so" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini - composer require "squizlabs/php_codesniffer=3.0.*" - ./vendor/bin/phpcs -p -n --extensions=php,inc --standard=ci/phpcs.xml --report-width=120 --report=summary --report=source --report=info . - fi - ;; -*) - echo "Error: not a valid CI_JOB" - ;; -esac +if [ "$1" == "-d" ] || [ "$1" == "--dir" ] ; then + + DIR=$2 + + case "$CI_JOB" in + + "lint_syntax") + cd $2 + find . -type d \( -path ./vendor \ + -o -path ./interface/main/calendar/modules \ + -o -path ./interface/reports \ + -o -path ./contrib/util \ + -o -path ./library/html2pdf/vendor/tecnickcom \ + -o -path ./library/classes/fpdf \ + -o -path ./library/html2pdf \ + -o -path ./gacl \ + -o -path ./library/edihistory \) -prune -o \ + -name "*.php" -print0 | xargs -0 -n1 -P8 php -l + ;; + "lint_style") + sniff . --standard=ci/phpcs.xml --report=summary --report=source --report=info + ;; + "lint_style_new_commit") + MODIFIED_FILES=$(git diff-tree --no-commit-id --name-only -r HEAD | tr "\n" " ") + sniff "$MODIFIED_FILES" --standard=ci/phpcs_strict.xml --report=summary + ;; + "lint_style_staged") + MODIFIED_FILES=$(git diff --cached --name-only | tr "\n" " ") + sniff "$MODIFIED_FILES" --standard=ci/phpcs_strict.xml --report=summary + ;; + *) + echo "Error: not a valid CI_JOB" + + ;; + esac +fi \ No newline at end of file diff --git a/ci/phpcs_strict.xml b/ci/phpcs_strict.xml new file mode 100644 index 000000000..fc73ddbcb --- /dev/null +++ b/ci/phpcs_strict.xml @@ -0,0 +1,22 @@ + + + PSR2 + + + + + */vendor/* + */gacl/* + */bower_components/* + */node_modules/* + */public/assets/* + */Documentation/* + */tests/* + */images/* + */library/html2pdf/* + */library/classes/fpdf/* + */library/classes/smtp/* + */library/classes/PDF_Label.php + */library/custom_template/* + */library/fonts/* + -- 2.11.4.GIT