Skip to content

Instantly share code, notes, and snippets.

@pollin14
Forked from mardix/php-cs-fixer-pre-commit.php
Last active September 25, 2015 21:31
Show Gist options
  • Save pollin14/85aac1a449a81ef5ed88 to your computer and use it in GitHub Desktop.
Save pollin14/85aac1a449a81ef5ed88 to your computer and use it in GitHub Desktop.
A pre-commit hook to check syntax errors and PSR2
#!/usr/bin/php
<?php
/**
* .git/hooks/pre-commit
*
* This pre-commit hooks will check for PHP error (lint), and make sure the code
* is PSR compliant.
*
* Dependecy: PHP-CS-Fixer (https://github.com/fabpot/PHP-CS-Fixer)
*
* @author Mardix http://github.com/mardix
* @since Sept 4 2012
*
*/
/**
* collect all files which have been added, copied or
* modified and store them in an array called output
*/
exec('git diff --cached --name-only --diff-filter=ACM', $output);
foreach ($output as $fileName) {
/**
* Only PHP file
*/
if (pathinfo($fileName,PATHINFO_EXTENSION) == "php") {
$lintOutput = [];
exec("php -l " . escapeshellarg($fileName), $lintOutput, $lintExitCode);
if ($lintExitCode != 0) {
echo implode("\n", $lint_output), "\n";
exit(1);
}
$csOutput = [];
exec("phpcs --standard=PSR2 {$fileName};", $csOutput, $csExitCode);
if ($csExitCode != 0) {
echo implode("\n", $csOutput), "\n";
exit(1);
}
}
}
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment