Skip to content
This repository has been archived by the owner on May 25, 2020. It is now read-only.

Commit

Permalink
Added support for --report argument. Only possible after upgrade to P…
Browse files Browse the repository at this point in the history
…HPCS 2
  • Loading branch information
morozov committed Mar 31, 2015
1 parent 7b9084a commit 56d6a38
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 79 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"license": "MIT",
"require": {
"morozov/bootstrap": "1.0.*",
"squizlabs/php_codesniffer": "1.5.*"
"squizlabs/php_codesniffer": "2.*"
},
"autoload": {
"files": ["src/functions.php"],
Expand Down
46 changes: 18 additions & 28 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @license http://mit-license.org/ MIT Licence
* @link http://github.com/morozov/diff-sniffer-core
*/
class PHP_CodeSniffer_Reports_Xml implements PHP_CodeSniffer_Report
class PHP_CodeSniffer_Reports_DiffSniffer implements PHP_CodeSniffer_Report
{
/**
* Temporary diff file path.
Expand All @@ -40,6 +40,13 @@ class PHP_CodeSniffer_Reports_Xml implements PHP_CodeSniffer_Report
*/
protected $baseDir;

/**
* Requested report type
*
* @var string
*/
protected $reportType;

/**
* Constructor.
*/
Expand All @@ -60,84 +67,78 @@ public function __construct()
);
}
$this->baseDir = $baseDir;

$this->reportType = $this->getEnv('PHPCS_REPORT_TYPE');
}

/**
* Retrieves the path specified by environment variable.
* Retrieves the value of environment variable.
*
* @param string $varName Environment variable name
*
* @return string
* @throws PHP_CodeSniffer_Exception
*/
protected function getPath($varName)
protected function getEnv($varName)
{
if (!isset($_SERVER[$varName])) {
throw new PHP_CodeSniffer_Exception(
$varName . ' environment variable is not set'
);
}

$path = realpath($_SERVER[$varName]);
return $_SERVER[$varName];
}

/**
* Retrieves the path specified by environment variable.
*
* @param string $varName Environment variable name
*
* @return string
* @throws PHP_CodeSniffer_Exception
*/
protected function getPath($varName)
{
$value = $this->getEnv($varName);
$path = realpath($value);

if (false === $path) {
throw new PHP_CodeSniffer_Exception(
$_SERVER[$varName] . ' path does not exist'
$value . ' path does not exist'
);
}

return $path;
}

/**
* 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 boolean $showSources Show sources?
* @param int $width Maximum allowed line width.
*
* @return boolean
*/
/** {@inheritDoc} */
public function generateFileReport(
$report,
$showSources=false,
$width=80
PHP_CodeSniffer_File $phpcsFile,
$showSources = false,
$width = 80
) {
$diff = $this->getStagedDiff();
$changes = $this->getChanges($diff);

$report = $this->filterReport($report, $changes);

$full = new PHP_CodeSniffer_Reports_Full();
return $full->generateFileReport($report, $showSources, $width);
$reporting = new PHP_CodeSniffer_Reporting();
$actual = $reporting->factory($this->reportType);
return $actual->generateFileReport($report, $phpcsFile, $showSources, $width);
}

/**
* 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 boolean $showSources Show sources?
* @param int $width Maximum allowed line width.
* @param boolean $toScreen Is the report being printed to screen?
*
* @return void
*/
/** {@inheritDoc} */
public function generate(
$cachedData,
$totalFiles,
$totalErrors,
$totalWarnings,
$showSources=false,
$width=80,
$toScreen=true
$totalFixable,
$showSources = false,
$width = 80,
$toScreen = true
) {
echo $cachedData;

Expand Down Expand Up @@ -233,23 +234,28 @@ protected function repairReport(array $report)
$repaired = array_merge($report, array(
'errors' => 0,
'warnings' => 0,
'fixable' => 0,
));

foreach ($report['messages'] as $columns) {
foreach ($columns as $messages) {
foreach ($messages as $message) {
switch($message['type']) {
case 'ERROR';
case 'ERROR':
$key = 'errors';
break;
case 'WARNING';
case 'WARNING':
$key = 'warnings';
break;
default;
default:
$key = null;
continue;
}
$repaired[$key]++;

if ($message['fixable']) {
$repaired['fixable']++;
}
}
}
}
Expand Down
17 changes: 9 additions & 8 deletions src/DiffSniffer/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,30 +156,31 @@ protected function createTempFile()
*/
protected function runCodeSniffer($dir, $diffPath, array $options = array())
{
include_once __DIR__ . '/CodeSniffer/Reports/Xml.php';
include_once __DIR__ . '/CodeSniffer/Reports/DiffSniffer.php';

// remove report-related options since we use custom report as a hack-filter
$options = array_filter(
$options,
function ($options) {
return strpos($options, '--report') === false;
$reportType = 'full';
foreach ($options as $i => $value) {
if (strpos($value, '--report=') !== false) {
$reportType = substr($value, 9);
unset($options[$i]);
}
);
}

$_SERVER['argv'] = array_merge(
array(
$_SERVER['argv'][0],
),
$options,
array(
'--report=xml',
'--report=diffSniffer',
$dir,
)
);
$_SERVER['argc'] = count($_SERVER['argv']);

$_SERVER['PHPCS_DIFF_PATH'] = $diffPath;
$_SERVER['PHPCS_BASE_DIR'] = $dir;
$_SERVER['PHPCS_REPORT_TYPE'] = $reportType;

$cli = new PHP_CodeSniffer_CLI();
$cli->checkRequirements();
Expand Down

0 comments on commit 56d6a38

Please sign in to comment.