Created
December 11, 2013 11:10
-
-
Save localheinz/7908614 to your computer and use it in GitHub Desktop.
Git pre-commit hook for use with https://github.com/localheinz/PHP-CS-Fixer/tree/feature/251.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
$changes = []; | |
exec( | |
'git diff --cached --name-status --diff-filter=ACM', | |
$changes | |
); | |
$exit = 0; | |
array_walk($changes, function ($change) use (&$exit) { | |
if ('D' === substr($change, 0, 1)) { | |
return; | |
} | |
$name = trim(substr( | |
$change, | |
1 | |
)); | |
$extension = pathinfo( | |
$name, | |
PATHINFO_EXTENSION | |
); | |
if (!preg_match('/^ph(p|tml)$/', $extension)) { | |
return; | |
} | |
$output = []; | |
$return = 0; | |
exec( | |
sprintf( | |
'php -l %s', | |
escapeshellarg($name) | |
), | |
$output, | |
$return | |
); | |
if (0 != $return) { | |
echo sprintf( | |
'Failed parsing: %s: ' . PHP_EOL, | |
$name | |
); | |
echo implode(PHP_EOL, $output) . PHP_EOL; | |
$exit = 1; | |
return; | |
} | |
$output = []; | |
$return = 0; | |
exec( | |
sprintf( | |
'vendor/bin/php-cs-fixer fix --verbose --config-file=.php_cs %s', | |
escapeshellarg($name) | |
), | |
$output, | |
$return | |
); | |
if (0 != $return) { | |
echo sprintf( | |
'Fixed coding style issues in : %s' . PHP_EOL, | |
$name | |
); | |
echo implode(PHP_EOL, $output) . PHP_EOL; | |
$exit = 1; | |
return; | |
} | |
}); | |
exit($exit); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment