Last active
October 11, 2015 09:38
-
-
Save Mezzle/3839688 to your computer and use it in GitHub Desktop.
Modified Version of ZF2 pre-commit hook
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
#!/bin/bash | |
for f in `git diff --cached --name-status --diff-filter=ACM | grep -Ev "^D" | awk '{print $2}' | grep -E "ph(p|tml)" | grep -v "misc/migrations"`; | |
do | |
php-cs-fixer --fixers=-psr0,-phpdoc_params fix $f | |
done |
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 | |
/** | |
* .git/hooks/pre-commit | |
* | |
* This pre-commit hooks will check for PHP errors (lint), and make sure the | |
* code is PSR-2 compliant. | |
* | |
* Dependecy: PHP-CS-Fixer (https://github.com/fabpot/PHP-CS-Fixer) | |
* | |
* @author Mardix http://github.com/mardix | |
* @author Matthew Weier O'Phinney http://mwop.net/ | |
* @since 4 Sept 2012 | |
*/ | |
$exit = 0; | |
/* | |
* collect all files which have been added, copied or | |
* modified and store them in an array called output | |
*/ | |
$output = array(); | |
exec('git diff --cached --name-status --diff-filter=ACM', $output); | |
foreach ($output as $file) { | |
if ('D' === substr($file, 0, 1)) { | |
// deleted file; do nothing | |
continue; | |
} | |
$fileName = trim(substr($file, 1)); | |
/* | |
* Only PHP files | |
*/ | |
$extension = pathinfo($fileName, PATHINFO_EXTENSION); | |
if (!preg_match('/^ph(p|tml)$/', $extension)) { | |
continue; | |
} | |
/* | |
* Check for parse errors | |
*/ | |
$output = array(); | |
$return = 0; | |
exec("php -l " . escapeshellarg($fileName) . ' 2>&1', $output, $return); | |
if ($return != 0) { | |
echo "PHP file fails to parse: " . $fileName . ":" . PHP_EOL; | |
echo implode(PHP_EOL, $output) . PHP_EOL; | |
$exit = 1; | |
continue; | |
} | |
/* | |
* PHP-CS-Fixer | |
*/ | |
$output = array(); | |
$return = null; | |
if (!preg_match('#misc/migrations#', $fileName)) { | |
exec("php-cs-fixer --dry-run --fixers=-psr0,-phpdoc_params fix " . escapeshellarg($fileName), $output, $return); | |
if ($return != 0 || !empty($output)) { | |
echo "PHP file fails contains CS issues: " . $fileName . ":" . PHP_EOL; | |
echo implode(PHP_EOL, $output) . PHP_EOL; | |
$exit = 1; | |
continue; | |
} | |
} | |
} | |
exit($exit); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment