Skip to content

Commit 6c96399

Browse files
committed
Reports are now able to get a count of fixable errors so they can disaply that count. Sniffs call a different function to add errors and warnings to indiciate that they are fixable.
1 parent e69f628 commit 6c96399

File tree

22 files changed

+257
-54
lines changed

22 files changed

+257
-54
lines changed

CodeSniffer/File.php

Lines changed: 110 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ class PHP_CodeSniffer_File
224224
*/
225225
private $_warningCount = 0;
226226

227+
/**
228+
* The total number of errors/warnings that can be fixed.
229+
*
230+
* @var int
231+
*/
232+
private $_fixableCount = 0;
233+
227234
/**
228235
* An array of sniffs listening to this file's processing.
229236
*
@@ -407,6 +414,7 @@ public function start($contents=null)
407414
$this->_warnings = array();
408415
$this->_errorCount = 0;
409416
$this->_warningCount = 0;
417+
$this->_fixableCount = 0;
410418

411419
$this->_parse($contents);
412420
$this->fixer->startFile($this);
@@ -435,6 +443,7 @@ public function start($contents=null)
435443
$this->_warnings = array();
436444
$this->_errorCount = 0;
437445
$this->_warningCount = 0;
446+
$this->_fixableCount = 0;
438447
return;
439448
} else if (strpos($token['content'], '@codingStandardsChangeSetting') !== false) {
440449
$start = strpos($token['content'], '@codingStandardsChangeSetting');
@@ -722,17 +731,24 @@ public static function detectLineEndings($file, $contents=null)
722731
/**
723732
* Adds an error to the error stack.
724733
*
725-
* @param string $error The error message.
726-
* @param int $stackPtr The stack position where the error occurred.
727-
* @param string $code A violation code unique to the sniff message.
728-
* @param array $data Replacements for the error message.
729-
* @param int $severity The severity level for this error. A value of 0
730-
* will be converted into the default severity level.
734+
* @param string $error The error message.
735+
* @param int $stackPtr The stack position where the error occurred.
736+
* @param string $code A violation code unique to the sniff message.
737+
* @param array $data Replacements for the error message.
738+
* @param int $severity The severity level for this error. A value of 0
739+
* will be converted into the default severity level.
740+
* @param boolean $fixable Can the error be fixed by the sniff?
731741
*
732742
* @return void
733743
*/
734-
public function addError($error, $stackPtr, $code='', $data=array(), $severity=0)
735-
{
744+
public function addError(
745+
$error,
746+
$stackPtr,
747+
$code='',
748+
$data=array(),
749+
$severity=0,
750+
$fixable=false
751+
) {
736752
// Don't bother doing any processing if errors are just going to
737753
// be hidden in the reports anyway.
738754
if ($this->phpcs->cli->errorSeverity === 0) {
@@ -804,6 +820,10 @@ public function addError($error, $stackPtr, $code='', $data=array(), $severity=0
804820
}
805821

806822
$this->_errorCount++;
823+
if ($fixable === true) {
824+
$this->_fixableCount++;
825+
}
826+
807827
if ($this->_recordErrors === false) {
808828
if (isset($this->_errors[$lineNum]) === false) {
809829
$this->_errors[$lineNum] = 0;
@@ -835,6 +855,7 @@ public function addError($error, $stackPtr, $code='', $data=array(), $severity=0
835855
'message' => $message,
836856
'source' => $sniff,
837857
'severity' => $severity,
858+
'fixable' => $fixable,
838859
);
839860

840861
}//end addError()
@@ -843,17 +864,24 @@ public function addError($error, $stackPtr, $code='', $data=array(), $severity=0
843864
/**
844865
* Adds an warning to the warning stack.
845866
*
846-
* @param string $warning The error message.
847-
* @param int $stackPtr The stack position where the error occurred.
848-
* @param string $code A violation code unique to the sniff message.
849-
* @param array $data Replacements for the warning message.
850-
* @param int $severity The severity level for this warning. A value of 0
851-
* will be converted into the default severity level.
867+
* @param string $warning The error message.
868+
* @param int $stackPtr The stack position where the error occurred.
869+
* @param string $code A violation code unique to the sniff message.
870+
* @param array $data Replacements for the warning message.
871+
* @param int $severity The severity level for this warning. A value of 0
872+
* will be converted into the default severity level.
873+
* @param boolean $fixable Can the warning be fixed by the sniff?
852874
*
853875
* @return void
854876
*/
855-
public function addWarning($warning, $stackPtr, $code='', $data=array(), $severity=0)
856-
{
877+
public function addWarning(
878+
$warning,
879+
$stackPtr,
880+
$code='',
881+
$data=array(),
882+
$severity=0,
883+
$fixable=false
884+
) {
857885
// Don't bother doing any processing if warnings are just going to
858886
// be hidden in the reports anyway.
859887
if ($this->phpcs->cli->warningSeverity === 0) {
@@ -925,6 +953,10 @@ public function addWarning($warning, $stackPtr, $code='', $data=array(), $severi
925953
}
926954

927955
$this->_warningCount++;
956+
if ($fixable === true) {
957+
$this->_fixableCount++;
958+
}
959+
928960
if ($this->_recordErrors === false) {
929961
if (isset($this->_warnings[$lineNum]) === false) {
930962
$this->_warnings[$lineNum] = 0;
@@ -956,11 +988,61 @@ public function addWarning($warning, $stackPtr, $code='', $data=array(), $severi
956988
'message' => $message,
957989
'source' => $sniff,
958990
'severity' => $severity,
991+
'fixable' => $fixable,
959992
);
960993

961994
}//end addWarning()
962995

963996

997+
/**
998+
* Adds a fixable error to the error stack.
999+
*
1000+
* @param string $error The error message.
1001+
* @param int $stackPtr The stack position where the error occurred.
1002+
* @param string $code A violation code unique to the sniff message.
1003+
* @param array $data Replacements for the error message.
1004+
* @param int $severity The severity level for this error. A value of 0
1005+
* will be converted into the default severity level.
1006+
*
1007+
* @return void
1008+
*/
1009+
public function addFixableError(
1010+
$error,
1011+
$stackPtr,
1012+
$code='',
1013+
$data=array(),
1014+
$severity=0
1015+
) {
1016+
$this->addError($error, $stackPtr, $code, $data, $severity, true);
1017+
1018+
}//end addFixableError()
1019+
1020+
1021+
/**
1022+
* Adds a fixable warning to the warning stack.
1023+
*
1024+
* @param string $warning The error message.
1025+
* @param int $stackPtr The stack position where the error occurred.
1026+
* @param string $code A violation code unique to the sniff message.
1027+
* @param array $data Replacements for the warning message.
1028+
* @param int $severity The severity level for this warning. A value of 0
1029+
* will be converted into the default severity level.
1030+
* @param boolean $fixable Can the warning be fixed by the sniff?
1031+
*
1032+
* @return void
1033+
*/
1034+
public function addFixableWarning(
1035+
$warning,
1036+
$stackPtr,
1037+
$code='',
1038+
$data=array(),
1039+
$severity=0
1040+
) {
1041+
$this->addWarning($warning, $stackPtr, $code, $data, $severity, true);
1042+
1043+
}//end addFixableWarning()
1044+
1045+
9641046
/**
9651047
* Returns the number of errors raised.
9661048
*
@@ -985,6 +1067,18 @@ public function getWarningCount()
9851067
}//end getWarningCount()
9861068

9871069

1070+
/**
1071+
* Returns the number of fixable errors/warnings raised.
1072+
*
1073+
* @return int
1074+
*/
1075+
public function getFixableCount()
1076+
{
1077+
return $this->_fixableCount;
1078+
1079+
}//end getFixableCount()
1080+
1081+
9881082
/**
9891083
* Returns the list of ignored lines.
9901084
*

CodeSniffer/Fixer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,11 @@ public function replaceToken($stackPtr, $content)
8181
return;
8282
}
8383

84-
#echo "replace token $stackPtr with \"$content\"\n";
84+
#$bt = debug_backtrace();
85+
#$sniff = $bt[1]['class'];
86+
#$line = $bt[0]['line'];
87+
#echo "$sniff (line $line): replace token $stackPtr with \"$content\"\n";
88+
8589
$this->_tokens[$stackPtr] = $content;
8690
$this->_numFixes++;
8791
$this->_fixedTokens[] = $stackPtr;

CodeSniffer/Report.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public function generateFileReport(
6161
* @param int $totalFiles Total number of files processed during the run.
6262
* @param int $totalErrors Total number of errors found during the run.
6363
* @param int $totalWarnings Total number of warnings found during the run.
64+
* @param int $totalFixable Total number of problems that can be fixed.
6465
* @param boolean $showSources Show sources?
6566
* @param int $width Maximum allowed line width.
6667
* @param boolean $toScreen Is the report being printed to screen?
@@ -72,6 +73,7 @@ public function generate(
7273
$totalFiles,
7374
$totalErrors,
7475
$totalWarnings,
76+
$totalFixable,
7577
$showSources=false,
7678
$width=80,
7779
$toScreen=true

CodeSniffer/Reporting.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,13 @@ class PHP_CodeSniffer_Reporting
5757
*/
5858
public $totalWarnings = 0;
5959

60+
/**
61+
* Total number of errors/warnings that can be fixed.
62+
*
63+
* @var int
64+
*/
65+
public $totalFixable = 0;
66+
6067
/**
6168
* A list of reports that have written partial report output.
6269
*
@@ -158,6 +165,7 @@ public function cacheFileReport(PHP_CodeSniffer_File $phpcsFile, array $cliValue
158165
$this->totalFiles++;
159166
$this->totalErrors += $reportData['errors'];
160167
$this->totalWarnings += $reportData['warnings'];
168+
$this->totalFixable += $reportData['fixable'];
161169
}
162170

163171
}//end cacheFileReport()
@@ -206,6 +214,7 @@ public function printReport(
206214
$this->totalFiles,
207215
$this->totalErrors,
208216
$this->totalWarnings,
217+
$this->totalFixable,
209218
$showSources,
210219
$reportWidth,
211220
$toScreen
@@ -244,6 +253,7 @@ public function prepareFileReport(PHP_CodeSniffer_File $phpcsFile)
244253
'filename' => $phpcsFile->getFilename(),
245254
'errors' => $phpcsFile->getErrorCount(),
246255
'warnings' => $phpcsFile->getWarningCount(),
256+
'fixable' => $phpcsFile->getFixableCount(),
247257
'messages' => array(),
248258
);
249259

@@ -263,9 +273,10 @@ public function prepareFileReport(PHP_CodeSniffer_File $phpcsFile)
263273
'message' => $data['message'],
264274
'source' => $data['source'],
265275
'severity' => $data['severity'],
276+
'fixable' => $data['fixable'],
266277
'type' => 'ERROR',
267278
);
268-
}
279+
}//end foreach
269280

270281
$errors[$line][$column] = $newErrors;
271282
}//end foreach
@@ -281,9 +292,10 @@ public function prepareFileReport(PHP_CodeSniffer_File $phpcsFile)
281292
'message' => $data['message'],
282293
'source' => $data['source'],
283294
'severity' => $data['severity'],
295+
'fixable' => $data['fixable'],
284296
'type' => 'WARNING',
285297
);
286-
}
298+
}//end foreach
287299

288300
if (isset($errors[$line]) === false) {
289301
$errors[$line] = array();

CodeSniffer/Reports/Checkstyle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ public function generateFileReport(
100100
* @param int $totalFiles Total number of files processed during the run.
101101
* @param int $totalErrors Total number of errors found during the run.
102102
* @param int $totalWarnings Total number of warnings found during the run.
103+
* @param int $totalFixable Total number of problems that can be fixed.
103104
* @param boolean $showSources Show sources?
104105
* @param int $width Maximum allowed line width.
105106
* @param boolean $toScreen Is the report being printed to screen?
@@ -111,6 +112,7 @@ public function generate(
111112
$totalFiles,
112113
$totalErrors,
113114
$totalWarnings,
115+
$totalFixable,
114116
$showSources=false,
115117
$width=80,
116118
$toScreen=true

CodeSniffer/Reports/Csv.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public function generateFileReport(
6666
$type = strtolower($error['type']);
6767
$source = $error['source'];
6868
$severity = $error['severity'];
69-
echo "\"$filename\",$line,$column,$type,\"$message\",$source,$severity".PHP_EOL;
69+
$fixable = (int) $error['fixable'];
70+
echo "\"$filename\",$line,$column,$type,\"$message\",$source,$severity,$fixable".PHP_EOL;
7071
}
7172
}
7273
}
@@ -84,6 +85,7 @@ public function generateFileReport(
8485
* @param int $totalFiles Total number of files processed during the run.
8586
* @param int $totalErrors Total number of errors found during the run.
8687
* @param int $totalWarnings Total number of warnings found during the run.
88+
* @param int $totalFixable Total number of problems that can be fixed.
8789
* @param boolean $showSources Show sources?
8890
* @param int $width Maximum allowed line width.
8991
* @param boolean $toScreen Is the report being printed to screen?
@@ -95,11 +97,12 @@ public function generate(
9597
$totalFiles,
9698
$totalErrors,
9799
$totalWarnings,
100+
$totalFixable,
98101
$showSources=false,
99102
$width=80,
100103
$toScreen=true
101104
) {
102-
echo 'File,Line,Column,Type,Message,Source,Severity'.PHP_EOL;
105+
echo 'File,Line,Column,Type,Message,Source,Severity,Fixable'.PHP_EOL;
103106
echo $cachedData;
104107

105108
}//end generate()

CodeSniffer/Reports/Diff.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function generateFileReport(
7878
* @param int $totalFiles Total number of files processed during the run.
7979
* @param int $totalErrors Total number of errors found during the run.
8080
* @param int $totalWarnings Total number of warnings found during the run.
81+
* @param int $totalFixable Total number of problems that can be fixed.
8182
* @param boolean $showSources Show sources?
8283
* @param int $width Maximum allowed line width.
8384
* @param boolean $toScreen Is the report being printed to screen?
@@ -89,6 +90,7 @@ public function generate(
8990
$totalFiles,
9091
$totalErrors,
9192
$totalWarnings,
93+
$totalFixable,
9294
$showSources=false,
9395
$width=80,
9496
$toScreen=true

CodeSniffer/Reports/Emacs.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ public function generateFileReport(
8585
* @param int $totalFiles Total number of files processed during the run.
8686
* @param int $totalErrors Total number of errors found during the run.
8787
* @param int $totalWarnings Total number of warnings found during the run.
88+
* @param int $totalFixable Total number of problems that can be fixed.
8889
* @param boolean $showSources Show sources?
8990
* @param int $width Maximum allowed line width.
9091
* @param boolean $toScreen Is the report being printed to screen?
@@ -96,6 +97,7 @@ public function generate(
9697
$totalFiles,
9798
$totalErrors,
9899
$totalWarnings,
100+
$totalFixable,
99101
$showSources=false,
100102
$width=80,
101103
$toScreen=true

0 commit comments

Comments
 (0)