Skip to content

Commit 32e4661

Browse files
committed
Fixed lots of coding standard errors and a couple of minor report bugs
1 parent 47d54c4 commit 32e4661

File tree

11 files changed

+315
-246
lines changed

11 files changed

+315
-246
lines changed

CodeSniffer/Report.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ interface PHP_CodeSniffer_Report
3232

3333

3434
/**
35-
* Generate the actual report.
36-
*
37-
* @param array $report Prepared report.
35+
* Generate a partial report for a single processed file.
36+
*
37+
* Function should return TRUE if it printed or stored data about the file
38+
* and FALSE if it ignored the file. Returning TRUE indicates that the file and
39+
* its data should be counted in the grand totals.
40+
*
41+
* @param array $report Prepared report data.
3842
* @param boolean $showSources Show sources?
39-
* @param int $width Maximum allowed lne width.
43+
* @param int $width Maximum allowed line width.
4044
*
41-
* @return string
45+
* @return boolean
4246
*/
4347
public function generateFileReport(
4448
$report,
@@ -50,12 +54,16 @@ public function generateFileReport(
5054
/**
5155
* Generate the actual report.
5256
*
53-
* @param array $report Prepared report.
54-
* @param boolean $showSources Show sources?
55-
* @param int $width Maximum allowed lne width.
56-
* @param boolean $toScreen Is the report being printed to screen?
57+
* @param string $cachedData Any partial report data that was returned from
58+
* generateFileReport during the run.
59+
* @param int $totalFiles Total number of files processed during the run.
60+
* @param int $totalErrors Total number of errors found during the run.
61+
* @param int $totalWarnings Total number of warnings found during the run.
62+
* @param boolean $showSources Show sources?
63+
* @param int $width Maximum allowed line width.
64+
* @param boolean $toScreen Is the report being printed to screen?
5765
*
58-
* @return string
66+
* @return void
5967
*/
6068
public function generate(
6169
$cachedData,

CodeSniffer/Reporting.php

Lines changed: 90 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,46 @@
3636
class PHP_CodeSniffer_Reporting
3737
{
3838

39-
private $_cachedReports = array();
40-
private $_reports = array();
41-
39+
/**
40+
* Total number of files that contain errors or warnings.
41+
*
42+
* @var int
43+
*/
4244
public $totalFiles = 0;
45+
46+
/**
47+
* Total number of errors found during the run.
48+
*
49+
* @var int
50+
*/
4351
public $totalErrors = 0;
52+
53+
/**
54+
* Total number of warnings found during the run.
55+
*
56+
* @var int
57+
*/
4458
public $totalWarnings = 0;
4559

60+
/**
61+
* A list of reports that have written partial report output.
62+
*
63+
* @var array
64+
*/
65+
private $_cachedReports = array();
4666

67+
/**
68+
* A cache of report objects.
69+
*
70+
* @var array
71+
*/
72+
private $_reports = array();
4773

4874

4975
/**
5076
* Produce the appropriate report object based on $type parameter.
5177
*
52-
* @param string $type Demanded report type.
78+
* @param string $type The type of the report.
5379
*
5480
* @return PHP_CodeSniffer_Report
5581
* @throws PHP_CodeSniffer_Exception If report is not available.
@@ -81,11 +107,59 @@ public function factory($type)
81107
/**
82108
* Actually generates the report.
83109
*
84-
* @param string $report Report type.
85-
* @param array $filesViolations Collected violations.
86-
* @param boolean $showSources Show sources?
87-
* @param string $reportFile Report file to generate.
88-
* @param integer $reportWidth Report max width.
110+
* @param PHP_CodeSniffer_File $phpcsFile The file that has been processed.
111+
* @param array $cliValues An array of command line arguments.
112+
*
113+
* @return void
114+
*/
115+
public function cacheFileReport(PHP_CodeSniffer_File $phpcsFile, array $cliValues)
116+
{
117+
$reportData = $this->prepareFileReport($phpcsFile);
118+
$errorsShown = false;
119+
120+
foreach ($cliValues['reports'] as $report => $output) {
121+
$reportClass = self::factory($report);
122+
123+
ob_start();
124+
$result = $reportClass->generateFileReport($reportData, $cliValues['showSources'], $cliValues['reportWidth']);
125+
if ($result === true) {
126+
$errorsShown = true;
127+
}
128+
129+
$generatedReport = ob_get_contents();
130+
ob_end_clean();
131+
132+
if ($generatedReport !== '') {
133+
$flags = FILE_APPEND;
134+
if (in_array($report, $this->_cachedReports) === false) {
135+
$this->_cachedReports[] = $report;
136+
$flags = null;
137+
}
138+
139+
if ($output === null) {
140+
$output = PHPCS_CWD.'/phpcs-'.$report.'.tmp';
141+
}
142+
143+
file_put_contents($output, $generatedReport, $flags);
144+
}
145+
}//end foreach
146+
147+
if ($errorsShown === true) {
148+
$this->totalFiles++;
149+
$this->totalErrors += $reportData['errors'];
150+
$this->totalWarnings += $reportData['warnings'];
151+
}
152+
153+
}//end cacheFileReport()
154+
155+
156+
/**
157+
* Actually generates the report.
158+
*
159+
* @param string $report Report type.
160+
* @param boolean $showSources Show sources?
161+
* @param string $reportFile Report file to generate.
162+
* @param integer $reportWidth Report max width.
89163
*
90164
* @return integer
91165
*/
@@ -117,7 +191,8 @@ public function printReport(
117191
$reportCache = '';
118192
}
119193

120-
$reportClass->generate($reportCache,
194+
$reportClass->generate(
195+
$reportCache,
121196
$this->totalFiles,
122197
$this->totalErrors,
123198
$this->totalWarnings,
@@ -128,10 +203,10 @@ public function printReport(
128203

129204
if ($reportFile !== null) {
130205
$generatedReport = ob_get_contents();
206+
ob_end_clean();
207+
131208
if (PHP_CODESNIFFER_VERBOSITY > 0) {
132-
ob_end_flush();
133-
} else {
134-
ob_end_clean();
209+
echo $generatedReport;
135210
}
136211

137212
$generatedReport = trim($generatedReport);
@@ -145,67 +220,12 @@ public function printReport(
145220
}//end printReport()
146221

147222

148-
/**
149-
* Actually generates the report.
150-
*
151-
* @param string $report Report type.
152-
* @param array $filesViolations Collected violations.
153-
* @param boolean $showSources Show sources?
154-
* @param string $reportFile Report file to generate.
155-
* @param integer $reportWidth Report max width.
156-
*
157-
* @return integer
158-
*/
159-
public function cacheFileReport(
160-
PHP_CodeSniffer_File $phpcsFile,
161-
array $cliValues
162-
) {
163-
164-
$reportData = $this->prepareFileReport($phpcsFile);
165-
$errorsShown = false;
166-
167-
foreach ($cliValues['reports'] as $report => $output) {
168-
$reportClass = self::factory($report);
169-
170-
ob_start();
171-
$result = $reportClass->generateFileReport($reportData, $cliValues['showSources'], $cliValues['reportWidth']);
172-
if ($result === true) {
173-
$errorsShown = true;
174-
}
175-
176-
$generatedReport = ob_get_contents();
177-
ob_end_clean();
178-
179-
if ($generatedReport !== '') {
180-
$flags = FILE_APPEND;
181-
if (in_array($report, $this->_cachedReports) === false) {
182-
$this->_cachedReports[] = $report;
183-
$flags = null;
184-
}
185-
186-
if ($output === null) {
187-
$output = PHPCS_CWD.'/phpcs-'.$report.'.tmp';
188-
}
189-
190-
file_put_contents($output, $generatedReport, $flags);
191-
}
192-
}//end foreach
193-
194-
if ($errorsShown === true) {
195-
$this->totalFiles++;
196-
$this->totalErrors += $reportData['errors'];
197-
$this->totalWarnings += $reportData['warnings'];
198-
}
199-
200-
}//end printReport()
201-
202-
203223
/**
204224
* Pre-process and package violations for all files.
205225
*
206226
* Used by error reports to get a packaged list of all errors in each file.
207227
*
208-
* @param array $filesViolations List of found violations.
228+
* @param PHP_CodeSniffer_File $phpcsFile The file that has been processed.
209229
*
210230
* @return array
211231
*/
@@ -277,7 +297,7 @@ public function prepareFileReport(PHP_CodeSniffer_File $phpcsFile)
277297
$report['messages'] = $errors;
278298
return $report;
279299

280-
}//end prepare()
300+
}//end prepareFileReport()
281301

282302

283303
}//end class

CodeSniffer/Reports/Checkstyle.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,17 @@ class PHP_CodeSniffer_Reports_Checkstyle implements PHP_CodeSniffer_Report
3434

3535

3636
/**
37-
* Prints all violations for processed files, in a Checkstyle format.
37+
* Generate a partial report for a single processed file.
3838
*
39-
* Violations are grouped by file.
39+
* Function should return TRUE if it printed or stored data about the file
40+
* and FALSE if it ignored the file. Returning TRUE indicates that the file and
41+
* its data should be counted in the grand totals.
4042
*
41-
* @param array $report Prepared report.
43+
* @param array $report Prepared report data.
4244
* @param boolean $showSources Show sources?
43-
* @param int $width Maximum allowed lne width.
44-
* @param boolean $toScreen Is the report being printed to screen?
45+
* @param int $width Maximum allowed line width.
4546
*
46-
* @return string
47+
* @return boolean
4748
*/
4849
public function generateFileReport(
4950
$report,
@@ -86,19 +87,22 @@ public function generateFileReport(
8687

8788
return true;
8889

89-
}//end generate()
90+
}//end generateFileReport()
91+
9092

9193
/**
9294
* Prints all violations for processed files, in a Checkstyle format.
9395
*
94-
* Violations are grouped by file.
95-
*
96-
* @param array $report Prepared report.
97-
* @param boolean $showSources Show sources?
98-
* @param int $width Maximum allowed lne width.
99-
* @param boolean $toScreen Is the report being printed to screen?
96+
* @param string $cachedData Any partial report data that was returned from
97+
* generateFileReport during the run.
98+
* @param int $totalFiles Total number of files processed during the run.
99+
* @param int $totalErrors Total number of errors found during the run.
100+
* @param int $totalWarnings Total number of warnings found during the run.
101+
* @param boolean $showSources Show sources?
102+
* @param int $width Maximum allowed line width.
103+
* @param boolean $toScreen Is the report being printed to screen?
100104
*
101-
* @return string
105+
* @return void
102106
*/
103107
public function generate(
104108
$cachedData,

CodeSniffer/Reports/Csv.php

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,26 +32,25 @@
3232
class PHP_CodeSniffer_Reports_Csv implements PHP_CodeSniffer_Report
3333
{
3434

35+
3536
/**
36-
* Generates a summary of errors and warnings for each file processed.
37-
*
38-
* If verbose output is enabled, results are shown for all files, even if
39-
* they have no errors or warnings. If verbose output is disabled, we only
40-
* show files that have at least one warning or error.
41-
*
42-
* @param array $report Prepared report.
37+
* Generate a partial report for a single processed file.
38+
*
39+
* Function should return TRUE if it printed or stored data about the file
40+
* and FALSE if it ignored the file. Returning TRUE indicates that the file and
41+
* its data should be counted in the grand totals.
42+
*
43+
* @param array $report Prepared report data.
4344
* @param boolean $showSources Show sources?
44-
* @param int $width Maximum allowed lne width.
45-
* @param boolean $toScreen Is the report being printed to screen?
45+
* @param int $width Maximum allowed line width.
4646
*
47-
* @return string
47+
* @return boolean
4848
*/
4949
public function generateFileReport(
5050
$report,
5151
$showSources=false,
5252
$width=80
5353
) {
54-
5554
if ($report['errors'] === 0 && $report['warnings'] === 0) {
5655
// Nothing to print.
5756
return false;
@@ -78,12 +77,16 @@ public function generateFileReport(
7877
/**
7978
* Generates a csv report.
8079
*
81-
* @param array $report Prepared report.
82-
* @param boolean $showSources Show sources?
83-
* @param int $width Maximum allowed lne width.
84-
* @param boolean $toScreen Is the report being printed to screen?
85-
*
86-
* @return string
80+
* @param string $cachedData Any partial report data that was returned from
81+
* generateFileReport during the run.
82+
* @param int $totalFiles Total number of files processed during the run.
83+
* @param int $totalErrors Total number of errors found during the run.
84+
* @param int $totalWarnings Total number of warnings found during the run.
85+
* @param boolean $showSources Show sources?
86+
* @param int $width Maximum allowed line width.
87+
* @param boolean $toScreen Is the report being printed to screen?
88+
*
89+
* @return void
8790
*/
8891
public function generate(
8992
$cachedData,

0 commit comments

Comments
 (0)