Created
September 4, 2012 17:06
-
-
Save mardix/3623562 to your computer and use it in GitHub Desktop.
A pre-commit hook to make PHP code PSR-2 compliant, check for syntax error
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/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); |
Here is a shell script too.
#!/bin/bash
while read -r file;
do
file=${file:1}
if [[ $file = *.php ]];
then
php-cs-fixer fix "$file" --level=psr2
git add "$file"
fi
done < <(git diff --cached --name-status --diff-filter=ACM)
Here is an implementation using PHP Code Sniffer:
/**
* Collect all files which have been added, copied or
* modified and store them in an array - output
*/
exec('git diff --cached --name-only --diff-filter=ACM', $output);
$isViolated = 0;
$violatedFiles = array();
// $php_cs_path = "/usr/local/bin/php-cs-fixer";
$php_cs_path = "~/.composer/vendor/bin/phpcs";
foreach ($output as $fileName) {
// Consider only PHP file for processing
if (pathinfo($fileName, PATHINFO_EXTENSION) == "php") {
$psr_output = array();
// Put the changes to be made in $psr_output, if not as per PSR2 standard
// php-cs-fixer
// exec("{$php_cs_path} fix {$fileName} --rules=@PSR2 --dry-run --diff", $psr_output, $return);
// php-code-sniffer
exec("{$php_cs_path} --standard=PSR2 --colors -n {$fileName}", $psr_output, $return);
if ($return != 0) {
$isViolated = 1;
$violatedFiles[] = $fileName;
echo implode("\n", $psr_output), "\n";
}
}
}
if ($isViolated == 1) {
echo "\n---------------------------- IMPORTANT --------------------------------\n";
echo "\nPlease use the suggestions above to fix the code in the following file: \n";
echo " => " . implode("\n => ", $violatedFiles);
echo "\n-----------------------------------------------------------------------\n\n\n";
exit(1);
} else {
echo "\n => Committed Successfully :-)\n\n";
exit(0);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You may use
--name-only
parameter ofgit diff
in order to get only name of ACM files