Skip to content

Instantly share code, notes, and snippets.

@Mezzle
Last active October 11, 2015 09:38
Show Gist options
  • Save Mezzle/3839688 to your computer and use it in GitHub Desktop.
Save Mezzle/3839688 to your computer and use it in GitHub Desktop.
Modified Version of ZF2 pre-commit hook
#!/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
#!/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