Skip to content

Commit

Permalink
phpcs and phpcbf scripts now include all their logic in the CLI class…
Browse files Browse the repository at this point in the history
…. This allows the phars to be built much more easily. Also changed phar building to be much simplier, removing all options and building both files at once.
  • Loading branch information
gsherwood committed Mar 30, 2014
1 parent 6f03a43 commit f841cd2
Show file tree
Hide file tree
Showing 4 changed files with 187 additions and 662 deletions.
131 changes: 131 additions & 0 deletions CodeSniffer/CLI.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* @link http://pear.php.net/package/PHP_CodeSniffer
*/

error_reporting(E_ALL | E_STRICT);

if (is_file(dirname(__FILE__).'/../CodeSniffer.php') === true) {
include_once dirname(__FILE__).'/../CodeSniffer.php';
} else {
Expand Down Expand Up @@ -71,6 +73,135 @@ class PHP_CodeSniffer_CLI
private $_cliArgs = array();


/**
* Run the PHPCS script.
*
* @return array
*/
public function runphpcs()
{
if (is_file(dirname(__FILE__).'/../CodeSniffer/Reporting.php') === true) {
include_once dirname(__FILE__).'/../CodeSniffer/Reporting.php';
} else {
include_once 'PHP/CodeSniffer/Reporting.php';
}

PHP_CodeSniffer_Reporting::startTiming();
$this->checkRequirements();
$numErrors = $this->process();
if ($numErrors === 0) {
exit(0);
} else {
exit(1);
}

}//end runphpcs()


/**
* Run the PHPCBF script.
*
* @return array
*/
public function runphpcbf()
{
if (defined('PHP_CODESNIFFER_CBF') === false) {
define('PHP_CODESNIFFER_CBF', true);
}

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

PHP_CodeSniffer_Reporting::startTiming();
$this->checkRequirements();

$this->dieOnUnknownArg = false;

// Override some of the command line settings that might break the fixes.
$cliValues = $this->getCommandLineValues();
$cliValues['generator'] = '';
$cliValues['explain'] = false;
$cliValues['interactive'] = false;
$cliValues['showSources'] = false;
$cliValues['reportFile'] = null;
$cliValues['generator'] = '';
$cliValues['reports'] = array();

$suffix = '';
if (isset($cliValues['suffix']) === true) {
$suffix = $cliValues['suffix'];
}

$allowPatch = true;
if (isset($cliValues['no-patch']) === true || empty($cliValues['files']) === true) {
// They either asked for this,
// or they are using STDIN, which can't use diff.
$allowPatch = false;
}

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 = $this->process($cliValues);

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;
}
}//end if
} else {
if ($numErrors === 0) {
// No errors left unfixed.
$exit = 0;
} else {
// Errors we can't fix.
$exit = 2;
}
}//end if

PHP_CodeSniffer_Reporting::printRunTime();
exit($exit);

}//end runphpcbf()


/**
* Exits if the minimum requirements of PHP_CodSniffer are not met.
*
Expand Down
Loading

0 comments on commit f841cd2

Please sign in to comment.