Skip to content

Instantly share code, notes, and snippets.

@localheinz
Created December 11, 2013 11:10
Show Gist options
  • Save localheinz/7908614 to your computer and use it in GitHub Desktop.
Save localheinz/7908614 to your computer and use it in GitHub Desktop.
#!/usr/bin/env php
<?php
$changes = [];
exec(
'git diff --cached --name-status --diff-filter=ACM',
$changes
);
$exit = 0;
array_walk($changes, function ($change) use (&$exit) {
if ('D' === substr($change, 0, 1)) {
return;
}
$name = trim(substr(
$change,
1
));
$extension = pathinfo(
$name,
PATHINFO_EXTENSION
);
if (!preg_match('/^ph(p|tml)$/', $extension)) {
return;
}
$output = [];
$return = 0;
exec(
sprintf(
'php -l %s',
escapeshellarg($name)
),
$output,
$return
);
if (0 != $return) {
echo sprintf(
'Failed parsing: %s: ' . PHP_EOL,
$name
);
echo implode(PHP_EOL, $output) . PHP_EOL;
$exit = 1;
return;
}
$output = [];
$return = 0;
exec(
sprintf(
'vendor/bin/php-cs-fixer fix --verbose --config-file=.php_cs %s',
escapeshellarg($name)
),
$output,
$return
);
if (0 != $return) {
echo sprintf(
'Fixed coding style issues in : %s' . PHP_EOL,
$name
);
echo implode(PHP_EOL, $output) . PHP_EOL;
$exit = 1;
return;
}
});
exit($exit);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment