3
* A class to manage reporting.
8
* @package PHP_CodeSniffer
9
* @author Gabriele Santini <gsantini@sqli.com>
10
* @author Greg Sherwood <gsherwood@squiz.net>
11
* @copyright 2009-2014 SQLI <www.sqli.com>
12
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
13
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
14
* @link http://pear.php.net/package/PHP_CodeSniffer
17
if (is_file(dirname(__FILE__).'/../CodeSniffer.php') === true) {
18
include_once dirname(__FILE__).'/../CodeSniffer.php';
20
include_once 'PHP/CodeSniffer.php';
24
* A class to manage reporting.
27
* @package PHP_CodeSniffer
28
* @author Gabriele Santini <gsantini@sqli.com>
29
* @author Greg Sherwood <gsherwood@squiz.net>
30
* @copyright 2009-2014 SQLI <www.sqli.com>
31
* @copyright 2006-2014 Squiz Pty Ltd (ABN 77 084 670 600)
32
* @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
33
* @version Release: 1.5.4
34
* @link http://pear.php.net/package/PHP_CodeSniffer
36
class PHP_CodeSniffer_Reporting
40
* Total number of files that contain errors or warnings.
44
public $totalFiles = 0;
47
* Total number of errors found during the run.
51
public $totalErrors = 0;
54
* Total number of warnings found during the run.
58
public $totalWarnings = 0;
61
* A list of reports that have written partial report output.
65
private $_cachedReports = array();
68
* A cache of report objects.
72
private $_reports = array();
75
* A cache of opened tmp files.
79
private $_tmpFiles = array();
83
* Produce the appropriate report object based on $type parameter.
85
* @param string $type The type of the report.
87
* @return PHP_CodeSniffer_Report
88
* @throws PHP_CodeSniffer_Exception If report is not available.
90
public function factory($type)
92
$type = ucfirst($type);
93
if (isset($this->_reports[$type]) === true) {
94
return $this->_reports[$type];
97
$filename = $type.'.php';
98
$reportClassName = 'PHP_CodeSniffer_Reports_'.$type;
99
if (class_exists($reportClassName, true) === false) {
100
throw new PHP_CodeSniffer_Exception('Report type "'.$type.'" not found.');
103
$reportClass = new $reportClassName();
104
if (false === ($reportClass instanceof PHP_CodeSniffer_Report)) {
105
throw new PHP_CodeSniffer_Exception('Class "'.$reportClassName.'" must implement the "PHP_CodeSniffer_Report" interface.');
108
$this->_reports[$type] = $reportClass;
109
return $this->_reports[$type];
115
* Actually generates the report.
117
* @param PHP_CodeSniffer_File $phpcsFile The file that has been processed.
118
* @param array $cliValues An array of command line arguments.
122
public function cacheFileReport(PHP_CodeSniffer_File $phpcsFile, array $cliValues)
124
if (isset($cliValues['reports']) === false) {
125
// This happens during unit testing, or any time someone just wants
126
// the error data and not the printed report.
130
$reportData = $this->prepareFileReport($phpcsFile);
131
$errorsShown = false;
133
foreach ($cliValues['reports'] as $report => $output) {
134
$reportClass = self::factory($report);
137
$result = $reportClass->generateFileReport($reportData, $cliValues['showSources'], $cliValues['reportWidth']);
138
if ($result === true) {
142
$generatedReport = ob_get_contents();
145
if ($generatedReport === '') {
149
if ($output === null && $cliValues['reportFile'] !== null) {
150
$output = $cliValues['reportFile'];
153
if ($output === null) {
154
// Using a temp file.
155
if (isset($this->_tmpFiles[$report]) === false) {
156
$this->_tmpFiles[$report] = tmpfile();
159
fwrite($this->_tmpFiles[$report], $generatedReport);
161
$flags = FILE_APPEND;
162
if (in_array($report, $this->_cachedReports) === false) {
163
$this->_cachedReports[] = $report;
167
file_put_contents($output, $generatedReport, $flags);
171
if ($errorsShown === true) {
173
$this->totalErrors += $reportData['errors'];
174
$this->totalWarnings += $reportData['warnings'];
177
}//end cacheFileReport()
181
* Actually generates the report.
183
* @param string $report Report type.
184
* @param boolean $showSources Show sources?
185
* @param string $reportFile Report file to generate.
186
* @param integer $reportWidth Report max width.
190
public function printReport(
196
$reportClass = self::factory($report);
198
if ($reportFile !== null) {
199
$filename = $reportFile;
203
if (file_exists($filename) === true) {
204
$reportCache = file_get_contents($filename);
209
if (isset($this->_tmpFiles[$report]) === true) {
210
$data = stream_get_meta_data($this->_tmpFiles[$report]);
211
$filename = $data['uri'];
212
$reportCache = file_get_contents($filename);
213
fclose($this->_tmpFiles[$report]);
222
$reportClass->generate(
226
$this->totalWarnings,
232
if ($reportFile !== null) {
233
$generatedReport = ob_get_contents();
236
if (PHP_CODESNIFFER_VERBOSITY > 0) {
237
echo $generatedReport;
240
$generatedReport = trim($generatedReport);
241
file_put_contents($reportFile, $generatedReport.PHP_EOL);
242
} else if ($filename !== null && file_exists($filename) === true) {
246
return ($this->totalErrors + $this->totalWarnings);
252
* Pre-process and package violations for all files.
254
* Used by error reports to get a packaged list of all errors in each file.
256
* @param PHP_CodeSniffer_File $phpcsFile The file that has been processed.
260
public function prepareFileReport(PHP_CodeSniffer_File $phpcsFile)
263
'filename' => $phpcsFile->getFilename(),
264
'errors' => $phpcsFile->getErrorCount(),
265
'warnings' => $phpcsFile->getWarningCount(),
266
'messages' => array(),
269
if ($report['errors'] === 0 && $report['warnings'] === 0) {
276
// Merge errors and warnings.
277
foreach ($phpcsFile->getErrors() as $line => $lineErrors) {
278
if (is_array($lineErrors) === false) {
282
foreach ($lineErrors as $column => $colErrors) {
283
$newErrors = array();
284
foreach ($colErrors as $data) {
285
$newErrors[] = array(
286
'message' => $data['message'],
287
'source' => $data['source'],
288
'severity' => $data['severity'],
293
$errors[$line][$column] = $newErrors;
296
ksort($errors[$line]);
299
foreach ($phpcsFile->getWarnings() as $line => $lineWarnings) {
300
if (is_array($lineWarnings) === false) {
304
foreach ($lineWarnings as $column => $colWarnings) {
305
$newWarnings = array();
306
foreach ($colWarnings as $data) {
307
$newWarnings[] = array(
308
'message' => $data['message'],
309
'source' => $data['source'],
310
'severity' => $data['severity'],
315
if (isset($errors[$line]) === false) {
316
$errors[$line] = array();
319
if (isset($errors[$line][$column]) === true) {
320
$errors[$line][$column] = array_merge(
322
$errors[$line][$column]
325
$errors[$line][$column] = $newWarnings;
329
ksort($errors[$line]);
333
$report['messages'] = $errors;
336
}//end prepareFileReport()