Skip to content

Instantly share code, notes, and snippets.

@chalasr
Created January 12, 2016 09:31
Show Gist options
  • Select an option

  • Save chalasr/56d52b3e92ffa6bb5ce3 to your computer and use it in GitHub Desktop.

Select an option

Save chalasr/56d52b3e92ffa6bb5ce3 to your computer and use it in GitHub Desktop.
Pre-commit hook that performs php-cs-fixer on each changed files.
#!/usr/bin/php
<?php
exec('git diff --cached --name-status --diff-filter=ACM', $output);
foreach ($output as $file) {
$fileName = trim(substr($file, 1) );
if (pathinfo($fileName,PATHINFO_EXTENSION) == "php") {
$lint_output = array();
exec("php -l " . escapeshellarg($fileName), $lint_output, $return);
if ($return == 0) {
exec("php php-cs-fixer.phar fix {$fileName} --config-file=.php_cs; git add {$fileName}");
echo $file.PHP_EOL;
} else {
echo implode("\n", $lint_output), "\n";
exit(1);
}
}
}
echo 'All source-code is clean'.PHP_EOL;
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment