-
-
Save Cayan/e4377be42f2cbc563eaa to your computer and use it in GitHub Desktop.
pre-commit hook for git, running php lint, phpcs and phpmd
This file contains 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
#!/usr/bin/php | |
<?php | |
// copied from https://gist.github.com/chelmertz/816166 | |
// authored by Travis Swicegood | |
// modified by Daniel Pinto and Israel Trindade | |
class Enable | |
{ | |
const PHP_LINT = true; | |
const PHP_CS = true; | |
const PHP_MD = true; | |
// Quando true, vai impedir os commits. | |
const DEBUG_MODE = true; | |
} | |
$output = []; | |
$return = 0; | |
exec('git rev-parse --verify HEAD 2> /dev/null', $output, $return); | |
$against = $return == 0 ? 'HEAD' : '4b825dc642cb6eb9a060e54bf8d69288fbee4904'; | |
exec("git diff-index --cached --name-only {$against}", $output); | |
// don't check files that aren't PHP | |
$filename_pattern = '/\.ph(tml|p)$/'; | |
$exclude_patterns = [ | |
"/\/migrations\//", | |
"/\/views\//" | |
]; | |
$exit_status = 0; | |
// First we remove those files that does not suit our needs | |
foreach ($output as $key => $file) { | |
foreach ($exclude_patterns as $exclude_pattern) { | |
if (preg_match($exclude_pattern, $file)) { | |
unset($output[$key]); | |
continue 2; | |
} | |
} | |
if (!preg_match($filename_pattern, $file)) { | |
unset($output[$key]); | |
continue; | |
} | |
if (!file_exists($file)) { | |
// if the file has been moved or deleted, | |
// the old filename should be skipped | |
unset($output[$key]); | |
continue; | |
} | |
} | |
if (Enable::PHP_LINT) { | |
foreach ($output as $file) { | |
$lint_output = []; | |
exec("git show :" . escapeshellarg($file) . " | php -l ", $lint_output, $return); | |
if ($return == 0) { | |
continue; | |
} | |
echo implode("\n", $lint_output), " {$file}\n"; | |
$exit_status = 1; | |
} | |
} | |
if ($exit_status != 0) { | |
exit($exit_status); | |
} | |
if (Enable::PHP_MD) { | |
foreach ($output as $file) { | |
$md_output = []; | |
// Parece que o PHPMD não suporta ler apenas os dados staged. | |
//exec("phpmd $(git show :" . escapeshellarg($file) . ") text build/phpmd.xml", $md_output, $return); | |
exec("phpmd " . escapeshellarg($file) . " text build/phpmd.xml", $md_output, $return); | |
if ($return == 0) { | |
continue; | |
} | |
echo str_replace('STDIN', escapeshellarg($file), implode("\n", $md_output)), "\n"; | |
$exit_status = 1; | |
} | |
} | |
// O PHPCS é executado por último, para que suas mensagens fiquem mais visíveis para o usuário. | |
if (Enable::PHP_CS) { | |
foreach ($output as $file) { | |
$cs_output = []; | |
exec("git show :" . escapeshellarg($file) . " | phpcs -n --standard=build/phpcs_ruleset.xml --encoding=utf-8 --tab-width=4", $cs_output, $return); | |
if ($return == 0) { | |
continue; | |
} | |
echo str_replace('STDIN', escapeshellarg($file), implode("\n", $cs_output)), "\n"; | |
$exit_status = 1; | |
} | |
} | |
if (Enable::DEBUG_MODE) { | |
exit(1); | |
} | |
exit($exit_status); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment