Chris@17
|
1 <?php
|
Chris@17
|
2 /**
|
Chris@17
|
3 * Diff report for PHP_CodeSniffer.
|
Chris@17
|
4 *
|
Chris@17
|
5 * @author Greg Sherwood <gsherwood@squiz.net>
|
Chris@17
|
6 * @copyright 2006-2015 Squiz Pty Ltd (ABN 77 084 670 600)
|
Chris@17
|
7 * @license https://github.com/squizlabs/PHP_CodeSniffer/blob/master/licence.txt BSD Licence
|
Chris@17
|
8 */
|
Chris@17
|
9
|
Chris@17
|
10 namespace PHP_CodeSniffer\Reports;
|
Chris@17
|
11
|
Chris@17
|
12 use PHP_CodeSniffer\Files\File;
|
Chris@17
|
13
|
Chris@17
|
14 class Diff implements Report
|
Chris@17
|
15 {
|
Chris@17
|
16
|
Chris@17
|
17
|
Chris@17
|
18 /**
|
Chris@17
|
19 * Generate a partial report for a single processed file.
|
Chris@17
|
20 *
|
Chris@17
|
21 * Function should return TRUE if it printed or stored data about the file
|
Chris@17
|
22 * and FALSE if it ignored the file. Returning TRUE indicates that the file and
|
Chris@17
|
23 * its data should be counted in the grand totals.
|
Chris@17
|
24 *
|
Chris@17
|
25 * @param array $report Prepared report data.
|
Chris@17
|
26 * @param \PHP_CodeSniffer\File $phpcsFile The file being reported on.
|
Chris@17
|
27 * @param bool $showSources Show sources?
|
Chris@17
|
28 * @param int $width Maximum allowed line width.
|
Chris@17
|
29 *
|
Chris@17
|
30 * @return bool
|
Chris@17
|
31 */
|
Chris@17
|
32 public function generateFileReport($report, File $phpcsFile, $showSources=false, $width=80)
|
Chris@17
|
33 {
|
Chris@17
|
34 $errors = $phpcsFile->getFixableCount();
|
Chris@17
|
35 if ($errors === 0) {
|
Chris@17
|
36 return false;
|
Chris@17
|
37 }
|
Chris@17
|
38
|
Chris@17
|
39 $phpcsFile->disableCaching();
|
Chris@17
|
40 $tokens = $phpcsFile->getTokens();
|
Chris@17
|
41 if (empty($tokens) === true) {
|
Chris@17
|
42 if (PHP_CODESNIFFER_VERBOSITY === 1) {
|
Chris@17
|
43 $startTime = microtime(true);
|
Chris@17
|
44 echo 'DIFF report is parsing '.basename($report['filename']).' ';
|
Chris@17
|
45 } else if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
46 echo 'DIFF report is forcing parse of '.$report['filename'].PHP_EOL;
|
Chris@17
|
47 }
|
Chris@17
|
48
|
Chris@17
|
49 $phpcsFile->parse();
|
Chris@17
|
50
|
Chris@17
|
51 if (PHP_CODESNIFFER_VERBOSITY === 1) {
|
Chris@17
|
52 $timeTaken = ((microtime(true) - $startTime) * 1000);
|
Chris@17
|
53 if ($timeTaken < 1000) {
|
Chris@17
|
54 $timeTaken = round($timeTaken);
|
Chris@17
|
55 echo "DONE in {$timeTaken}ms";
|
Chris@17
|
56 } else {
|
Chris@17
|
57 $timeTaken = round(($timeTaken / 1000), 2);
|
Chris@17
|
58 echo "DONE in $timeTaken secs";
|
Chris@17
|
59 }
|
Chris@17
|
60
|
Chris@17
|
61 echo PHP_EOL;
|
Chris@17
|
62 }
|
Chris@17
|
63
|
Chris@17
|
64 $phpcsFile->fixer->startFile($phpcsFile);
|
Chris@17
|
65 }//end if
|
Chris@17
|
66
|
Chris@17
|
67 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
68 ob_end_clean();
|
Chris@17
|
69 echo "\t*** START FILE FIXING ***".PHP_EOL;
|
Chris@17
|
70 }
|
Chris@17
|
71
|
Chris@17
|
72 $fixed = $phpcsFile->fixer->fixFile();
|
Chris@17
|
73
|
Chris@17
|
74 if (PHP_CODESNIFFER_VERBOSITY > 1) {
|
Chris@17
|
75 echo "\t*** END FILE FIXING ***".PHP_EOL;
|
Chris@17
|
76 ob_start();
|
Chris@17
|
77 }
|
Chris@17
|
78
|
Chris@17
|
79 if ($fixed === false) {
|
Chris@17
|
80 return false;
|
Chris@17
|
81 }
|
Chris@17
|
82
|
Chris@17
|
83 $diff = $phpcsFile->fixer->generateDiff();
|
Chris@17
|
84 if ($diff === '') {
|
Chris@17
|
85 // Nothing to print.
|
Chris@17
|
86 return false;
|
Chris@17
|
87 }
|
Chris@17
|
88
|
Chris@17
|
89 echo $diff.PHP_EOL;
|
Chris@17
|
90 return true;
|
Chris@17
|
91
|
Chris@17
|
92 }//end generateFileReport()
|
Chris@17
|
93
|
Chris@17
|
94
|
Chris@17
|
95 /**
|
Chris@17
|
96 * Prints all errors and warnings for each file processed.
|
Chris@17
|
97 *
|
Chris@17
|
98 * @param string $cachedData Any partial report data that was returned from
|
Chris@17
|
99 * generateFileReport during the run.
|
Chris@17
|
100 * @param int $totalFiles Total number of files processed during the run.
|
Chris@17
|
101 * @param int $totalErrors Total number of errors found during the run.
|
Chris@17
|
102 * @param int $totalWarnings Total number of warnings found during the run.
|
Chris@17
|
103 * @param int $totalFixable Total number of problems that can be fixed.
|
Chris@17
|
104 * @param bool $showSources Show sources?
|
Chris@17
|
105 * @param int $width Maximum allowed line width.
|
Chris@17
|
106 * @param bool $interactive Are we running in interactive mode?
|
Chris@17
|
107 * @param bool $toScreen Is the report being printed to screen?
|
Chris@17
|
108 *
|
Chris@17
|
109 * @return void
|
Chris@17
|
110 */
|
Chris@17
|
111 public function generate(
|
Chris@17
|
112 $cachedData,
|
Chris@17
|
113 $totalFiles,
|
Chris@17
|
114 $totalErrors,
|
Chris@17
|
115 $totalWarnings,
|
Chris@17
|
116 $totalFixable,
|
Chris@17
|
117 $showSources=false,
|
Chris@17
|
118 $width=80,
|
Chris@17
|
119 $interactive=false,
|
Chris@17
|
120 $toScreen=true
|
Chris@17
|
121 ) {
|
Chris@17
|
122 echo $cachedData;
|
Chris@17
|
123 if ($toScreen === true && $cachedData !== '') {
|
Chris@17
|
124 echo PHP_EOL;
|
Chris@17
|
125 }
|
Chris@17
|
126
|
Chris@17
|
127 }//end generate()
|
Chris@17
|
128
|
Chris@17
|
129
|
Chris@17
|
130 }//end class
|