Skip to content

Commit

Permalink
By default, phpcbf will replace files using the patch command. A coup…
Browse files Browse the repository at this point in the history
…le of vars can be changed in the script to now replace the file using PHP or output a new copy of the file using a filename suffix. When not using the patch command, a new report type is used: Cbf. This report cannot be selected from the command line and is only used to update file content when running with phpcbf.
  • Loading branch information
gsherwood committed Aug 19, 2013
1 parent fddf489 commit 8f62e7f
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 27 deletions.
117 changes: 117 additions & 0 deletions CodeSniffer/Reports/Cbf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
/**
* CBF report for PHP_CodeSniffer.
*
* This report implements the various auto-fixing features of the
* PHPCBF script and is not intended (or allowed) to be selected as a
* report from the command line.
*
* 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
*/

/**
* CBF report for PHP_CodeSniffer.
*
* This report implements the various auto-fixing features of the
* PHPCBF script and is not intended (or allowed) to be selected as a
* report from the command line.
*
* 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
* @version Release: @package_version@
* @link http://pear.php.net/package/PHP_CodeSniffer
*/
class PHP_CodeSniffer_Reports_Cbf implements PHP_CodeSniffer_Report
{


/**
* Generate a partial report for a single processed file.
*
* Function should return TRUE if it printed or stored data about the file
* and FALSE if it ignored the file. Returning TRUE indicates that the file and
* its data should be counted in the grand totals.
*
* @param array $report Prepared report data.
* @param PHP_CodeSniffer_File $phpcsFile The file being reported on.
* @param boolean $showSources Show sources?
* @param int $width Maximum allowed line width.
*
* @return boolean
*/
public function generateFileReport(
$report,
PHP_CodeSniffer_File $phpcsFile,
$showSources=false,
$width=80
) {
ob_start();
$diff = $phpcsFile->phpcs->reporting->factory('diff');
$changed = $diff->generateFileReport($report, $phpcsFile);
ob_end_clean();

if ($changed === true) {
$cliValues = $phpcsFile->phpcs->cli->getCommandLineValues();
$newFilename = $report['filename'].$cliValues['phpcbf-suffix'];
$newContent = $phpcsFile->fixer->getContents();
file_put_contents($newFilename, $newContent);

echo 'Fixed '.$report['fixable'].' sniff violations in '.$report['filename'].PHP_EOL;
if ($newFilename === $report['filename']) {
echo "\t=> file was overwritten".PHP_EOL;
} else {
echo "\t=> fixed file written to ".basename($newFilename).PHP_EOL;
}
}

return $changed;

}//end generateFileReport()


/**
* Prints all errors and warnings for each file processed.
*
* @param string $cachedData Any partial report data that was returned from
* generateFileReport during the run.
* @param int $totalFiles Total number of files processed during the run.
* @param int $totalErrors Total number of errors found during the run.
* @param int $totalWarnings Total number of warnings found during the run.
* @param int $totalFixable Total number of problems that can be fixed.
* @param boolean $showSources Show sources?
* @param int $width Maximum allowed line width.
* @param boolean $toScreen Is the report being printed to screen?
*
* @return void
*/
public function generate(
$cachedData,
$totalFiles,
$totalErrors,
$totalWarnings,
$totalFixable,
$showSources=false,
$width=80,
$toScreen=true
) {
echo $cachedData;
echo "Fixed $totalFiles files".PHP_EOL;

}//end generate()


}//end class

?>
3 changes: 3 additions & 0 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
</file>
</dir>
<dir name="Reports">
<file baseinstalldir="PHP" name="Cbf.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
<file baseinstalldir="PHP" name="Checkstyle.php" role="php">
<tasks:replace from="@package_version@" to="version" type="package-info" />
</file>
Expand Down
75 changes: 48 additions & 27 deletions scripts/phpcbf
Original file line number Diff line number Diff line change
Expand Up @@ -32,45 +32,66 @@ $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';

// Override some of the command line settings that might break the fixes.
$cliValues['generator'] = '';
$cliValues['explain'] = false;
$cliValues['reports'] = array('diff' => $diffFile);

if (file_exists($diffFile) === true) {
unlink($diffFile);
$suffix = '';
$allowPatch = true;

if ($suffix === '' && $allowPatch === true) {
// Using the diff/patch tools.
$diffFile = getcwd().'/phpcbf-fixed.diff';
$cliValues['reports'] = array('diff' => $diffFile);
if (file_exists($diffFile) === true) {
unlink($diffFile);
}
} else {
// Replace the file without the patch command
// or writing to a file with a new suffix.
$cliValues['reports'] = array('cbf' => null);
$cliValues['phpcbf-suffix'] = $suffix;
}

$numErrors = $phpcs->process($cliValues);
if (file_exists($diffFile) === false) {
// Nothing to fix.


if ($suffix === '' && $allowPatch === true) {
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;
}
}
} else {
if ($numErrors === 0) {
// And no errors reported.
// No errors left unfixed.
$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) {
Expand Down

0 comments on commit 8f62e7f

Please sign in to comment.