Skip to content

Instantly share code, notes, and snippets.

@chalasr
Last active August 24, 2016 09:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chalasr/3af69909ad83df8b444f438818a70d7e to your computer and use it in GitHub Desktop.
Save chalasr/3af69909ad83df8b444f438818a70d7e to your computer and use it in GitHub Desktop.
git pre-commit hook that clean each modified file using php-cs-fixer
#!/usr/bin/php
<?php
exec('git diff --cached --name-status --diff-filter=ACM', $output);
foreach ($output as $file) {
$fileName = trim(substr($file, 1) );
if ("php" == pathinfo($fileName, PATHINFO_EXTENSION)) {
$lint_output = array();
exec("php -l " . escapeshellarg($fileName), $lint_output, $return);
if ($return == 0) {
exec("php-cs-fixer fix {$fileName} --config-file=.php_cs; git add {$fileName}");
echo $file.PHP_EOL;
} else {
echo implode("\n", $lint_output), "\n";
exit(1);
}
}
}
echo 'All source-code is clean'.PHP_EOL;
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment