Created

Embed URL

HTTPS clone URL

SSH clone URL

You can clone with HTTPS or SSH.

Download Gist

A pre-commit hook to make PHP code PSR-2 compliant, check for syntax error

View php-cs-fixer-pre-commit.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
#!/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-status --diff-filter=ACM', $output);
foreach ($output as $file) {
$fileName = trim(substr($file, 1) );
/**
* Only PHP file
*/
if (pathinfo($fileName,PATHINFO_EXTENSION) == "php") {
/**
* Check for error
*/
$lint_output = array();
exec("php -l " . escapeshellarg($fileName), $lint_output, $return);
if ($return == 0) {
/**
* PHP-CS-Fixer && add it back
*/
exec("php-cs-fixer fix {$fileName} --level=all; git add {$fileName}");
} else {
echo implode("\n", $lint_output), "\n";
exit(1);
}
}
}
exit(0);

You may use --name-only parameter of git diff in order to get only name of ACM files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.