Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MadmanMonty/496870cfa67c262b9dfe to your computer and use it in GitHub Desktop.
Save MadmanMonty/496870cfa67c262b9dfe to your computer and use it in GitHub Desktop.
php-cs-fixer dry run on git commit
#!/usr/bin/env php
<?php
// If you want to auto correct files, or just inform and fail. (values: disabled, manual, partial, auto. default: partial)
//$autoCorrect = 'partial';
$autoCorrect = 'auto';
$verboseOutput = true;
$binPath = '..\\..\\..\\bin\\';
/**
* .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
* @author MadmanMonty https://github.com/MadmanMonty
* @since Sept 4 2012
*
*/
/**
* collect all files which have been added, copied or
* modified and store them in an array called output
*/
vLog ("Starting checks. Correction Mode: {$autoCorrect}\n");
exec('git diff --cached --name-status --diff-filter=ACM', $output);
$errorCount = 0;
foreach ($output as $file) {
$fileName = trim(substr($file, 1) );
/**
* Only PHP file
*/
if (pathinfo($fileName,PATHINFO_EXTENSION) == "php") {
vLog ("$fileName\n");
/**
* Check for error
*/
$lint_output = array();
exec("php -l " . escapeshellarg($fileName), $lint_output, $return);
if ($return == 0) {
if(!codingStandards($fileName, $autoCorrect)) {
exit(1);
}
} else {
echo implode("\n", $lint_output), "\n";
exit(1);
}
}
}
if($errorCount && $autoCorrect == 'partial') {
echo "Coding standards are not correct, corrections have been made,.\n";
echo "Please review and add to this commit if ok:\n";
echo "\n";
exit(1);
} else {
exit(0);
}
function codingStandards($fileName, $autoCorrect)
{
global $errorCount;
global $binPath;
vLog("Checking: {$fileName}\n");
switch($autoCorrect) {
case 'manual':
// manual fix
if (!isManualCheckOk($fileName)) {
echo "Coding standards are not correct, cancelling your commit.\n";
echo "Try running to correct:\n";
echo "\n";
echo " {$binPath}php-cs-fixer.bat fix {$fileName}\n";
return false;
}
break;
case 'partial':
if (!isManualCheckOk($fileName)) {
$errorCount++;
vLog(" file being corrected.");
$tstr = "{$binPath}php-cs-fixer.bat fix {$fileName}";
exec($tstr, $cs_output);
vLog($cs_output);
}
break;
case 'auto':
// auto fix
$tstr = "{$binPath}php-cs-fixer.bat fix {$fileName} || git add {$fileName}";
exec($tstr, $cs_output);
vLog($cs_output);
break;
case 'disabled':
break;
}
return true;
}
function isManualCheckOk($fileName)
{
global $binPath;
$tstr = "{$binPath}php-cs-fixer.bat fix {$fileName} --verbose --dry-run";
exec($tstr, $cs_output);
if($cs_output[0] != '.') {
$errorCount++;
return false;
} else {
vLog ("{$tstr} OK\n");
}
return true;
}
function vLog($string)
{
global $verboseOutput;
if($verboseOutput) {
if (is_array($string)) {
print_r($string);
} else {
echo "{$string}";
}
}
}
#!/usr/bin/env php
<?php
// If you want to auto correct files, or just inform and fail. (values: disabled, manual, partial, auto. default: partial)
//$autoCorrect = 'partial';
$autoCorrect = 'auto';
$verboseOutput = true;
$binPath = 'bin\\';
/**
* .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
* @author MadmanMonty https://github.com/MadmanMonty
* @since Sept 4 2012
*
*/
/**
* collect all files which have been added, copied or
* modified and store them in an array called output
*/
vLog ("Starting checks. Correction Mode: {$autoCorrect}\n");
exec('git diff --cached --name-status --diff-filter=ACM', $output);
$errorCount = 0;
foreach ($output as $file) {
$fileName = trim(substr($file, 1) );
/**
* Only PHP file
*/
if (pathinfo($fileName,PATHINFO_EXTENSION) == "php") {
vLog ("$fileName\n");
/**
* Check for error
*/
$lint_output = array();
exec("php -l " . escapeshellarg($fileName), $lint_output, $return);
if ($return == 0) {
if(!codingStandards($fileName, $autoCorrect)) {
exit(1);
}
} else {
echo implode("\n", $lint_output), "\n";
exit(1);
}
}
}
if($errorCount && $autoCorrect == 'partial') {
echo "Coding standards are not correct, corrections have been made,.\n";
echo "Please review and add to this commit if ok:\n";
echo "\n";
exit(1);
} else {
exit(0);
}
function codingStandards($fileName, $autoCorrect)
{
global $errorCount;
global $binPath;
vLog("Checking: {$fileName}\n");
switch($autoCorrect) {
case 'manual':
// manual fix
if (!isManualCheckOk($fileName)) {
echo "Coding standards are not correct, cancelling your commit.\n";
echo "Try running to correct:\n";
echo "\n";
echo " {$binPath}php-cs-fixer.bat fix {$fileName}\n";
return false;
}
break;
case 'partial':
if (!isManualCheckOk($fileName)) {
$errorCount++;
vLog(" file being corrected.");
$tstr = "{$binPath}php-cs-fixer.bat fix {$fileName}";
exec($tstr, $cs_output);
vLog($cs_output);
}
break;
case 'auto':
// auto fix
$tstr = "{$binPath}php-cs-fixer.bat fix {$fileName} || git add {$fileName}";
exec($tstr, $cs_output);
vLog($cs_output);
break;
case 'disabled':
break;
}
return true;
}
function isManualCheckOk($fileName)
{
global $binPath;
$tstr = "{$binPath}php-cs-fixer.bat fix {$fileName} --verbose --dry-run";
exec($tstr, $cs_output);
if($cs_output[0] != '.') {
$errorCount++;
return false;
} else {
vLog ("{$tstr} OK\n");
}
return true;
}
function vLog($string)
{
global $verboseOutput;
if($verboseOutput) {
if (is_array($string)) {
print_r($string);
} else {
echo "{$string}";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment