Skip to content

Instantly share code, notes, and snippets.

@iolloyd
Last active August 29, 2015 14:16
Show Gist options
  • Save iolloyd/235e5876cbfca2e15072 to your computer and use it in GitHub Desktop.
Save iolloyd/235e5876cbfca2e15072 to your computer and use it in GitHub Desktop.
php syntax checker before commiting to git
#!/usr/bin/env php
<?php
exec ('git diff --cached --name-status --diff-filter=ACM', $files);
// remove the character before the fileName
$files = array_map(function($x) {return trim(substr($x, 1)); }, $files);
// we only want the php files
$phps = array_filter($files, function($x) {return "php" == pathinfo($x, PATHINFO_EXTENSION);});
foreach ($phps as $file) {
$lint = [];
exec("php -l " . escapeshellarg($file), $lint, $return);
if (!$return) { continue; }
echo implode("\n", $lint), "\n";
exit(1);
}
exit(0);
@iolloyd
Copy link
Author

iolloyd commented Apr 15, 2015

  • Save this in your local repository as .git/hooks/pre-commit
  • run chmod +x .git/hooks/pre-commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment