Skip to content

Instantly share code, notes, and snippets.

@win0err
Created September 21, 2017 23:30
Show Gist options
  • Save win0err/9de50685ecbdb8a7557f2c649083ffbc to your computer and use it in GitHub Desktop.
Save win0err/9de50685ecbdb8a7557f2c649083ffbc to your computer and use it in GitHub Desktop.
A git hook for php-cs-fixer
#!/usr/bin/env php
<?php
exec('git diff --cached --name-only', $stagedFiles);
$fixedFiles = [];
foreach ($stagedFiles as $fileName) {
if (preg_match('/\.php$/', $fileName) && is_file($fileName)) {
exec(sprintf('php-cs-fixer fix %s -q', $fileName), $output, $exitCode);
if ($exitCode === 1) {
exec('git add ' . $fileName);
$fixedFiles [] = $fileName;
}
}
}
if (count($fixedFiles)) {
echo sprintf("Code style fixes were applied to the following files:\n\n%s\n\nFiles were added to the commit after fixes.\n\n", implode("\n", $fixedFiles));
}
exit(0);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment