Skip to content

Commit

Permalink
Temp script to apply fixes to code. Use in place of the phpcs script …
Browse files Browse the repository at this point in the history
…and pass it all the same args. It will ignore the ones that it doesn't need, generate a diff file, patch all your files, then exit. Errors are shown if there are any, and the diff file is left in the current dir so it can be used to undo the changes.
  • Loading branch information
gsherwood committed May 10, 2013
1 parent 7eeb17a commit 402f2c3
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions scripts/phpcbf
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/usr/bin/env php
<?php
/**
* PHP Code Beautifier and Fixer
*
* PHP version 5
*
* @category PHP
* @package PHP_CodeSniffer
* @author Greg Sherwood <[email protected]>
* @copyright 2006-2012 Squiz Pty Ltd (ABN 77 084 670 600)
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

error_reporting(E_ALL | E_STRICT);

// Optionally use PHP_Timer to print time/memory stats for the run.
@include_once 'PHP/Timer.php';
if (class_exists('PHP_Timer', false) === true) {
PHP_Timer::start();
}

if (is_file(dirname(__FILE__).'/../CodeSniffer/CLI.php') === true) {
include_once dirname(__FILE__).'/../CodeSniffer/CLI.php';
} else {
include_once 'PHP/CodeSniffer/CLI.php';
}

$phpcs = new PHP_CodeSniffer_CLI();
$phpcs->checkRequirements();
$cliValues = $phpcs->getCommandLineValues();


// Override some of the command line settings that might be used and stop us
// gettting a diff file.
$diffFile = getcwd().'/phpcbf-fixed.diff';

$cliValues['generator'] = '';
$cliValues['explain'] = false;
$cliValues['reports'] = array('diff' => $diffFile);

if (file_exists($diffFile) === true) {
unlink($diffFile);
}

$numErrors = $phpcs->process($cliValues);
if (file_exists($diffFile) === false) {
// Nothing to fix.
if ($numErrors === 0) {
// And no errors reported.
$exit = 0;
} else {
// Errors we can't fix.
$exit = 2;
}
} else {
$cmd = "patch -p0 -ui \"$diffFile\"";
$output = array();
$retVal = null;
exec($cmd, $output, $retVal);
#unlink($diffFile);

if ($retVal === 0) {
// Everything went well.
$filesPatched = count($output);
echo "Patched $filesPatched files\n";
$exit = 1;
} else {
print_r($output);
echo "Returned: $retVal\n";
$exit = 3;
}
}

if (class_exists('PHP_Timer', false) === true) {
echo PHP_Timer::resourceUsage().PHP_EOL.PHP_EOL;
}

exit($exit);

?>

0 comments on commit 402f2c3

Please sign in to comment.