Skip to content

Instantly share code, notes, and snippets.

@leocaseiro
Created February 27, 2015 01:16
Show Gist options
  • Save leocaseiro/3a899db45d8ed6ba15fe to your computer and use it in GitHub Desktop.
Save leocaseiro/3a899db45d8ed6ba15fe to your computer and use it in GitHub Desktop.
Syntax check your PHP before GIT Commit
#.git/hooks/pre-commit
#chmod +x pre-commit
#http://www.phil-barker.com/2013/07/syntax-check-your-php-before-git-commit/
#!/usr/bin/php
<?php
// Grab all added, copied or modified files into $output array
exec('git diff --cached --name-status --diff-filter=ACM', $output);
foreach ($output as $file) {
$fileName = trim(substr($file, 1));
// We only want to check PHP files
if (pathinfo($fileName,PATHINFO_EXTENSION) == "php") {
$lint_output = array();
// Check syntax with PHP lint
exec("php -l " . escapeshellarg($fileName), $lint_output, $return);
if ($return == 0) {
continue;
} 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