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

Commit 56d6a38

Browse files
committed
Added support for --report argument. Only possible after upgrade to PHPCS 2
1 parent 7b9084a commit 56d6a38

File tree

4 files changed

+76
-79
lines changed

4 files changed

+76
-79
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"license": "MIT",
1111
"require": {
1212
"morozov/bootstrap": "1.0.*",
13-
"squizlabs/php_codesniffer": "1.5.*"
13+
"squizlabs/php_codesniffer": "2.*"
1414
},
1515
"autoload": {
1616
"files": ["src/functions.php"],

composer.lock

Lines changed: 18 additions & 28 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/DiffSniffer/CodeSniffer/Reports/Xml.php renamed to src/DiffSniffer/CodeSniffer/Reports/DiffSniffer.php

Lines changed: 48 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
* @license http://mit-license.org/ MIT Licence
2525
* @link http://github.com/morozov/diff-sniffer-core
2626
*/
27-
class PHP_CodeSniffer_Reports_Xml implements PHP_CodeSniffer_Report
27+
class PHP_CodeSniffer_Reports_DiffSniffer implements PHP_CodeSniffer_Report
2828
{
2929
/**
3030
* Temporary diff file path.
@@ -40,6 +40,13 @@ class PHP_CodeSniffer_Reports_Xml implements PHP_CodeSniffer_Report
4040
*/
4141
protected $baseDir;
4242

43+
/**
44+
* Requested report type
45+
*
46+
* @var string
47+
*/
48+
protected $reportType;
49+
4350
/**
4451
* Constructor.
4552
*/
@@ -60,84 +67,78 @@ public function __construct()
6067
);
6168
}
6269
$this->baseDir = $baseDir;
70+
71+
$this->reportType = $this->getEnv('PHPCS_REPORT_TYPE');
6372
}
6473

6574
/**
66-
* Retrieves the path specified by environment variable.
75+
* Retrieves the value of environment variable.
6776
*
6877
* @param string $varName Environment variable name
6978
*
7079
* @return string
7180
* @throws PHP_CodeSniffer_Exception
7281
*/
73-
protected function getPath($varName)
82+
protected function getEnv($varName)
7483
{
7584
if (!isset($_SERVER[$varName])) {
7685
throw new PHP_CodeSniffer_Exception(
7786
$varName . ' environment variable is not set'
7887
);
7988
}
8089

81-
$path = realpath($_SERVER[$varName]);
90+
return $_SERVER[$varName];
91+
}
92+
93+
/**
94+
* Retrieves the path specified by environment variable.
95+
*
96+
* @param string $varName Environment variable name
97+
*
98+
* @return string
99+
* @throws PHP_CodeSniffer_Exception
100+
*/
101+
protected function getPath($varName)
102+
{
103+
$value = $this->getEnv($varName);
104+
$path = realpath($value);
82105

83106
if (false === $path) {
84107
throw new PHP_CodeSniffer_Exception(
85-
$_SERVER[$varName] . ' path does not exist'
108+
$value . ' path does not exist'
86109
);
87110
}
88111

89112
return $path;
90113
}
91114

92-
/**
93-
* Generate a partial report for a single processed file.
94-
*
95-
* Function should return TRUE if it printed or stored data about the file
96-
* and FALSE if it ignored the file. Returning TRUE indicates that the file and
97-
* its data should be counted in the grand totals.
98-
*
99-
* @param array $report Prepared report data.
100-
* @param boolean $showSources Show sources?
101-
* @param int $width Maximum allowed line width.
102-
*
103-
* @return boolean
104-
*/
115+
/** {@inheritDoc} */
105116
public function generateFileReport(
106117
$report,
107-
$showSources=false,
108-
$width=80
118+
PHP_CodeSniffer_File $phpcsFile,
119+
$showSources = false,
120+
$width = 80
109121
) {
110122
$diff = $this->getStagedDiff();
111123
$changes = $this->getChanges($diff);
112124

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

115-
$full = new PHP_CodeSniffer_Reports_Full();
116-
return $full->generateFileReport($report, $showSources, $width);
127+
$reporting = new PHP_CodeSniffer_Reporting();
128+
$actual = $reporting->factory($this->reportType);
129+
return $actual->generateFileReport($report, $phpcsFile, $showSources, $width);
117130
}
118131

119-
/**
120-
* Prints all errors and warnings for each file processed.
121-
*
122-
* @param string $cachedData Any partial report data that was returned from
123-
* generateFileReport during the run.
124-
* @param int $totalFiles Total number of files processed during the run.
125-
* @param int $totalErrors Total number of errors found during the run.
126-
* @param int $totalWarnings Total number of warnings found during the run.
127-
* @param boolean $showSources Show sources?
128-
* @param int $width Maximum allowed line width.
129-
* @param boolean $toScreen Is the report being printed to screen?
130-
*
131-
* @return void
132-
*/
132+
/** {@inheritDoc} */
133133
public function generate(
134134
$cachedData,
135135
$totalFiles,
136136
$totalErrors,
137137
$totalWarnings,
138-
$showSources=false,
139-
$width=80,
140-
$toScreen=true
138+
$totalFixable,
139+
$showSources = false,
140+
$width = 80,
141+
$toScreen = true
141142
) {
142143
echo $cachedData;
143144

@@ -233,23 +234,28 @@ protected function repairReport(array $report)
233234
$repaired = array_merge($report, array(
234235
'errors' => 0,
235236
'warnings' => 0,
237+
'fixable' => 0,
236238
));
237239

238240
foreach ($report['messages'] as $columns) {
239241
foreach ($columns as $messages) {
240242
foreach ($messages as $message) {
241243
switch($message['type']) {
242-
case 'ERROR';
244+
case 'ERROR':
243245
$key = 'errors';
244246
break;
245-
case 'WARNING';
247+
case 'WARNING':
246248
$key = 'warnings';
247249
break;
248-
default;
250+
default:
249251
$key = null;
250252
continue;
251253
}
252254
$repaired[$key]++;
255+
256+
if ($message['fixable']) {
257+
$repaired['fixable']++;
258+
}
253259
}
254260
}
255261
}

src/DiffSniffer/Runner.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,30 +156,31 @@ protected function createTempFile()
156156
*/
157157
protected function runCodeSniffer($dir, $diffPath, array $options = array())
158158
{
159-
include_once __DIR__ . '/CodeSniffer/Reports/Xml.php';
159+
include_once __DIR__ . '/CodeSniffer/Reports/DiffSniffer.php';
160160

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

169169
$_SERVER['argv'] = array_merge(
170170
array(
171171
$_SERVER['argv'][0],
172172
),
173173
$options,
174174
array(
175-
'--report=xml',
175+
'--report=diffSniffer',
176176
$dir,
177177
)
178178
);
179179
$_SERVER['argc'] = count($_SERVER['argv']);
180180

181181
$_SERVER['PHPCS_DIFF_PATH'] = $diffPath;
182182
$_SERVER['PHPCS_BASE_DIR'] = $dir;
183+
$_SERVER['PHPCS_REPORT_TYPE'] = $reportType;
183184

184185
$cli = new PHP_CodeSniffer_CLI();
185186
$cli->checkRequirements();

0 commit comments

Comments
 (0)