Created
September 21, 2017 23:30
-
-
Save win0err/9de50685ecbdb8a7557f2c649083ffbc to your computer and use it in GitHub Desktop.
A git hook for php-cs-fixer
This file contains 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 | |
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