From a8575a1ecdc4d94d22b59cb49c6bbeaf1dd5a16e Mon Sep 17 00:00:00 2001 From: Zach Borboa Date: Tue, 17 Feb 2015 00:31:43 +0700 Subject: [PATCH] Enforce indentation consistency in php files --- examples/put.php | 6 +++--- tests/script.sh | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 3 deletions(-) mode change 100644 => 100755 tests/script.sh diff --git a/examples/put.php b/examples/put.php index 21add57..e9440d5 100644 --- a/examples/put.php +++ b/examples/put.php @@ -6,9 +6,9 @@ use \Curl\Curl; // curl -X PUT -d "id=1&first_name=Zach&last_name=Borboa" "http://httpbin.org/put" $curl = new Curl(); $curl->put('http://httpbin.org/put', array( - 'id' => 1, - 'first_name' => 'Zach', - 'last_name' => 'Borboa', + 'id' => 1, + 'first_name' => 'Zach', + 'last_name' => 'Borboa', )); echo 'Data server received via PUT:' . "\n"; diff --git a/tests/script.sh b/tests/script.sh old mode 100644 new mode 100755 index 99a6faa..e56da5d --- a/tests/script.sh +++ b/tests/script.sh @@ -18,6 +18,38 @@ if [[ ! -z "${tab_char}" ]]; then exit 1 fi +# Enforce indentation consistency in php files. +find_invalid_indentation() { + filename="${1}" + script=$(cat <<'EOF' + $filename = $argv['1']; + $lines = explode("\n", file_get_contents($filename)); + $line_number = 0; + foreach ($lines as $line) { + $line_number += 1; + $leading_space_count = strspn($line, ' '); + $remainder = $leading_space_count % 4; + if (!($remainder === 0)) { + // Allow doc comments. + if (substr(ltrim($line), 0, 1) === '*') { + continue; + } + $add_count = 4 - $remainder; + $remove_count = $remainder; + echo 'Invalid indentation found in ' . $filename . ':' . $line_number . + ' (' . $leading_space_count . ':+' . $add_count . '/-' . $remove_count . ')' . "\n"; + } + } +EOF) + php --run "${script}" "${filename}" +} +export -f "find_invalid_indentation" +invalid_indentation=$(find . -type "f" -iname "*.php" -exec bash -c 'find_invalid_indentation "{}"' \;) +if [[ ! -z "${invalid_indentation}" ]]; then + echo "${invalid_indentation}" + exit 1 +fi + # Prohibit trailing whitespace in php files. trailing_whitespace=$(find . -type "f" -iname "*.php" -exec egrep --line-number -H " +$" {} \;) if [[ ! -z "${trailing_whitespace}" ]]; then