Skip to content

Instantly share code, notes, and snippets.

@Firesphere
Created February 17, 2017 07:08
Show Gist options
  • Save Firesphere/d2df6d902870620209e493b5a9fbb28f to your computer and use it in GitHub Desktop.
Save Firesphere/d2df6d902870620209e493b5a9fbb28f to your computer and use it in GitHub Desktop.
#!/usr/bin/php
<?php
/**
* .git/hooks/pre-commit
*/
/**
* collect all files which have been added, copied or
* modified and store them in an array called output
*/
exec('git diff --cached --name-status --diff-filter=ACM', $output);
$topLevel = exec("git rev-parse --show-toplevel");
foreach ($output as $file) {
$fileName = trim(substr($file, 1));
/**
* Only PHP file
*/
if (pathinfo($fileName, PATHINFO_EXTENSION) == "php") {
/**
* Check for errors
*/
$lint_output = array();
exec("php -l " . escapeshellarg($fileName), $lint_output, $return);
if ($return == 0) {
/**
* PHP-CS-Fixer && add it back
*/
exec($topLevel . "/../vendor/bin/php-cs-fixer fix $topLevel".'/'."$fileName");
exec("git add $topLevel".'/'."$fileName");
} else {
echo implode("\n", $lint_output), "\n";
exit(1);
}
}
}
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment